|
@@ -8,11 +8,13 @@ import org.springframework.context.annotation.Configuration;
|
|
|
import org.springframework.integration.annotation.MessagingGateway;
|
|
import org.springframework.integration.annotation.MessagingGateway;
|
|
|
import org.springframework.integration.annotation.ServiceActivator;
|
|
import org.springframework.integration.annotation.ServiceActivator;
|
|
|
import org.springframework.integration.channel.DirectChannel;
|
|
import org.springframework.integration.channel.DirectChannel;
|
|
|
|
|
+import org.springframework.integration.mqtt.core.DefaultMqttPahoClientFactory;
|
|
|
import org.springframework.integration.mqtt.outbound.MqttPahoMessageHandler;
|
|
import org.springframework.integration.mqtt.outbound.MqttPahoMessageHandler;
|
|
|
import org.springframework.integration.mqtt.support.MqttHeaders;
|
|
import org.springframework.integration.mqtt.support.MqttHeaders;
|
|
|
import org.springframework.messaging.MessageChannel;
|
|
import org.springframework.messaging.MessageChannel;
|
|
|
import org.springframework.messaging.MessageHandler;
|
|
import org.springframework.messaging.MessageHandler;
|
|
|
import org.springframework.messaging.handler.annotation.Header;
|
|
import org.springframework.messaging.handler.annotation.Header;
|
|
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
|
|
|
|
|
import java.util.Map;
|
|
import java.util.Map;
|
|
|
|
|
|
|
@@ -20,7 +22,7 @@ import java.util.Map;
|
|
|
* @author han
|
|
* @author han
|
|
|
* @date 2025/03/20 14:31
|
|
* @date 2025/03/20 14:31
|
|
|
*/
|
|
*/
|
|
|
-@Configuration
|
|
|
|
|
|
|
+@Component
|
|
|
public class MqttOutConfig {
|
|
public class MqttOutConfig {
|
|
|
public MqttBaseConfig mqttBaseConfig;
|
|
public MqttBaseConfig mqttBaseConfig;
|
|
|
|
|
|
|
@@ -40,24 +42,28 @@ public class MqttOutConfig {
|
|
|
return new DirectChannel();
|
|
return new DirectChannel();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* 发送消息和消费消息Channel可以使用相同MqttPahoClientFactory
|
|
* 发送消息和消费消息Channel可以使用相同MqttPahoClientFactory
|
|
|
|
|
+ * 注意:这个方法不再由Spring自动创建,而是在需要时手动创建
|
|
|
*
|
|
*
|
|
|
- * @return
|
|
|
|
|
|
|
+ * @param username MQTT用户名
|
|
|
|
|
+ * @param factory MQTT客户端工厂
|
|
|
|
|
+ * @return MessageHandler实例
|
|
|
*/
|
|
*/
|
|
|
- @Bean(name = MESSAGE_NAME)
|
|
|
|
|
- @ServiceActivator(inputChannel = CHANNEL_NAME_OUT)
|
|
|
|
|
- public MessageHandler outbound() {
|
|
|
|
|
|
|
+ public MessageHandler outbound(String username, DefaultMqttPahoClientFactory factory) {
|
|
|
// 根据username动态生成client-id,格式:mqttx-username
|
|
// 根据username动态生成client-id,格式:mqttx-username
|
|
|
- String clientId = "mqttx-" + mqttBaseConfig.getUsername();
|
|
|
|
|
|
|
+ String clientId = "mqttx-" + username;
|
|
|
MqttPahoMessageHandler messageHandler =
|
|
MqttPahoMessageHandler messageHandler =
|
|
|
- new MqttPahoMessageHandler(clientId, mqttBaseConfig.mqttClientFactory());
|
|
|
|
|
|
|
+ new MqttPahoMessageHandler(clientId, factory);
|
|
|
//如果设置成true,发送消息时将不会阻塞。
|
|
//如果设置成true,发送消息时将不会阻塞。
|
|
|
messageHandler.setAsync(true);
|
|
messageHandler.setAsync(true);
|
|
|
messageHandler.setDefaultTopic(DEFAULT_TOPIC);
|
|
messageHandler.setDefaultTopic(DEFAULT_TOPIC);
|
|
|
return messageHandler;
|
|
return messageHandler;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ // 注意:这个接口需要被Spring扫描到,所以我们保留@MessagingGateway注解
|
|
|
|
|
+ // 但是我们移除了其他@Bean注解,以避免Spring自动创建时出现NullPointerException
|
|
|
@MessagingGateway(defaultRequestChannel = CHANNEL_NAME_OUT)
|
|
@MessagingGateway(defaultRequestChannel = CHANNEL_NAME_OUT)
|
|
|
public interface MqttGateway {
|
|
public interface MqttGateway {
|
|
|
/**
|
|
/**
|