Queue Size in Spring AMQP Java client

瘦欲@ 提交于 2019-12-04 19:09:26

问题


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?


回答1:


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)




回答2:


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();
}


来源:https://stackoverflow.com/questions/11446443/queue-size-in-spring-amqp-java-client

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!