|
|
@@ -0,0 +1,96 @@
|
|
|
+package com.fs.framework.config;
|
|
|
+
|
|
|
+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.ServiceActivator;
|
|
|
+import org.springframework.integration.channel.DirectChannel;
|
|
|
+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.MessageChannel;
|
|
|
+import org.springframework.messaging.MessageHandler;
|
|
|
+
|
|
|
+@Configuration
|
|
|
+public class MqttConfig {
|
|
|
+
|
|
|
+ @Value("${mqtt.host}")
|
|
|
+ private String host;
|
|
|
+
|
|
|
+ @Value("${mqtt.username}")
|
|
|
+ private String username;
|
|
|
+
|
|
|
+ @Value("${mqtt.password}")
|
|
|
+ private String password;
|
|
|
+
|
|
|
+ @Value("${mqtt.clientId}")
|
|
|
+ private String clientId;
|
|
|
+
|
|
|
+ @Value("${mqtt.defaultTopic}")
|
|
|
+ private String defaultTopic;
|
|
|
+
|
|
|
+ @Value("${mqtt.timeout}")
|
|
|
+ private int timeout;
|
|
|
+
|
|
|
+ @Value("${mqtt.keepalive}")
|
|
|
+ private int keepalive;
|
|
|
+
|
|
|
+ @Bean
|
|
|
+ public MqttPahoClientFactory mqttClientFactory() {
|
|
|
+ DefaultMqttPahoClientFactory factory = new DefaultMqttPahoClientFactory();
|
|
|
+ MqttConnectOptions options = new MqttConnectOptions();
|
|
|
+ options.setServerURIs(new String[]{host});
|
|
|
+ options.setUserName(username);
|
|
|
+ options.setPassword(password.toCharArray());
|
|
|
+ options.setConnectionTimeout(timeout);
|
|
|
+ options.setKeepAliveInterval(keepalive);
|
|
|
+ options.setCleanSession(true);
|
|
|
+ factory.setConnectionOptions(options);
|
|
|
+ return factory;
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== 订阅(入站)====================
|
|
|
+
|
|
|
+ @Bean
|
|
|
+ public MessageChannel mqttInputChannel() {
|
|
|
+ return new DirectChannel();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Bean
|
|
|
+ public MqttPahoMessageDrivenChannelAdapter mqttInbound() {
|
|
|
+ MqttPahoMessageDrivenChannelAdapter adapter =
|
|
|
+ new MqttPahoMessageDrivenChannelAdapter(clientId + "-inbound", mqttClientFactory(), defaultTopic);
|
|
|
+ adapter.setCompletionTimeout(5000);
|
|
|
+ adapter.setConverter(new DefaultPahoMessageConverter());
|
|
|
+ adapter.setQos(1);
|
|
|
+ adapter.setOutputChannel(mqttInputChannel());
|
|
|
+ return adapter;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Bean
|
|
|
+ @ServiceActivator(inputChannel = "mqttInputChannel")
|
|
|
+ public MessageHandler mqttMessageHandler() {
|
|
|
+ return new MqttMessageHandler();
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== 发布(出站)====================
|
|
|
+
|
|
|
+ @Bean
|
|
|
+ public MessageChannel mqttOutputChannel() {
|
|
|
+ return new DirectChannel();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Bean
|
|
|
+ @ServiceActivator(inputChannel = "mqttOutputChannel")
|
|
|
+ public MessageHandler mqttOutbound() {
|
|
|
+ MqttPahoMessageHandler handler =
|
|
|
+ new MqttPahoMessageHandler(clientId + "-outbound", mqttClientFactory());
|
|
|
+ handler.setAsync(true);
|
|
|
+ handler.setDefaultTopic(defaultTopic);
|
|
|
+ handler.setDefaultQos(1);
|
|
|
+ return handler;
|
|
|
+ }
|
|
|
+}
|