** 本文代码有些许问题,处理方案见:解决 spring-integration-mqtt 频繁报 Lost connection 错误**
一、添加配置
1 | spring: |
二、客户端配置
1 | /* 客户端 */ |
三、发布消息
3.1 配置
1 | @Bean |
3.2 消息推送接口类
1 | @MessagingGateway(defaultRequestChannel = MqttConfig.OUTBOUND_CHANNEL) |
3.3 测试
1 | @RestController |
四、订阅消息
4.1 配置
1 | @Bean |
4.2 测试
1 | @Component |
注意事项
@ServiceActivator 和 @MessagingGateway 中绑定的 Channel 名,需与返回 MessageChannel 的 Bean 的方法名一样:
如发布者绑定的 Channel 名为 outboundChannel,则需要有对应的方法,如下:
1
2
3
4@Bean
public MessageChannel outboundChannel() {
return new DirectChannel();
}发布者与订阅者的 Channel 名不能相同
连接服务器的超时时间和订阅的超时时间单位不一样
参考
完整代码:GitHub