spring mqtt: catch ConnectException

2019-07-25 15:50发布

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条回答
不美不萌又怎样
2楼-- · 2019-07-25 16:18

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

查看更多
登录 后发表回答