简体   繁体   中英

Check if a durable queue in rabbitMQ has data in it?

I have some durable queues running in the broker. At some point, I want to check if the queue has got any data in it and close or kill that queue if it is empty.

I am using java to code the senders and receivers. I know which all queues are present in the broker.

Suggest your way of doing this.

You can check the GetResponse object value for getting the info that whether the queue has something in it or not. If GetResponse is null, you can delete the queue, considering the queue as empty.

ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");
        Connection con = factory.newConnection();
        rabbitChannel = con.createChannel();
GetResponse response = rabbitChannel.basicGet(QUEUE_NAME, BOOLEAN_NOACK);
if(response != null){
String body = new String(responseQuestion.getBody());
// do whatever you want to do here
}
else{
rabbitChannel.queueDelete(QUEUE_NAME);
}

OR

Use this queueDelete(java.lang.String queue, boolean ifUnused, boolean ifEmpty)

This will automatically check whether if queue is empty and not_in_use based on boolean you provided in arguments & delete it accordingly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM