RabbitMQ _ How to Close a Channel

发布时间 2023-04-28 17:19:29作者: PanPan003

https://low-orbit.net/rabbitmq-how-to-close-a-channel

 

RabbitMQ How to Close a Channel

If you have found your way to this page you are probably wondering how to close a channel in RabbitMQ. Channels should be closed when they are no longer in use. There are multiple different ways of going about doing this that we are going to cover.

To close a RabbitMQ channel do one of the following:

  • Kill the corresponding consumer.
  • Close the channel from within your code: channel.close()
  • Close the associated connection from within your code: connection.close()
  • Use the CLI command: rabbitmqctl close_connection CONNECTION-NAME “my message”
  • Click on the button in the web GUI to close it.

Programmatically Close a RabbitMQ Channel

Option 1: Close Channel

A channel can be programmatically closed like this:

channel.close()

Option 2: Close Connection

All channels on a connection will be closed when you close that connection. You can close a connection programmatically like this:

connection.close()

Using the CLI

You can close a channel from the CLI by closing it’s connection.

To close a channel using the CLI you will first need to find the channel and it’s associated connection. Use the following command to list out channels and connections.

rabbitmqctl list_channels connection

You can close the channel’s connection using the following command. You need to specify the connection which we found previously. The last parameter is a message explaining why the connection is being closed. This will be included in the error message that is output by the consumer.

rabbitmqctl close_connection "<rabbit@zippy-zap.1608598465.5412.0
>" "go away"