node-amqp-connection-manager
node-amqp-connection-manager copied to clipboard
Reconnect to the closed channel.
Reconnection to the broker works fine! Do you have the closed channels detection and reconnection? How could I test it?
I tried the next code. No reconnection was found:
const connection = await amqp.connect([connectionString]);
const channel = connection.createChannel({
setup: function (channel) {
// Emulate channel closing (timeout, error, whatever).
// It will close the channel and no reconnection will be performed.
// Probably I should use different approach to close the channel.
setTimeout(channel.close, 5000);
return channel;
},
});
channel.on('close', () => {
// event will be triggered in 5 seconds
console.log("closed");
})
channel.on('connect', () => {
// no event will be triggered after the channel closing
console.log("connected");
})
What is your recommendation related to the channel reconnection?
Did you manage to test it? in my case also after I'm using connection.close();
to test the reconnection I do not see any reconnection attempts.
@terrasoff Try watching for connect
and closed
events from amqp
instead of from the channel.
@elizatlawy Calling connection.close()
will close the connection - no further reconnect attempts will be made, since you asked for the channel to be closed. The way I usually test this is to spin up a broker in Docker, and then stop and restart the broker.
Oh, but there should be connect/close events for the channel too...
Try watching for connect and closed events from amqp instead of from the channel.
@jwalton connect
or closed
events are not fired when a channel is closed.
Here is the complete example.
const amqp = require('amqp-connection-manager');
(async() => {
console.log("Try to connect.");
const connection = await amqp.connect(<rabbitmq host>);
const channel = connection.createChannel({
setup: function (channel) {
console.log("Set up channel to close channel in 5 seconds.");
setTimeout(async () => {
console.log("Close the channel in 5 seconds.");
await channel.close();
console.log("The channel is closed.");
// trying to manipulate with the channel you will get an error like the channel is closed
}, 5000);
return channel;
},
});
connection.on('connect', () => {
console.log("Connection is established.");
});
connection.on('close', () => {
console.log("Connection was closed.");
});
channel.on('close', () => {
console.log("Channel is closed.");
})
channel.on('connect', () => {
console.log("Channel is connected.");
});
})()