admin管理员组

文章数量:1356768

I am writing producer & consumer using rabbitMq node-amqplib library, I am afraid about suddenly to lost connection of server , How could I check whether the connection is alive or not ?

I am writing producer & consumer using rabbitMq node-amqplib library, I am afraid about suddenly to lost connection of server , How could I check whether the connection is alive or not ?

Share Improve this question asked Apr 30, 2018 at 10:10 mayurmayur 1171 gold badge1 silver badge7 bronze badges 1
  • Connect with RabbitMQ Web UI Server. You can achieve this with the plugin. This might help: rabbitmq./management.html There you can see bindings between producer & consumer. – Zhivko.Kostadinov Commented Apr 30, 2018 at 10:32
Add a ment  | 

2 Answers 2

Reset to default 4

AMQP 0-9-1 offers a heartbeat feature to ensure that the application layer promptly finds out about disrupted connections (and also pletely unresponsive peers).

In amqplib you only need to set a heartbeat timeout (non 0) when you call connect([url, [socketOptions]]) and the check will be performed automatically.

More info here:

https://www.squaremobius/amqp.node/channel_api.html#heartbeating

http://www.rabbitmq./heartbeats.html

There are some options in doing this:

One way is using the heartbeat and an event listener, something like conn.on('close', (err) => { this.connected = false; } )

Or you can get into the connection object. There is a risk here as upgrades to amqplib may break this, as it's not part of the official interface: const connClosed = conn.connection['expectSocketClose']

There are other properties inside the connection object that also can tell you if it's closed, like stream writeable state (could be a false flag) or the heartbeater object.

本文标签: