Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
G
glinfo-api
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
林业通
glinfo-api
Commits
4a252c7e
提交
4a252c7e
authored
5月 11, 2022
作者:
linzhenjie
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
加mqtt订阅
上级
6dcf1796
隐藏空白字符变更
内嵌
并排
正在显示
7 个修改的文件
包含
189 行增加
和
1 行删除
+189
-1
pom.xml
appapi/pom.xml
+5
-0
MqttConfig.java
...pi/src/main/java/tech/glinfo/enbao/config/MqttConfig.java
+130
-0
MqttGateway.java
...i/src/main/java/tech/glinfo/enbao/config/MqttGateway.java
+12
-0
application-dev.yml
appapi/src/main/resources/application-dev.yml
+11
-0
application-local.yml
appapi/src/main/resources/application-local.yml
+10
-1
application-prod.yml
appapi/src/main/resources/application-prod.yml
+11
-0
application-test.yml
appapi/src/main/resources/application-test.yml
+10
-0
没有找到文件。
appapi/pom.xml
浏览文件 @
4a252c7e
...
...
@@ -97,6 +97,11 @@
</exclusion>
</exclusions>
</dependency>
<!--mqtt-->
<dependency>
<groupId>
org.springframework.integration
</groupId>
<artifactId>
spring-integration-mqtt
</artifactId>
</dependency>
</dependencies>
...
...
appapi/src/main/java/tech/glinfo/enbao/config/MqttConfig.java
0 → 100644
浏览文件 @
4a252c7e
package
tech
.
glinfo
.
enbao
.
config
;
import
lombok.extern.slf4j.Slf4j
;
import
org.eclipse.paho.client.mqttv3.MqttConnectOptions
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.integration.annotation.IntegrationComponentScan
;
import
org.springframework.integration.annotation.ServiceActivator
;
import
org.springframework.integration.channel.DirectChannel
;
import
org.springframework.integration.core.MessageProducer
;
import
org.springframework.integration.mqtt.core.DefaultMqttPahoClientFactory
;
import
org.springframework.integration.mqtt.core.MqttPahoClientFactory
;
import
org.springframework.integration.mqtt.inbound.MqttPahoMessageDrivenChannelAdapter
;
import
org.springframework.integration.mqtt.outbound.MqttPahoMessageHandler
;
import
org.springframework.integration.mqtt.support.DefaultPahoMessageConverter
;
import
org.springframework.messaging.Message
;
import
org.springframework.messaging.MessageChannel
;
import
org.springframework.messaging.MessageHandler
;
import
org.springframework.messaging.MessagingException
;
import
java.util.List
;
@Slf4j
@Configuration
@IntegrationComponentScan
public
class
MqttConfig
{
@Value
(
"${spring.mqtt.username}"
)
private
String
username
;
@Value
(
"${spring.mqtt.password}"
)
private
String
password
;
@Value
(
"${spring.mqtt.url}"
)
private
String
hostUrl
;
@Value
(
"${spring.mqtt.client.id}"
)
private
String
clientId
;
@Value
(
"${spring.mqtt.default.topic}"
)
private
String
defaultTopic
;
@Value
(
"${spring.mqtt.completionTimeout}"
)
private
int
completionTimeout
;
//连接超时
@Bean
public
MqttConnectOptions
getMqttConnectOptions
(){
MqttConnectOptions
mqttConnectOptions
=
new
MqttConnectOptions
();
mqttConnectOptions
.
setUserName
(
username
);
mqttConnectOptions
.
setPassword
(
password
.
toCharArray
());
mqttConnectOptions
.
setServerURIs
(
new
String
[]{
hostUrl
});
mqttConnectOptions
.
setKeepAliveInterval
(
2
);
// 设置超时时间 单位为秒
mqttConnectOptions
.
setConnectionTimeout
(
10
);
mqttConnectOptions
.
setMaxInflight
(
100000000
);
return
mqttConnectOptions
;
}
@Bean
public
MqttPahoClientFactory
mqttClientFactory
()
{
DefaultMqttPahoClientFactory
factory
=
new
DefaultMqttPahoClientFactory
();
factory
.
setConnectionOptions
(
getMqttConnectOptions
());
return
factory
;
}
@Bean
@ServiceActivator
(
inputChannel
=
"mqttOutboundChannel"
)
public
MessageHandler
mqttOutbound
()
{
MqttPahoMessageHandler
messageHandler
=
new
MqttPahoMessageHandler
(
clientId
,
mqttClientFactory
());
messageHandler
.
setAsync
(
true
);
messageHandler
.
setDefaultTopic
(
defaultTopic
);
return
messageHandler
;
}
@Bean
public
MessageChannel
mqttOutboundChannel
()
{
return
new
DirectChannel
();
}
//接收通道
@Bean
public
MessageChannel
mqttInputChannel
()
{
return
new
DirectChannel
();
}
//配置client,监听的topic
@Bean
public
MessageProducer
inbound
()
{
// List<String> topicList = Arrays.asList(defaultTopic.trim().split(","));
// String[] topics = new String[topicList.size()];
// topicList.toArray(topics);
// MqttPahoMessageDrivenChannelAdapter adapter =
// new MqttPahoMessageDrivenChannelAdapter(clientId,mqttClientFactory(),defaultTopic);
// adapter.setCompletionTimeout(completionTimeout);
// adapter.setConverter(new DefaultPahoMessageConverter());
// adapter.setQos(1);
// adapter.setOutputChannel(mqttInputChannel());
// return adapter;
log
.
info
(
"clientId:{}"
,
clientId
);
MqttPahoMessageDrivenChannelAdapter
adapter
=
new
MqttPahoMessageDrivenChannelAdapter
(
clientId
+
"_inbound"
,
mqttClientFactory
(),
defaultTopic
);
adapter
.
setCompletionTimeout
(
completionTimeout
);
adapter
.
setConverter
(
new
DefaultPahoMessageConverter
());
adapter
.
setQos
(
1
);
adapter
.
setOutputChannel
(
mqttInputChannel
());
return
adapter
;
}
//通过通道获取数据
@Bean
@ServiceActivator
(
inputChannel
=
"mqttInputChannel"
)
public
MessageHandler
handler
()
{
return
new
MessageHandler
()
{
@Override
public
void
handleMessage
(
Message
<?>
message
)
throws
MessagingException
{
String
topic
=
message
.
getHeaders
().
get
(
"mqtt_receivedTopic"
).
toString
();
String
msg
=
message
.
getPayload
().
toString
();
// 这里可以处理接收的数据
log
.
info
(
"\n----------------------------START---------------------------\n"
+
"接收到订阅消息:\ntopic:"
+
topic
+
"\nmessage:"
+
msg
+
"\n-----------------------------END----------------------------"
);
}
};
}
}
appapi/src/main/java/tech/glinfo/enbao/config/MqttGateway.java
0 → 100644
浏览文件 @
4a252c7e
package
tech
.
glinfo
.
enbao
.
config
;
import
org.springframework.integration.annotation.MessagingGateway
;
import
org.springframework.integration.mqtt.support.MqttHeaders
;
import
org.springframework.messaging.handler.annotation.Header
;
@MessagingGateway
(
defaultRequestChannel
=
"mqttOutboundChannel"
)
public
interface
MqttGateway
{
void
sendToMqtt
(
String
data
,
@Header
(
MqttHeaders
.
TOPIC
)
String
topic
);
}
\ No newline at end of file
appapi/src/main/resources/application-dev.yml
浏览文件 @
4a252c7e
...
...
@@ -58,6 +58,17 @@ spring:
pool
:
enabled
:
false
#是否替换默认的连接池,使用ActiveMQ的连接池需引入的依赖
#mqtt配置
mqtt
:
url
:
tcp://mqtt.gonglian.info:1883
username
:
002WX212230209
password
:
nfefhjljt
client
:
id
:
'
iotclientid${random.int}'
default
:
topic
:
$share/api/sys/#
completionTimeout
:
3000
##多数据源的配置
#dynamic:
# datasource:
...
...
appapi/src/main/resources/application-local.yml
浏览文件 @
4a252c7e
...
...
@@ -57,7 +57,16 @@ spring:
trust-all
:
true
#信任所有的包
pool
:
enabled
:
false
#是否替换默认的连接池,使用ActiveMQ的连接池需引入的依赖
#mqtt配置
mqtt
:
url
:
tcp://mqtt.gonglian.info:1883
username
:
002WX212230209
password
:
nfefhjljt
client
:
id
:
'
iotclientid${random.int}'
default
:
topic
:
$share/api/sys/#
completionTimeout
:
3000
##多数据源的配置
#dynamic:
# datasource:
...
...
appapi/src/main/resources/application-prod.yml
浏览文件 @
4a252c7e
...
...
@@ -60,6 +60,17 @@ spring:
max-connections
:
10
idle-timeout
:
30000
#mqtt配置
mqtt
:
url
:
tcp://mqtt.gonglian.info:1883
username
:
002WX212230209
password
:
nfefhjljt
client
:
id
:
'
iotclientid${random.int}'
default
:
topic
:
$share/api/sys/#
completionTimeout
:
3000
##多数据源的配置
#dynamic:
# datasource:
...
...
appapi/src/main/resources/application-test.yml
浏览文件 @
4a252c7e
...
...
@@ -59,6 +59,16 @@ spring:
enabled
:
true
#是否替换默认的连接池,使用ActiveMQ的连接池需引入的依赖
max-connections
:
5
idle-timeout
:
30000
#mqtt配置
mqtt
:
url
:
tcp://mqtt.gonglian.info:1883
username
:
002WX212230209
password
:
nfefhjljt
client
:
id
:
'
iotclientid${random.int}'
default
:
topic
:
$share/api/sys/#
completionTimeout
:
3000
##多数据源的配置
#dynamic:
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论