简体   繁体   中英

Automatic retry connection to broker by spring-rabbitmq

I have read this fragment of docs:

RabbitMQ Automatic Connection/Topology recovery

Since the first version of Spring AMQP, the framework has provided its own connection and channel recovery in the event of a broker failure. Also, as discussed in Section 3.1.10, “Configuring the broker”, the RabbitAdmin will re-declare any infrastructure beans (queues etc) when the connection is re-established. It therefore does not rely on the Auto Recovery that is now provided by the amqp-client library. Spring AMQP now uses the 4.0.x version of amqp-client, which has auto recovery enabled by default. Spring AMQP can still use its own recovery mechanisms if you wish, disabling it in the client, (by setting the automaticRecoveryEnabled property on the underlying RabbitMQ connectionFactory to false). However, the framework is completely compatible with auto recovery being enabled. This means any consumers you create within your code (perhaps via RabbitTemplate.execute()) can be recovered automatically.

I am not sure If I correctly understand. In my application.properties I have defined port and host. During starting my spring-boot app it successfully established connection and all necessary beans to communicate with queue.

However, what in case when during start my app broker is shutdown and it will be launched five minutes after starting of app ? Does spring-rabbitmq manage to reconnect and define all beans ?

That's correct. Spring AMQP manages the re-connection and recovery automatically.

This subject isn't related to bean definitions. If you talk about Broker entities declaration, then yes, that are processed really on the connection establishing.

I've had a similar issue, you just have to put a property on the connection factory configuration.

As per the article here set factory.setAutomaticRecoveryEnabled(true); and factory.setNetworkRecoveryInterval(10000); on the factory and the rabbit client will try to reconnect when the rabbit server is down or the connection is lost.

Because you are using spring configuration for the connection factory your connection factory will be like the following

<bean id="connectionFactory"
      class="org.springframework.amqp.rabbit.connection.CachingConnectionFactory">
    <constructor-arg value="somehost"/>
    <property name="username" value="guest"/>
    <property name="password" value="guest"/>
    <property name="automaticRecoveryEnabled" value="true"/>
    <property name="networkRecoveryInterval" value="100000"/>
</bean>

Connection factory reference here

Yes, the connections will be recreated when the broker is back on line. The default recovery interval is 5 seconds. You can change the recovery interval by setting container.setRecoveryInterval(30000); where container is a SimpleMessageListenerContainer . Setting recovery interval in the underlying connection factory cachingConnectionFactory.getRabbitConnectionFactory().setNetworkRecoveryInterval(int) seems not reflecting.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM