Anand03
Anand03

Reputation: 223

Spring message-driven-channel-adapter: handling connection refused error

I want to configure my message-driven-channel-adapter in such a way that I can grab the 'connection refused' error in case when: 1) the message broker is down or 2) A wrong broker URL is specified

I tried doing this with the code below but it did not work:

<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"  />

The processError() method is defined as below:

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

I am trying to grab the exception which is logged as below in case of connection exception:

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 

Can someone please help ?

Regards

Upvotes: 1

Views: 1083

Answers (1)

Artem Bilan
Artem Bilan

Reputation: 121177

Connection refused error isn't a part of Messaging flow, so it isn't handled by the errorChannel. M-m-m... Just because it is caused before any Message appears.

UPDATE

Starting with Spring Integration 4.2.2 the MqttConnectionFailedEvent is available for these cases.

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

Upvotes: 1

Related Questions