Sthita
Sthita

Reputation: 1782

RabbitMQ Java client 4.0.0 reconnect Failed

When I force disconnect the client connection via the rabbitmq management interface, it is not reconnecting back. I am using client 4.0.0. Please find my code below.

public void processQueue(){
    // Establishing a Connection with RabbitMQ server, 
    // running in the local machine, localhost
    ConnectionFactory factory = new ConnectionFactory();
    Connection connection=null;
    try {
        factory.setHost("localhost");
        factory.setRequestedHeartbeat(5);
        factory.setAutomaticRecoveryEnabled(true);
        factory.setConnectionTimeout(5000);
        factory.setNetworkRecoveryInterval(10000);
        connection = factory.newConnection();

        // creating a channel with first_queue
        Channel channel = connection.createChannel();
        channel.queueDeclare("xyz", true, false, false, null);


        // creating the Consumer, that will be receive a message and convert to String
        Consumer consumer = new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
                    byte[] body) throws IOException {
                String message = new String(body, "UTF-8");
                System.out.println("Java Queue - Message Received '" + message + "'");
            }
        };
        // loop that waits for message      
        channel.basicConsume("xyz", true, consumer);

    } catch (Exception e) {
        System.out.println("server is Down !");
        System.out.println(e.getMessage());
        try {
            if(connection!=null)
                connection.close();
        } catch (Exception e1) {
            e1.printStackTrace();
        }
    }
}

I have a simple consumer program which is not able to reconnect. I followed this post but it is not helping also RabbitMQ Java client auto reconnect. Can anyone help me on this ?

Upvotes: 3

Views: 1627

Answers (1)

Sthita
Sthita

Reputation: 1782

I got some information from rabbitmq-user mailing list. Hope this will help someone.

Connection recovery is in no way dependent on existence of queues. The algorithm used for recovery is documented on http://www.rabbitmq.com/api-guide.html.

What would be considered an event that triggers connection recovery?

  • Any exception in the I/O thread (this involves network connectivity problems).
  • Skipped peer heartbeats (http://rabbitmq.com/heartbeats.html)
  • Server-sent connection.close (which is used by the connection closure action in CLI and management UI)

As I mentioned earlier, the latter is a controversial topic because then you cannot force close a connection of a rogue app without the app giving up on recovery — which it typically never does.

What WILL NOT trigger connection recovery?

  • When you intentionally close it using Connection#close() or #abort().
  • A channel exception — channels are not recovered intentionally since their handling is entirely application-specific

The easiest way to test connection recovery is to shut down your node, wait for a period of time and start it back. You can also use iptables and such but then you'll have to wait for missed heartbeats — see that guide for more details.

It should be trivial to see reconnection attempts in Wireshark (https://www.rabbitmq.com/amqp-wireshark.html), server log or — since you use the 4.0 version of the client — there should be at least something in the client log. There are also connection recovery lifecycle listeners: https://github.com/rabbitmq/rabbitmq-java-client/pull/144

I'm highly confident that there are no easy to hit recovery issues in 3.6.6 and 4.0.0 clients. We have fixed a few known issues in 3.6.6, confirmed the fixes with their reporters and haven't seen new issue reported in maybe a couple of months.

Upvotes: 1

Related Questions