Queue Size in Spring AMQP Java client

hellojava picture hellojava · Jul 12, 2012 · Viewed 9.8k times · Source

I am using Spring amqp 1.1 version as my java client. I have a queue which has around 2000 messages. I want to have a service which checks this queue size and and if it is empty it will send out a message saying " All items processed".

I dont know how to get current queue size ? Please help

I googled and found a class "RabbitBrokerAdmin" that was present in earlier version 1.0. I think it is not present in 1.1 now.

Any pointers in getting current queue size?

Answer

Jesse picture Jesse · Mar 3, 2014

So I know this is a little late and a solution has already been found but here is another way to look message counts in your queues

This solution assumes that you are using the spring rabbitmq framework and have defined your queues in your application config with the following tags defined

<rabbit:queue>
<rabbit:admin>

The java class:

public class QueueStatsProcessor {
    @Autowired
    private RabbitAdmin admin;
    @Autowired
    private List<Queue> rabbitQueues;

    public void getCounts(){
        Properties props;
        Integer messageCount;
        for(Queue queue : rabbitQueues){
            props = admin.getQueueProperties(queue.getName());
            messageCount = Integer.parseInt(props.get("QUEUE_MESSAGE_COUNT").toString());
            System.out.println(queue.getName() + " has " + messageCount + " messages");
        }
    }
}

You can also use this solution to read the current consumers attached to the queue http://docs.spring.io/spring-amqp/docs/1.2.1.RELEASE/api/org/springframework/amqp/rabbit/core/RabbitAdmin.html#getQueueProperties(java.lang.String)