Hi!
I'm going to suppose that you are using Spring XML configuration to setup your connection factory. Let's say you have something like this:
Code:
<bean id="connectionFactory" class="org.springframework.amqp.rabbit.connection.SingleConnectionFactory">
<constructor-arg value="localhost"/>
<property name="port" value="5672"/>
<property name="username" value="I'am"/>
<property name="password" value="beautiful"/>
</bean>
Now, the shutdown listeners are not really worked out yet in this early version of Spring AMQP. But if you inspect the class SingleconnectionFactory, you will see some tips on how to add the listeners. You should see this method:
Code:
protected void prepareConnection(Connection con) throws IOException {
//TODO configure ShutdownListener, investigate reconnection exceptions
}
If you override that class and implement that method to add the listener to the "con" object, you should be ready to go.
Code:
public class RabbitConnectionFactory extends SingleConnectionFactory {
public RabbitConnectionFactory() {
}
public RabbitConnectionFactory(final String hostname) {
super(hostname);
}
@Override
protected void prepareConnection(Connection con) throws IOException {
con.addShutdownListener(new ShutdownListener(){
@Override
public void shutdownCompleted(final ShutdownSignalException cause) {
//TODO: Implement a way to reconnect? Perhaps with a timer that
//periodically tries to reconnect or by reconnecting next time a RabbitTemplate is used...
System.out.printf("Aye! %s \n", cause.getMessage());
}
});
}
}
Then, instead of declaring the original SingleConnectionFactory in the Spring XML config, use your implementation.
Code:
<bean id="connectionFactory" class="my.package.RabbitConnectionFactory">
<constructor-arg value="localhost"/>
<property name="port" value="5672"/>
<property name="username" value="I'am"/>
<property name="password" value="beautiful"/>
</bean>
Hope it helps.