问题
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 ?
回答1:
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.
回答2:
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
回答3:
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.
来源:https://stackoverflow.com/questions/42981299/automatic-retry-connection-to-broker-by-spring-rabbitmq