Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Close RabbitMQ connection even if application does not end gracefully

Tags:

c#

rabbitmq

I use RabbitMQ.Client (runtime version v2.0.50727, version 2.8.1.0) with C# .NET application.

I create connection like this:

using (IConnection connection = _factory.CreateConnection())
{
    using (IModel channel = connection.CreateModel())
    {
        // code...
    }
}

If I close the application properly, it simply closes the channel and connection. But if application is closed incorrectly (e.g. pressing restart button on PC), it is not. I would like to ensure that all unnecessary connections (from previous sessions or even other applications, if by mistake other instance of the application is running somewhere else) are closed before I start my application.

I know I may use heart beats but it is possible that my application requires really much time to start doing anything (many hours of opened connection and not being used). So I think heartbeat is not the best thing.

How can I close all opened connections for RMQ? (Or, even better, all opened connections, with the exception for one given IP)?

Regards!

like image 218
user1809566 Avatar asked Nov 08 '12 14:11

user1809566


People also ask

Should I close RabbitMQ connection?

In general, it's not a good practice to open and close connections and channels per message. Connections are long lived and it takes resources to keep opening and closing them.

How do I close a RabbitMQ connection?

A connection can be closed via the RabbitMQ Management Interface. Enter the connection tab and press on the connection. Go to the bottom of the page and press Close this connection, followed by pressing Force Close.

What happens when RabbitMQ goes down?

In detail: The messages that RabbitMQ publish are persistent. When the message broker goes down, the events are not published and I am planning to store these events in a database and publish them again to RabbitMQ when it is up.


2 Answers

I encountered the same problem as you. I tried a lot of methods to close those connections.

Those methods, some are works some are only part works. Maybe you can have a try.

  1. Use rabbitmqctl command to delete the queues then use RMQ's management website close the connections. It means current opened queues will expire after 1 second(1000 miliseconds). This way all queues are deleted. Then you can close connections.

    Command:

    rabbitmqctl set_policy expiry ".*" '{"expires":1000}' --apply-to queues

    Queue Time-To-Live policy--> https://www.rabbitmq.com/ttl.html

    This method works partly.

  2. After searching, got the idea that reason might be:

    Your producer waiting the return ACK back, it won't be destroyed untill you send ACK to acknowledge the job has finished.

  3. Abrupt way. Works. RMQ save connections information in its DB menisa.

    Step1: Stop rabbitmq-server first, and kill rabbitmq thread by 'ps aux | grep rabbitmq' to find its pid then kill all rabbitmq thread.

    Find RMQ DB mensia location (1). $> cd /var/lib/rabbitmq/

    Step2: rename mensia file to another name to "delete" (2). $> mv mensia mensia-bak

    Step3: restart rabbitmq-server

    The third methods works for me now. It can close all open connections.

like image 197
user2206782 Avatar answered Nov 07 '22 10:11

user2206782


Sounds like Heartbeats is your best solution. I added heartbeats to my RabbitMQ apps for the same reason (ungraceful shutdowns). It shouldn't matter that there is a long gap of time between messages, heartbeat will just verify that the connection is still open on both sides.

like image 24
timothymcgrath Avatar answered Nov 07 '22 09:11

timothymcgrath