SpringBoot实现MQTT消息发送和接收

目录

1、maven依赖

2.yml配置

3.配置获取yml信息  

4.发送

4.1 相关问题:

4.2 只发不接收可使用Netty-client 具体看Netty那章,等我空了写

5.接收信息

6.监听

7.总结


Spring integration交互逻辑
对于发布者:

消息通过消息网关发送出去,由 MessageChannel 的实例 DirectChannel 处理发送的细节。
DirectChannel 收到消息后,内部通过 MessageHandler 的实例 MqttPahoMessageHandler 发送到指定的 Topic。
对于订阅者:

通过注入 MessageProducerSupport 的实例 MqttPahoMessageDrivenChannelAdapter,实现订阅 Topic 和绑定消息消费的 MessageChannel。
同样由 MessageChannel 的实例 DirectChannel 处理消费细节。Channel 消息后会发送给我们自定义的 MqttInboundMessageHandler 实例进行消费。

1、maven依赖

 <dependency>
            <groupId>org.springframework.integration</groupId>
            <artifactId>spring-integration-mqtt</artifactId>
            <version>5.3.1.RELEASE</version>
        </dependency>

2.yml配置

mqtt:
    appid: mqttx_${random.value}
    #订阅主题,多个主题用逗号分隔
    inputTopic:  test009
    #MQTT服务器地址
    services: tcp://ip:1883
    #mqtt用户名
    user: 
    #mqtt密码,默认无
    password: 
    #心跳间隔时间,默认300
    KeepAliveInterval: 30
    #是否不保持session,默认false
    CleanSession: false
    #是否自动连接,默认true
    AutomaticReconnect: true
    #连接超时,默认30000
    CompletionTimeout: 30000
    #传输质量,默认1
    Qos: 1

3.配置获取yml信息  

@Service
    @Value("${mqtt.appid}")
    private  String appid;

    @Value("${mqtt.inputTopic}")
    private  String[] inputTopic;//订阅主题

//    @Value("${mqtt.outTopic}")
//    private  String[] outTopic;//发布主题

    @Value("${mqtt.services}")
    private  String[] mqttServices;//服务器地址以及端口

    @Value("${mqtt.user}")
    private  String user;//用户名

    @Value("${mqtt.password}")
    private  String password;//密码

    @Value("${mqtt.KeepAliveInterval}")
    private  Integer KeepAliveInterval;//心跳时间

    @Value("${mqtt.CleanSession}")
    private  Boolean CleanSession;//是否不保持session,默认为session保持

    @Value("${mqtt.AutomaticReconnect}")
    private  Boolean AutomaticReconnect;//是否自动重联,默认为开启自动重联

    @Value("${mqtt.CompletionTimeout}")
    private  Long CompletionTimeout;//连接超时,默认为30秒

    @Value("${mqtt.Qos}")
    private  Integer Qos;//通信质量


    @Bean
    public MqttPahoClientFactory mqttClientFactory() {
        DefaultMqttPahoClientFactory factory = new DefaultMqttPahoClientFactory();//连接工厂类
        MqttConnectOptions options = new MqttConnectOptions();//连接参数
        options.setServerURIs(mqttServices);//连接地址
        if(user!=null) {
            options.setUserName(user);//用户名
        }
        if(password!=null) {
            options.setPassword(password.toCharArray());//密码
        }
        options.setKeepAliveInterval(KeepAliveInterval);//心跳时间
        options.setAutomaticReconnect(AutomaticReconnect);//断开是否自动重联
        options.setCleanSession(CleanSession);//保持session
        factory.setConnectionOptions(options);
        return factory;
    }

4.发送

@Configuration
@IntegrationComponentScan
@Slf4j
public class MqttOutboundConfiguration {
    @Autowired
    private MqttMessageService mqttMessageService;

    @Bean
   @ServiceActivator(inputChannel = "mqttOutboundChannel")
    public  MessageHandler mqttOutbound() {
        MqttPahoMessageHandler messageHandler = new MqttPahoMessageHandler(mqttMess
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值