Queue Size in Spring AMQP Java client

后端 未结 2 1108
醉梦人生
醉梦人生 2021-02-14 10:13

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 empt

2条回答
  •  伪装坚强ぢ
    2021-02-14 10:44

    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

    
    
    

    The java class:

    public class QueueStatsProcessor {
        @Autowired
        private RabbitAdmin admin;
        @Autowired
        private List 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)

提交回复
热议问题