A client may disconnect from the server due to server upgrade, server restart, or network jitters. This topic describes how to enable automatic recovery of connections and topology on the client to prevent the impact of network disconnection on your business.

Triggers

The following events can trigger automatic recovery of connections and topology:

  • An I/O exception is thrown.
  • A socket read operation times out.
  • Missing server heartbeats are detected.

Enable automatic recovery of connections and topology

Notice In Java client 4.0.0 and later versions, automatic recovery of connections and topology is enabled by default. You do not need to configure automatic recovery parameters in code.

You can configure the following parameters in code to enable automatic recovery of connections and topology. Topology recovery includes actions on queues, exchanges, bindings, and consumers.

  • factory.setAutomaticRecoveryEnabled(boolean): specifies whether to enable automatic connection recovery.
  • factory.setNetworkRecoveryInterval(long): specifies the retry interval. If the connection fails to be recovered, the client tries again after the specified interval. The default interval is 5 seconds.
  • factory.setTopologyRecoveryEnabled(boolean): specifies whether to enable automatic topology recovery. Topology recovery includes actions on queues, exchanges, bindings, and consumers.

Sample code

You can run the following code to enable automatic recovery of connections and topology:

ConnectionFactory factory = new ConnectionFactory();
// Specify an endpoint. You can obtain the endpoint on the Instances page of the Message Queue for RabbitMQ console.
factory.setHost("xxx.xxx.aliyuncs.com");
// Set ${instanceId} to the ID of your instance. You can obtain the instance ID on the Instances page of the Message Queue for RabbitMQ console.
// Set ${AccessKey} to your AccessKey ID. You can create an AccessKey pair in the Alibaba Cloud Management Console.
// Set ${SecretKey} to your AccessKey secret. You can create an AccessKey pair in the Alibaba Cloud Management Console.
factory.setCredentialsProvider(new AliyunCredentialsProvider("${AccessKey}", "${SecretKey}", "${instanceId}"));
// Specify a vhost name. Make sure that the vhost has been created in the Message Queue for RabbitMQ console.
factory.setVirtualHost("${VhostName}");
// Specify a default port. Use port 5672 for a non-encrypted connection and port 5671 for an encrypted connection.
factory.setPort(5672);
// Specify an appropriate timeout period based on the network environment.
factory.setConnectionTimeout(30 * 1000);
factory.setHandshakeTimeout(30 * 1000);
factory.setShutdownTimeout(0);
// Enable automatic connection recovery.
factory.setAutomaticRecoveryEnabled(true);
// Set the connection retry interval to 10 seconds.
factory.setNetworkRecoveryInterval(10000);
// Enable automatic topology recovery.
factory.setTopologyRecoveryEnabled(true);
Connection connection = factory.newConnection();                      

Limits

Automatic connection recovery has the following limits:

  • The detection of connection failures takes a period of time. You can use Publisher Confirms to make sure that messages sent during this period are not lost.
  • Channel exceptions cannot trigger automatic connection recovery. In most cases, channel exceptions are application-level issues and need to be handled by application owners.
  • Automatic connection recovery does not cause channels to automatically recover.