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

Important 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();
// The endpoint. You can view the endpoint of an instance on the Instance Details page in the ApsaraMQ for RabbitMQ console. 
factory.setHost("xxx.xxx.aliyuncs.com");
// Replace ${instanceId} with the ID of the ApsaraMQ for RabbitMQ instance. You can view the instance ID on the Instances page in the ApsaraMQ for RabbitMQ console. 
factory.setCredentialsProvider(new AliyunCredentialsProvider("${instanceId}"));
// The vhost name. Make sure that the vhost was created in the ApsaraMQ for RabbitMQ console. 
factory.setVirtualHost("${VhostName}");
// The default port. Use port 5672 for non-encrypted connections and port 5671 for encrypted connections. 
factory.setPort(5672);
// The timeout period. Specify the value based on the network environment. 
factory.setConnectionTimeout(30 * 1000);
factory.setHandshakeTimeout(30 * 1000);
factory.setShutdownTimeout(0);
// Specifies whether to enable automatic connection recovery. 
factory.setAutomaticRecoveryEnabled(true);
// The retry interval. Specify the value to 10 seconds. 
factory.setNetworkRecoveryInterval(10000);
// Specifies whether to 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.