|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+package com.sjls.nstthh.mqtt;
|
|
|
2
|
+
|
|
|
3
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
4
|
+import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
|
|
|
5
|
+import org.slf4j.Logger;
|
|
|
6
|
+import org.slf4j.LoggerFactory;
|
|
|
7
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
8
|
+import org.springframework.context.annotation.Bean;
|
|
|
9
|
+import org.springframework.context.annotation.Configuration;
|
|
|
10
|
+import org.springframework.integration.annotation.IntegrationComponentScan;
|
|
|
11
|
+import org.springframework.integration.annotation.ServiceActivator;
|
|
|
12
|
+import org.springframework.integration.channel.DirectChannel;
|
|
|
13
|
+import org.springframework.integration.core.MessageProducer;
|
|
|
14
|
+import org.springframework.integration.mqtt.core.DefaultMqttPahoClientFactory;
|
|
|
15
|
+import org.springframework.integration.mqtt.core.MqttPahoClientFactory;
|
|
|
16
|
+import org.springframework.integration.mqtt.inbound.MqttPahoMessageDrivenChannelAdapter;
|
|
|
17
|
+import org.springframework.integration.mqtt.outbound.MqttPahoMessageHandler;
|
|
|
18
|
+import org.springframework.integration.mqtt.support.DefaultPahoMessageConverter;
|
|
|
19
|
+import org.springframework.messaging.Message;
|
|
|
20
|
+import org.springframework.messaging.MessageChannel;
|
|
|
21
|
+import org.springframework.messaging.MessageHandler;
|
|
|
22
|
+
|
|
|
23
|
+@Configuration
|
|
|
24
|
+@IntegrationComponentScan
|
|
|
25
|
+@Slf4j
|
|
|
26
|
+public class MqttSenderConfig {
|
|
|
27
|
+
|
|
|
28
|
+ private static final Logger LOGGER = LoggerFactory.getLogger(MqttSenderConfig.class);
|
|
|
29
|
+
|
|
|
30
|
+ @Value("${spring.mqtt.username}")
|
|
|
31
|
+ private String username;
|
|
|
32
|
+
|
|
|
33
|
+ @Value("${spring.mqtt.password}")
|
|
|
34
|
+ private String password;
|
|
|
35
|
+
|
|
|
36
|
+ @Value("${spring.mqtt.url}")
|
|
|
37
|
+ private String hostUrl;
|
|
|
38
|
+
|
|
|
39
|
+ @Value("${spring.mqtt.publishclientid}")
|
|
|
40
|
+ private String publishclientid;
|
|
|
41
|
+
|
|
|
42
|
+ @Value("${spring.mqtt.subsribeclientid}")
|
|
|
43
|
+ private String subsribeclientid;
|
|
|
44
|
+
|
|
|
45
|
+ @Value("${spring.mqtt.default_topic}")
|
|
|
46
|
+ private String defaultTopic;
|
|
|
47
|
+
|
|
|
48
|
+ /**
|
|
|
49
|
+ * 订阅的bean名称
|
|
|
50
|
+ */
|
|
|
51
|
+ public static final String CHANNEL_NAME_IN = "mqttInboundChannel";
|
|
|
52
|
+ /**
|
|
|
53
|
+ * 发布的bean名称
|
|
|
54
|
+ */
|
|
|
55
|
+ public static final String CHANNEL_NAME_OUT = "mqttOutboundChannel";
|
|
|
56
|
+
|
|
|
57
|
+// @Autowired
|
|
|
58
|
+// private MqttCallbackHandler mqttCallbackHandler;
|
|
|
59
|
+
|
|
|
60
|
+ /**
|
|
|
61
|
+ * MQTT连接器选项
|
|
|
62
|
+ */
|
|
|
63
|
+ @Bean
|
|
|
64
|
+ public <hostUrl> MqttConnectOptions getMqttConnectOptions() {
|
|
|
65
|
+ MqttConnectOptions options = new MqttConnectOptions();
|
|
|
66
|
+ // 设置是否清空session,这里如果设置为false表示服务器会保留客户端的连接记录,
|
|
|
67
|
+ // 这里设置为true表示每次连接到服务器都以新的身份连接
|
|
|
68
|
+ options.setCleanSession(false);
|
|
|
69
|
+ // 设置连接的用户名
|
|
|
70
|
+ options.setUserName(username);
|
|
|
71
|
+ // 设置连接的密码
|
|
|
72
|
+ options.setPassword(password.toCharArray());
|
|
|
73
|
+ options.setServerURIs(new String[]{hostUrl});
|
|
|
74
|
+ // 设置超时时间 单位为秒
|
|
|
75
|
+ options.setConnectionTimeout(10);
|
|
|
76
|
+ // 设置会话心跳时间 单位为秒 服务器会每隔1.5*20秒的时间向客户端发送心跳判断客户端是否在线,但这个方法并没有重连的机制
|
|
|
77
|
+ options.setKeepAliveInterval(20);
|
|
|
78
|
+ // 设置“遗嘱”消息的话题,若客户端与服务器之间的连接意外中断,服务器将发布客户端的“遗嘱”消息。
|
|
|
79
|
+ //options.setWill("willTopic", WILL_DATA, 2, false);
|
|
|
80
|
+ return options;
|
|
|
81
|
+ }
|
|
|
82
|
+
|
|
|
83
|
+
|
|
|
84
|
+ /**
|
|
|
85
|
+ * MQTT客户端
|
|
|
86
|
+ */
|
|
|
87
|
+ @Bean
|
|
|
88
|
+ public MqttPahoClientFactory mqttClientFactory() {
|
|
|
89
|
+ DefaultMqttPahoClientFactory factory = new DefaultMqttPahoClientFactory();
|
|
|
90
|
+ factory.setConnectionOptions(getMqttConnectOptions());
|
|
|
91
|
+ return factory;
|
|
|
92
|
+ }
|
|
|
93
|
+
|
|
|
94
|
+ /**
|
|
|
95
|
+ * MQTT信息通道(生产者)
|
|
|
96
|
+ */
|
|
|
97
|
+ @Bean(name = CHANNEL_NAME_OUT)
|
|
|
98
|
+ public MessageChannel mqttOutboundChannel() {
|
|
|
99
|
+ return new DirectChannel();
|
|
|
100
|
+ }
|
|
|
101
|
+
|
|
|
102
|
+ /**
|
|
|
103
|
+ * MQTT消息处理器(生产者)
|
|
|
104
|
+ */
|
|
|
105
|
+ @Bean
|
|
|
106
|
+ @ServiceActivator(inputChannel = CHANNEL_NAME_OUT)
|
|
|
107
|
+ public MessageHandler mqttOutbound() {
|
|
|
108
|
+ MqttPahoMessageHandler messageHandler = new MqttPahoMessageHandler(
|
|
|
109
|
+ publishclientid, mqttClientFactory());
|
|
|
110
|
+ messageHandler.setAsync(true);
|
|
|
111
|
+ //if (null == topic) topic = defaultTopic;
|
|
|
112
|
+ messageHandler.setDefaultTopic(defaultTopic);
|
|
|
113
|
+ return messageHandler;
|
|
|
114
|
+ }
|
|
|
115
|
+
|
|
|
116
|
+ /**
|
|
|
117
|
+ * MQTT消息订阅绑定(消费者)
|
|
|
118
|
+ */
|
|
|
119
|
+ @Bean
|
|
|
120
|
+ public MessageProducer inbound() {
|
|
|
121
|
+ // 可以同时消费(订阅)多个Topic
|
|
|
122
|
+ MqttPahoMessageDrivenChannelAdapter adapter = new MqttPahoMessageDrivenChannelAdapter(
|
|
|
123
|
+ subsribeclientid, mqttClientFactory(), new String[]{defaultTopic});
|
|
|
124
|
+ adapter.setCompletionTimeout(5000);
|
|
|
125
|
+ adapter.setConverter(new DefaultPahoMessageConverter());
|
|
|
126
|
+ adapter.setQos(2);
|
|
|
127
|
+ // 设置订阅通道
|
|
|
128
|
+ adapter.setOutputChannel(mqttInboundChannel());
|
|
|
129
|
+ return adapter;
|
|
|
130
|
+ }
|
|
|
131
|
+
|
|
|
132
|
+ /**
|
|
|
133
|
+ * MQTT信息通道(消费者)
|
|
|
134
|
+ */
|
|
|
135
|
+ @Bean(name = CHANNEL_NAME_IN)
|
|
|
136
|
+ public MessageChannel mqttInboundChannel() {
|
|
|
137
|
+ return new DirectChannel();
|
|
|
138
|
+ }
|
|
|
139
|
+
|
|
|
140
|
+ /**
|
|
|
141
|
+ * MQTT消息处理器(消费者)
|
|
|
142
|
+ */
|
|
|
143
|
+ @Bean
|
|
|
144
|
+ @ServiceActivator(inputChannel = CHANNEL_NAME_IN)
|
|
|
145
|
+ public MessageHandler handler() {
|
|
|
146
|
+ return new MessageHandler() {
|
|
|
147
|
+ @Override
|
|
|
148
|
+ public void handleMessage(Message<?> message) {
|
|
|
149
|
+ try {
|
|
|
150
|
+ String topic = message.getHeaders().get("mqtt_receivedTopic").toString();
|
|
|
151
|
+ String qos = message.getHeaders().get("mqtt_receivedQos").toString();
|
|
|
152
|
+ String payload = message.getPayload().toString();
|
|
|
153
|
+ //mqttCallbackHandler.handle(topic,payload);
|
|
|
154
|
+ LOGGER.info("主题:"+topic);
|
|
|
155
|
+ LOGGER.info("内容:"+payload);
|
|
|
156
|
+ LOGGER.info("级别:"+qos);
|
|
|
157
|
+ //message.get
|
|
|
158
|
+ } catch (Exception e) {
|
|
|
159
|
+ LOGGER.error(e.getMessage(), e);
|
|
|
160
|
+ }
|
|
|
161
|
+ }
|
|
|
162
|
+ };
|
|
|
163
|
+ }
|
|
|
164
|
+}
|