Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reconnect to the closed channel. #230

Open
terrasoff opened this issue Feb 21, 2022 · 4 comments
Open

Reconnect to the closed channel. #230

terrasoff opened this issue Feb 21, 2022 · 4 comments

Comments

@terrasoff
Copy link

terrasoff commented Feb 21, 2022

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?

@elizatlawy
Copy link

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.

@jwalton
Copy link
Owner

jwalton commented Aug 14, 2022

@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.

@jwalton
Copy link
Owner

jwalton commented Aug 14, 2022

Oh, but there should be connect/close events for the channel too...

@terrasoff
Copy link
Author

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.");
  });
})()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants