|
@@ -0,0 +1,60 @@
|
|
|
|
+package com.lantone.message.config;
|
|
|
|
+
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
|
+import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
+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.outbound.MqttPahoMessageHandler;
|
|
|
|
+import org.springframework.messaging.Message;
|
|
|
|
+import org.springframework.messaging.MessageChannel;
|
|
|
|
+import org.springframework.messaging.MessageHandler;
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * @Description: (服务端)即时消息发布者相关配置
|
|
|
|
+ * @author: rengb
|
|
|
|
+ * @time: 2021/1/5 18:27
|
|
|
|
+ */
|
|
|
|
+@Slf4j
|
|
|
|
+@Configuration
|
|
|
|
+public class MqttOutboundConfig {
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ private MqttConfig mqttConfig;
|
|
|
|
+
|
|
|
|
+ @Bean
|
|
|
|
+ public MqttPahoClientFactory mqttClientFactory() {
|
|
|
|
+ DefaultMqttPahoClientFactory factory = new DefaultMqttPahoClientFactory();
|
|
|
|
+ MqttConnectOptions options = new MqttConnectOptions();
|
|
|
|
+ options.setServerURIs(new String[] { mqttConfig.getUrl() });
|
|
|
|
+ factory.setConnectionOptions(options);
|
|
|
|
+ return factory;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Bean
|
|
|
|
+ @ServiceActivator(inputChannel = "mqttOutboundChannel")
|
|
|
|
+ public MessageHandler mqttOutbound() {
|
|
|
|
+ MqttPahoMessageHandler messageHandler =
|
|
|
|
+ new MqttPahoMessageHandler("publisherClient", mqttClientFactory()) {
|
|
|
|
+ @Override
|
|
|
|
+ public void handleMessage(Message<?> message) {
|
|
|
|
+ super.handleMessage(message);
|
|
|
|
+ //处理发布消息 此处可作为业务处理的入口
|
|
|
|
+ log.info("handleMessage : {}", message.getPayload());
|
|
|
|
+ }
|
|
|
|
+ };
|
|
|
|
+ messageHandler.setAsync(true);
|
|
|
|
+ messageHandler.setDefaultTopic(mqttConfig.getDefaultTopic());
|
|
|
|
+ return messageHandler;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Bean
|
|
|
|
+ public MessageChannel mqttOutboundChannel() {
|
|
|
|
+ return new DirectChannel();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+}
|