本文介绍了春天mqtt:捕获ConnectException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于春天的问题.

I have a question about spring.

我使用Spring-Paho MqttPahoMessageDrivenChannelAdapter与MQTT代理建立连接.这是一个Java配置部分:

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;
}

当代理关闭并且未建立连接时,将抛出ConnectException.很好,但我不仅要在日志中看到它的踪迹,还要收到警告电子邮件.

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.

我希望可以在mqttErrorChannel的帮助下实现,但是ConnectException .是否有任何方法可以将ConnectException捕获到另一个通道或以其他方式捕获?

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?

谢谢.

推荐答案

从Spring Integration 4.2.2开始,当我们失去连接或无法进行订阅时,会发出MqttConnectionFailedEvent.

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

您可以使用ApplicationEventListeningMessageProducer捕获该ApplicationEvent并将其发送到适当的频道.

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

请参阅参考手册中的更多信息: http://docs. spring.io/spring-integration/reference/html/mqtt.html

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

这篇关于春天mqtt:捕获ConnectException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-24 13:15