Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
mica
mica-mqtt
提交
9d205ba1
mica-mqtt
项目概览
mica
/
mica-mqtt
通知
71
Star
1
Fork
1
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
mica-mqtt
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
9d205ba1
编写于
7月 05, 2021
作者:
如梦技术
🐛
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
✨
完善 mica-mqtt
上级
205d5465
变更
8
隐藏空白更改
内联
并排
Showing
8 changed file
with
76 addition
and
20 deletion
+76
-20
mica-mqtt-core/src/main/java/net/dreamlu/iot/mqtt/core/client/MqttClientCreator.java
...a/net/dreamlu/iot/mqtt/core/client/MqttClientCreator.java
+2
-2
mica-mqtt-core/src/main/java/net/dreamlu/iot/mqtt/core/server/MqttServer.java
...ain/java/net/dreamlu/iot/mqtt/core/server/MqttServer.java
+11
-9
mica-mqtt-example/src/main/java/net/dreamlu/iot/mqtt/benchmark/MqttBenchmark.java
...in/java/net/dreamlu/iot/mqtt/benchmark/MqttBenchmark.java
+48
-0
mica-mqtt-example/src/main/java/net/dreamlu/iot/mqtt/client/MqttClientTest.java
...main/java/net/dreamlu/iot/mqtt/client/MqttClientTest.java
+5
-2
mica-mqtt-example/src/main/java/net/dreamlu/iot/mqtt/server/MqttPublishManager.java
.../java/net/dreamlu/iot/mqtt/server/MqttPublishManager.java
+3
-3
mica-mqtt-example/src/main/java/net/dreamlu/iot/mqtt/server/MqttServerTest.java
...main/java/net/dreamlu/iot/mqtt/server/MqttServerTest.java
+4
-1
mica-mqtt-example/src/main/resources/log4j2.properties
mica-mqtt-example/src/main/resources/log4j2.properties
+2
-2
pom.xml
pom.xml
+1
-1
未找到文件。
mica-mqtt-core/src/main/java/net/dreamlu/iot/mqtt/core/client/MqttClientCreator.java
浏览文件 @
9d205ba1
...
...
@@ -47,9 +47,9 @@ public final class MqttClientCreator {
*/
private
String
name
=
"Mica-Mqtt-Client"
;
/**
* ip,可为空,
为空 t-io
默认为 127.0.0.1
* ip,可为空,默认为 127.0.0.1
*/
private
String
ip
;
private
String
ip
=
"127.0.0.1"
;
/**
* 端口,默认:1883
*/
...
...
mica-mqtt-core/src/main/java/net/dreamlu/iot/mqtt/core/server/MqttServer.java
浏览文件 @
9d205ba1
...
...
@@ -28,6 +28,7 @@ import org.tio.core.ChannelContext;
import
org.tio.core.Tio
;
import
org.tio.server.ServerTioConfig
;
import
org.tio.server.TioServer
;
import
org.tio.utils.lock.ReadLockHandler
;
import
org.tio.utils.lock.SetWithLock
;
import
java.nio.ByteBuffer
;
...
...
@@ -217,15 +218,16 @@ public final class MqttServer {
*/
public
Boolean
publishAll
(
String
topic
,
ByteBuffer
payload
,
MqttQoS
qos
,
boolean
retain
)
{
SetWithLock
<
ChannelContext
>
contextSet
=
Tio
.
getAll
(
getServerConfig
());
Set
<
ChannelContext
>
channelContexts
=
contextSet
.
getObj
();
if
(
channelContexts
.
isEmpty
())
{
logger
.
warn
(
"Mqtt publish to all ChannelContext is empty."
);
return
false
;
}
for
(
ChannelContext
context
:
channelContexts
)
{
String
clientId
=
context
.
getBsId
();
publish
(
clientId
,
topic
,
payload
,
qos
,
retain
);
}
contextSet
.
handle
((
ReadLockHandler
<
Set
<
ChannelContext
>>)
channelContexts
->
{
if
(
channelContexts
.
isEmpty
())
{
logger
.
warn
(
"Mqtt publish to all ChannelContext is empty."
);
return
;
}
for
(
ChannelContext
context
:
channelContexts
)
{
String
clientId
=
context
.
getBsId
();
publish
(
clientId
,
topic
,
payload
,
qos
,
retain
);
}
});
return
true
;
}
...
...
mica-mqtt-example/src/main/java/net/dreamlu/iot/mqtt/benchmark/MqttBenchmark.java
0 → 100644
浏览文件 @
9d205ba1
/*
* Copyright (c) 2019-2029, Dreamlu 卢春梦 (596392912@qq.com & www.net.dreamlu.net).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
net.dreamlu.iot.mqtt.benchmark
;
import
net.dreamlu.iot.mqtt.codec.ByteBufferUtil
;
import
net.dreamlu.iot.mqtt.core.client.MqttClient
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
/**
* mqtt 压力测试
*
* @author L.cm
*/
public
class
MqttBenchmark
{
private
static
final
Logger
logger
=
LoggerFactory
.
getLogger
(
MqttBenchmark
.
class
);
public
static
void
main
(
String
[]
args
)
throws
Exception
{
// 1. 模拟 1w 连接,在开发机(i5-7500 4核4线程 win10 MqttServer 6G)1万连连接很轻松。
int
clientCount
=
1_0000
;
for
(
int
i
=
0
;
i
<
clientCount
;
i
++)
{
// 2. 初始化 mqtt 客户端
MqttClient
client
=
MqttClient
.
create
()
.
username
(
"admin"
)
.
password
(
"123456"
)
.
connect
();
// 3. 订阅服务端消息
client
.
subQos0
(
"/#"
,
(
topic
,
payload
)
->
{
logger
.
info
(
topic
+
'\t'
+
ByteBufferUtil
.
toString
(
payload
));
});
}
}
}
mica-mqtt-example/src/main/java/net/dreamlu/iot/mqtt/client/MqttClientTest.java
浏览文件 @
9d205ba1
...
...
@@ -19,6 +19,8 @@ package net.dreamlu.iot.mqtt.client;
import
net.dreamlu.iot.mqtt.codec.ByteBufferUtil
;
import
net.dreamlu.iot.mqtt.codec.MqttVersion
;
import
net.dreamlu.iot.mqtt.core.client.MqttClient
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
java.nio.ByteBuffer
;
import
java.nio.charset.StandardCharsets
;
...
...
@@ -31,6 +33,7 @@ import java.util.TimerTask;
* @author L.cm
*/
public
class
MqttClientTest
{
private
static
final
Logger
logger
=
LoggerFactory
.
getLogger
(
MqttClientTest
.
class
);
public
static
void
main
(
String
[]
args
)
throws
Exception
{
// 初始化 mqtt 客户端
...
...
@@ -42,11 +45,11 @@ public class MqttClientTest {
.
connect
();
client
.
subQos0
(
"/test/#"
,
(
topic
,
payload
)
->
{
System
.
out
.
println
(
topic
+
'\t'
+
ByteBufferUtil
.
toString
(
payload
));
logger
.
info
(
topic
+
'\t'
+
ByteBufferUtil
.
toString
(
payload
));
});
client
.
subQos0
(
"/#"
,
(
topic
,
payload
)
->
{
System
.
out
.
println
(
topic
+
'\t'
+
ByteBufferUtil
.
toString
(
payload
));
logger
.
info
(
topic
+
'\t'
+
ByteBufferUtil
.
toString
(
payload
));
});
Timer
timer
=
new
Timer
();
...
...
mica-mqtt-example/src/main/java/net/dreamlu/iot/mqtt/server/MqttPublishManager.java
浏览文件 @
9d205ba1
...
...
@@ -4,12 +4,12 @@ import net.dreamlu.iot.mqtt.core.common.MqttPendingPublish;
import
net.dreamlu.iot.mqtt.core.common.MqttPendingQos2Publish
;
import
net.dreamlu.iot.mqtt.core.server.IMqttPublishManager
;
import
java.util.LinkedHashMap
;
import
java.util.Map
;
import
java.util.concurrent.ConcurrentHashMap
;
public
class
MqttPublishManager
implements
IMqttPublishManager
{
private
final
Map
<
Integer
,
MqttPendingPublish
>
pendingPublishData
=
new
Linked
HashMap
<>();
private
final
Map
<
Integer
,
MqttPendingQos2Publish
>
pendingQos2PublishData
=
new
Linked
HashMap
<>();
private
final
Map
<
Integer
,
MqttPendingPublish
>
pendingPublishData
=
new
Concurrent
HashMap
<>();
private
final
Map
<
Integer
,
MqttPendingQos2Publish
>
pendingQos2PublishData
=
new
Concurrent
HashMap
<>();
@Override
public
void
addPendingPublish
(
int
messageId
,
MqttPendingPublish
pendingPublish
)
{
...
...
mica-mqtt-example/src/main/java/net/dreamlu/iot/mqtt/server/MqttServerTest.java
浏览文件 @
9d205ba1
...
...
@@ -22,6 +22,8 @@ import net.dreamlu.iot.mqtt.core.common.MqttSubscription;
import
net.dreamlu.iot.mqtt.core.server.DefaultMqttServerSubManager
;
import
net.dreamlu.iot.mqtt.core.server.IMqttMessageIdGenerator
;
import
net.dreamlu.iot.mqtt.core.server.MqttServer
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
java.io.IOException
;
import
java.nio.ByteBuffer
;
...
...
@@ -34,12 +36,13 @@ import java.util.TimerTask;
* @author L.cm
*/
public
class
MqttServerTest
{
private
static
final
Logger
logger
=
LoggerFactory
.
getLogger
(
MqttServerTest
.
class
);
public
static
void
main
(
String
[]
args
)
throws
IOException
{
DefaultMqttServerSubManager
subManager
=
new
DefaultMqttServerSubManager
();
// 服务端注册订阅
subManager
.
subscribe
(
new
MqttSubscription
(
MqttQoS
.
AT_MOST_ONCE
,
"/test/#"
,
((
topic
,
payload
)
->
{
System
.
out
.
println
(
topic
+
'\t'
+
ByteBufferUtil
.
toString
(
payload
));
logger
.
info
(
"subscribe:\t"
+
topic
+
'\t'
+
ByteBufferUtil
.
toString
(
payload
));
})));
IMqttMessageIdGenerator
messageIdGenerator
=
new
MqttMessageIdGenerator
();
...
...
mica-mqtt-example/src/main/resources/log4j2.properties
浏览文件 @
9d205ba1
log4j.rootLogger
=
DEBUG
、
log4j.rootLogger
=
DEBUG
appender.console.type
=
Console
appender.console.name
=
STDOUT
appender.console.layout.type
=
PatternLayout
appender.console.layout.pattern
=
%d %-5p %c{2} - %m%n
# debug
rootLogger.level
=
debug
rootLogger.level
=
INFO
rootLogger.appenderRef.stdout.ref
=
STDOUT
pom.xml
浏览文件 @
9d205ba1
...
...
@@ -19,7 +19,7 @@
<project.build.sourceEncoding>
UTF-8
</project.build.sourceEncoding>
<maven-flatten.version>
1.2.2
</maven-flatten.version>
<!-- tio version -->
<tio.version>
3.7.
2.v2021031
6-RELEASE
</tio.version>
<tio.version>
3.7.
3.v2021070
6-RELEASE
</tio.version>
</properties>
<modules>
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录