Automatic reconnection of RabbitMQ channels

I found this stone:

If the connection fails, the client will need to establish a new connection with the broker. Any channels opened in the previous connection will be automatically closed, and they will also require re-opening.

So this is not good. I am going to write a large layer of processing automatic reconnections and recreate channels, and then encapsulate this from all my code. The problem is that this should already be done. Is this possible in Java RMQ libraries?

+7
source share
4 answers

Lyra Checkout : A high-availability RabbitMQ client that automatically restores resources (connections / channels / consumers) when they unexpectedly close.

+6
source

This may be a new RabbitMQ client feature, but I found this in my docs:

To enable automatic reconnection, use factory.setAutomaticRecoveryEnabled (true):

https://www.rabbitmq.com/api-guide.html

Looks like he should solve the problem.

+6
source

From RabbitMQ 3.3.0 (April 2014), this is possible with Java clients.

This release., Allows Java-based clients to automatically connect after a network failure.

I do not know if this is a change only for the server, a change only in the client libraries, which makes this possible, etc. Still exploring.

+2
source

Yes, I agree that this is the main drawback of current RabbitMQ client implementations. I have been using RMQ for about 2 years (.NET library), and a lot has changed during this time. It should be completely rewritten from scratch, and I just did not have time to do it.

But I have some pointers. First, I would create a wrapper class for your connection / channel object (you need a channel to perform AMQP operations, the only thing the connection is used for is creating channels). Then your wrapper class can keep track of whether the channel or connection is open, and act accordingly.

My code is as follows:

while (_iNeedToBeSendingAndReceiving) { try { //This blocks indefinitely while waiting for a connection. using (var channel = ConnectionWrapper.CreateChannel(string connectionString) { //Do stuff, blah, blah //When the connection or channel closes, an exception is thrown and //I move to the catch block. } catch(ConnectionInterruptException ex) { //Eat, yummy! } } 

My possible plan is to distract even this material and create a completely new way of interacting with the RabbitMQ library (or any other). I will let you know when I do some work on this issue, it may be in a couple of months.

+1
source

All Articles