I have a scenario where my rabbit mq instance is not always available and would like to set the maximum number of times a connection retry happens, Is this possible with amqp?
Example,
@Bean
public ConnectionFactory connectionFactory() {
CachingConnectionFactory factory = new CachingConnectionFactory();
factory.setUri("amqprl//");
factory ../ try uri connection for 4 times max then fail if still no connection
return factory;
}
Message producers will only try to create a connection when you send a message.
Message consumers (container factories) will retry indefinitely.
You can add a ConnectionListener
to the connection factory and stop()
the listener containers after some number of failures.
@FunctionalInterface
public interface ConnectionListener {
/**
* Called when a new connection is established.
* @param connection the connection.
*/
void onCreate(Connection connection);
/**
* Called when a connection is closed.
* @param connection the connection.
* @see #onShutDown(ShutdownSignalException)
*/
default void onClose(Connection connection) {
}
/**
* Called when a connection is force closed.
* @param signal the shut down signal.
* @since 2.0
*/
default void onShutDown(ShutdownSignalException signal) {
}
/**
* Called when a connection couldn't be established.
* @param exception the exception thrown.
* @since 2.2.17
*/
default void onFailed(Exception exception) {
}
}