Detect Exception on RabbitMq connection

1.1k Views Asked by At

Using spring integration to transfer message from RabbitMQ to MQ works well.

If i stop the RabbitMQ server, then we have an error on log files :

ERROR o.s.a.r.c.CachingConnectionFactory - Channel shutdown: connection error; protocol method: #method<connection.close>(reply-code=320, reply-text=CONNECTION_FORCED - Node was put into maintenance mode, class-id=0, method-id=0)

How can we intercept this exception ?

This works fine when adding ExceptionListener on jms DefaultMessageListenerContainer

Following the configuration of beans:

<bean id="connectionAmqpFactorySrc" class="com.rabbitmq.client.ConnectionFactory">
    <property name="automaticRecoveryEnabled" value="true"/>
    <property name="networkRecoveryInterval" value="10000"/>
</bean>

<rabbit:connection-factory  id="rabbitConnectionFactory" connection-factory="connectionAmqpFactorySrc"
    username="guest" 
    password="guest" 
    addresses="XX.XX.XX.XX"
    cache-mode="CONNECTION" 
    virtual-host="/"  
    shuffle-addresses="true" />


<bean id="fixedBackOffRabbitMQ" class="org.springframework.util.backoff.FixedBackOff">
    <constructor-arg index="0" value="10000" />
    <constructor-arg index="1" value="3" />
</bean>
    
<bean id="myListener" class="org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer">
    <property name="connectionFactory" ref="rabbitConnectionFactory" />
    <property name="queueNames" value="MyQueue" />
    <property name="recoveryBackOff" ref="fixedBackOffRabbitMQ"/>
    <property name="channelTransacted" value="true"></property>
    <property name="errorHandler" ref="errorHandler"></property>
</bean>
    
<int-amqp:inbound-channel-adapter   channel="channelRmqMQ" 
        id="inboundChannelAdapter" 
        auto-startup="true" listener-container="myListener" error-channel="processChannel1" />

EDIT1

As you advised me I use the definition of a bean like that :

<bean id="listeners" class="java.util.ArrayList">
    <constructor-arg>
        <list>
            <ref bean="connectionAmqpListener" />
        </list>
    </constructor-arg>
</bean>

<bean id="rabbitConnectionFactory"  class="org.springframework.amqp.rabbit.connection.CachingConnectionFactory">
    <constructor-arg value="connectionAmqpFactorySrc"/>
    <property name="username" value="guest"/>
    <property name="password" value="guest"/>
    <property name="addresses" value="XX.XX.XX.XX"/>
    <property name="cacheMode" value="CONNECTION"/>
    <property name="virtualHost" value="/"/>
    <property name="shuffleAddresses" value="true"/>
    <property name="connectionListeners" ref="listeners"/>
</bean>

With the ConnectionAmqpListener.java

public class ConnectionAmqpListener implements ConnectionListener {
    
    private final Log LOG = LogFactory.getLog(ConnectionAmqpListener.class);
    
    public ConnectionAmqpListener() {
        super();
    }
    
    public void onCreate(Connection connection) {
        System.out.println("Open connection");
    }
    
    public void onClose(Connection connection) {
        System.out.println("Connection is closed");
    }
    
    public void onShutDown(ShutdownSignalException signal) {
        System.out.println("Connection is shutdown");
        System.exit(-1);
    }
}   

This works fine, when i stop the broker , the method onShutDown is called.

But if i restart my process (with the broker down) i do not have any message in the log file and the process is stopped.

Do you have any recommandations on how to get informations if the connection fails?

END EDIT1

Thanks for your help

Regards,

Eric

1

There are 1 best solutions below

2
Artem Bilan On

See ConnectionFactory:

void addConnectionListener(ConnectionListener listener)

That callback has this hook:

/**
 * Called when a connection is force closed.
 * @param signal the shut down signal.
 * @since 2.0
 */
default void onShutDown(ShutdownSignalException signal) {
}