Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Queue Size in Spring AMQP Java client

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?

like image 257
hellojava Avatar asked Jul 12 '12 06:07

hellojava


2 Answers

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)

like image 56
Jesse Avatar answered Nov 08 '22 18:11

Jesse


You can use the RabbitAdmin instance to get the details from the queue, as follows:

@Resource RabbitAdmin admin;
...
protected int getQueueCount(final String name) {
    DeclareOk declareOk = admin.getRabbitTemplate().execute(new ChannelCallback<DeclareOk>() {
        public DeclareOk doInRabbit(Channel channel) throws Exception {
            return channel.queueDeclarePassive(name);
        }
    });
    return declareOk.getMessageCount();
}
like image 26
Dominic Messenger Avatar answered Nov 08 '22 17:11

Dominic Messenger