Spring消息驱动通道适配器:处理连接拒绝错误

时间:2015-11-05 01:06:39

标签: spring-integration mqtt

我想以这样的方式配置我的消息驱动通道适配器,以便在以下情况下我可以获取“连接被拒绝”错误:     1)消息代理已关闭或2)指定了错误的代理URL

我尝试使用下面的代码执行此操作,但它不起作用:

<int:channel id="invalidChannel" />

<int-mqtt:message-driven-channel-adapter 
     id="myAdapter" 
     client-id="${inbound.client.id}"
     url="${host.url}"
     topics="${inbound.topic}"
     channel="incomingChannel"
     error-channel="errorChannel"
     client-factory="clientFactory" />

<int:exception-type-router input-channel="errorChannel">
        <int:mapping exception-type="java.net.ConnectException" channel="invalidChannel" />
    </int:exception-type-router>


<int:service-activator id="ErrorActivator" 
            input-channel="invalidChannel" 
            ref="errorListener" 
            method="processError" />


<bean id="errorListener" class="com.ErrorListener"  />

processError()方法定义如下:

public void processError(Message<MessageHandlingException> message)
    {
        System.out.println("an error / exception occured");
    }

我正在尝试获取在连接异常的情况下记录的异常:

Unable to connect to server (32103) - java.net.ConnectException: Connection refused: connect
        at org.eclipse.paho.client.mqttv3.internal.TCPNetworkModule.start(TCPNetworkModule.java:79)
        at org.eclipse.paho.client.mqttv3.internal.ClientComms$ConnectBG.run(ClientComms.java:590)
        at java.lang.Thread.run(Thread.java:745)
Caused by: java.net.ConnectException: Connection refused: connect
        at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
        at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85)
        at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
        at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
        at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
        at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
        at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
        at java.net.Socket.connect(Socket.java:579)
        at org.eclipse.paho.client.mqttv3.internal.TCPNetworkModule.start(TCPNetworkModule.java:70)
        ... 2 more 

有人可以帮忙吗?

此致

1 个答案:

答案 0 :(得分:1)

Connection refused错误不是Messaging流的一部分,因此errorChannel不会处理它。 M-m-m ......只是因为它是在任何Message出现之前引起的。

<强>更新

从Spring Integration 4.2.2开始,MqttConnectionFailedEvent可用于这些情况。

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