I am using RabbitMQ for daily transactions. My consumers are .Net desktop applications deployed in multiple machines. Every day the transactions are pushed to queue within a certain time duration only. Beyond that there needs to be a hard stop on any new transaction. I have managed to stop sending new transaction to the queue. However, the existing transactions in the queue also needs to be flushed so that it is not sent to any consumer. I tried searching for this but did not get any solution for purging a queue except for two options-
Both of these approaches can be implemented but it requires significant amount of changes on my systems. I want to know if there is a better approach.
In the navigation pane, choose Queues. On the Queues page, choose the queue to delete. Choose Delete. In the Delete queue dialog box, confirm the deletion by entering delete .
Now go back to the Queues tab. Click on the queue with the 'Unacked' message. Expand the Purge section and hit the Purge Messages button.
An auto-delete queue will be deleted when its last consumer is cancelled (e.g. using the basic. cancel in AMQP 0-9-1) or gone (closed channel or connection, or lost TCP connection with the server). If a queue never had any consumers, for instance, when all consumption happens using the basic.
To reset a running and responsive node, first stop RabbitMQ on it using rabbitmqctl stop_app and then reset it using rabbitmqctl reset: # on rabbit1 rabbitmqctl stop_app # => Stopping node rabbit@rabbit1 ... done.
In C# you can purge a Queue like this
ConnectionFactory factory = new ConnectionFactory();
factory.HostName = "localhost";
factory.UserName = "guest";
factory.Password = "guest";
using (var connection = factory.CreateConnection())
{
using (var channel = connection.CreateModel())
{
channel.QueuePurge(queueName);
}
}
This blog article describes how to purge a queue in RabbitMQ in different ways.
rabbitmqadmin: The management plugin ships with a command line tool, rabbitmqadmin, which can perform the same actions as the web-based UI (the RabbitMQ management interface).
The script used to purge all messages in a single queue is:
$ rabbitmqadmin purge queue name=name_of_queue
HTTP API: The Rabbitmq Management plugin provides an HTTP-based API for management and monitoring of your RabbitMQ server.
curl -i -XDELETE https://USERNAME:PASSWORD@HOST/api/queues/vhost/QUEUE_NAME/contents
Policy: Add a policy that matches the queue names with an max-lenght rule. A policy can be added by entering the Management Interface and then pressing the admin tab. (Don't forget to delete the policy after it has been applied.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With