记录springboot集成多个mqtt,实现重连后重新订阅

本文介绍了如何在SpringBoot项目中整合多个MQTT服务器,以满足不同需求。在实际应用中遇到的问题是,当MQTT客户端重连后,之前订阅的主题无法继续接收消息。为解决这个问题,文章详细阐述了引入相关依赖、配置连接、管理连接、设置回调函数以及项目监听等步骤。特别地,通过调整回调函数并启用自动重连选项,实现了重连后自动重新订阅已有的主题,确保消息的正常接收。
摘要由CSDN通过智能技术生成

springboot整合多个mqtt

前提:实际应用中,项目需要订阅其他服务器的消息,并且把本机消息发布,所以需要连接多个mqtt,实现不同的需求
参考:https://blog.csdn.net/qq_40083897/article/details/117333716
整合大致结构如下:

1. 引入依赖

 <!-- mqtt -->
 <dependency>
     <groupId>org.springframework.integration</groupId>
     <artifactId>spring-integration-stream</artifactId>
 </dependency>
 <dependency>
     <groupId>org.springframework.integration</groupId>
     <artifactId>spring-integration-mqtt</artifactId>
 </dependency>

2.连接配置

 #MQTT配置信息
  mqtt:
    # 设备连接
    channel1:
      username: xxxxxx
      password: xxxxxx
      host: tcp://192.168.xx.xxx:1883
      clientid: device_channel1
      timeout: 1000
      keepalive: 10
      cleanSession: false
    #本地连接
    channel2:
      username: xxxxxx
      password: xxxxxx
      host: tcp://192.168.xx.xxx:1883
      clientid: local_channel2
      timeout: 1000
      keepalive: 10
      cleanSession: false

3.连接配置

@Data
public class MqttConfiguration {
   

    private String host;
    private String clientId;
    private String userName;
    private String password;
    private String topic;
    private int timeout;
    private int keepAlive;
    private boolean cleanSession;
}

@Slf4j
@Component
@Configuration
@ConfigurationProperties(MqttConfiguration1.PREFIX)
public class MqttConfiguration1 extends MqttConfiguration{
   

    //指定配置文件application-local.properties中的属性名前缀
    public static final String PREFIX = "spring.mqtt.channel1";

}
@Slf4j
@Component
@Configuration
@ConfigurationProperties(MqttConfiguration2.PREFIX)
public class MqttConfiguration2 extends MqttConfiguration{
   

    //指定配置文件application-local.properties中的属性名前缀
    public static final String PREFIX = "spring.mqtt.channel2";

}

4.连接管理

@Slf4j
@Component
public class MqttClientConnection {
   

    private MqttClient mqttClient;

    /**
     * 系统的mqtt客户端id
     */
    private String mqttClientId;


    /**
     * 客户端
     */
    public static ConcurrentHashMap<String, MqttClientConnection> mqttClients = new ConcurrentHashMap();
    /**
     * 客户端connect连接mqtt服务器
     *
     * @param userName     用户名
     * @param passWord     密码
     * @param mqttCallback 回调函数
     **/
    public MqttClient setMqttClient(String host, String clientId, String userName, String passWord, boolean cleanSession, MqttCallback mqttCallback) throws MqttException {
   
        MqttConnectOptions options = mqttConnectOptions(host, clientId, userName, passWord, cleanSession);
        if (mqttCallback == null) {
   
            mqttClient.setCallback(new MqttClientCallback(mqttClientId));
        } else {
   
            mqttClient.setCallback(mqttCallback
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值