spring mqtt: catch ConnectException

拜拜、爱过 提交于 2019-12-11 10:43:27

问题


I have a question about spring.

I make a connection with MQTT broker using Spring-Paho MqttPahoMessageDrivenChannelAdapter. Here is a java config part:

@Bean
@Description("mqtt inbound adapter: receives mqtt messages")
public MessageProducer mqttInboundAdapter() {
    log.info("creating mqtt inbound adapter");
    MqttPahoMessageDrivenChannelAdapter adapter =
            new MqttPahoMessageDrivenChannelAdapter(
                    env.getProperty("mqtt.hostname")+":" +env.getProperty("mqtt.port"), 
                    "myClient",
                    "#");
    adapter.setCompletionTimeout(5000);
    adapter.setConverter(new DefaultPahoMessageConverter());
    adapter.setQos(1);
    adapter.setOutputChannel(mqttInputChannel());
    adapter.setErrorChannel(mqttErrorChannel());
    return adapter;
}

When the broker is off and the connection is not establiched the ConnectException is thrown. It is great, but I want not only to see the trace of it in the log, but also receive a warning email.

I hoped that it could be realized with the help of mqttErrorChannel, but ConnectException is not the case of usage of error channels. Is there any way to catch the ConnectException to another channel or in another way?

Thank you in advance.


回答1:


Starting with Spring Integration 4.2.2 the MqttConnectionFailedEvent is emitted, when we lost connection or can't connect on subscribe.

You can catch that ApplicationEvent for example with the ApplicationEventListeningMessageProducer and send it to the appropriate channel.

See more information in the Reference Manual: http://docs.spring.io/spring-integration/reference/html/mqtt.html



来源:https://stackoverflow.com/questions/36066471/spring-mqtt-catch-connectexception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!