Application Scenario
The network fails in many cases, sometimes in subtle ways (e.g. high packet loss). It takes a moderate amount of time for the operating system to detect the TCP disconnection (the default duration in Linux is 11 minutes). AMQP 0-9-1 provides the heartbeat detection feature to ensure that the application layer promptly detects interrupted connections (or connections that are not working at all). Heartbeat detection also protects the connection from being terminated after a period of inactivity.
Heartbeat Timeout
The heartbeat timeout value determines the maximum time for TCPs to connect with each other. Beyond this time, the connection will be regarded as unreachable by RabbitMQ and the client. This value is negotiated when the RabbitMQ server and client are connected. The client needs to configure the request heartbeat detection.
Heartbeat Frame
Heartbeat frames are sent at timeout/2 intervals. After two consecutive heartbeats fail, the connection will be regarded as unreachable. Different clients behave differently, but the TCP connection will be closed. When the client detects that the RabbitMQ service node is unreachable, it needs to reinitiate the connection.
Any connection data exchange (e.g. protocol operation, publishing message, message acknowledgment) counts as a valid heartbeat. The client may also send heartbeat packets where there are other data exchanges in the connection, but some only send heartbeat packets when needed.
Setting the heartbeat timeout on the client
Setting heartbeat time in the Java client
ConnectionFactory cf = new ConnectionFactory();
cf.setRequestedHeartbeat(30);