如何设置Spring AMQP的最大连接重试次数



我有一个场景,我的rabbit mq实例并不总是可用,并希望设置连接重试发生的最大次数,这是可能的amqp吗?

,

@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;
}

消息生产者只会在您发送消息时尝试创建连接。

消息消费者(容器工厂)将无限重试。

可以在一定次数的失败后将ConnectionListener添加到连接工厂,将stop()添加到侦听器容器。

@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) {
}
}

最新更新