spring-integration-mqtt 网络断开时无法启动应用程序
Posted
技术标签:
【中文标题】spring-integration-mqtt 网络断开时无法启动应用程序【英文标题】:spring-integration-mqtt failed to start app when the network is disconnected 【发布时间】:2022-01-19 06:00:42 【问题描述】:配置
@Configuration
@IntegrationComponentScan
public class MqttV5ChannelConfig
@Bean
public MqttConnectionOptions getMqttConnectionOptions() throws Exception
MqttConnectionOptions options = new MqttConnectionOptions();
options.setUserName(xxx);
options.setPassword(xxx);
options.setServerURIs(xxx);
options.setAutomaticReconnect(true);
options.setCleanStart(false);
@Bean
public MessageProducer inbound(MqttConnectionOptions options)
Mqttv5PahoMessageDrivenChannelAdapter adapter = new Mqttv5PahoMessageDrivenChannelAdapter(options, "Client_" + System.currentTimeMillis(), "test");
adapter.setCompletionTimeout(5000);
adapter.setQos(0);
adapter.setOutputChannel(mqttInboundChannel());
adapter.setErrorChannel(errorChannel());
return adapter;
@Bean
@ServiceActivator(inputChannel = "mqttOutboundChannel")
public MessageHandler mqttOutbound(MqttConnectionOptions options)
Mqttv5PahoMessageHandler handler = new Mqttv5PahoMessageHandler(options, "Client_" + System.currentTimeMillis());
handler.setAsync(true);
handler.setDefaultTopic("test");
return handler;
@Bean
public MessageChannel mqttOutboundChannel()
return new DirectChannel();
@Bean
public MessageChannel mqttInboundChannel()
return new DirectChannel();
@Bean
public MessageChannel errorChannel()
return new DirectChannel();
错误日志
org.springframework.context.ApplicationContextException: Failed to start bean 'mqttV5ChannelConfig.mqttOutbound.serviceActivator'; nested exception is java.lang.IllegalStateException: Cannot connect 'MqttAsyncClient' for: mqttOutbound
at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:181)
at org.springframework.context.support.DefaultLifecycleProcessor.access$200(DefaultLifecycleProcessor.java:54)
at org.springframework.context.support.DefaultLifecycleProcessor$LifecycleGroup.start(DefaultLifecycleProcessor.java:356)
问题
我希望网络断开时启动不会失败。当网络连接或我推送消息时它会自动重新连接。
如何配置?
【问题讨论】:
【参考方案1】:在inbound
bean 中,adapter.setAutoStartup(false)
将阻止上下文在初始化期间启动适配器。
然后您可以手动 start()
适配器(大概在 try/catch 块中)。
【讨论】:
我认为我们在Mqttv5PahoMessageHandler
中有一个错误:我们不能重新抛出异常 - 只是事件和日志消息。其余逻辑应依赖于在后台内部完成的options.setAutomaticReconnect(true)
。我今天会和github.com/spring-projects/spring-integration/issues/3697
修复在这里:github.com/spring-projects/spring-integration/pull/3698
太好了。谢谢。以上是关于spring-integration-mqtt 网络断开时无法启动应用程序的主要内容,如果未能解决你的问题,请参考以下文章