转载:Springboot得到RabbitMQ队列消息的数量_坐在西半球上的博客-CSDN博客
import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.amqp.rabbit.core.ChannelCallback; import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import com.rabbitmq.client.AMQP; import com.rabbitmq.client.Channel; @RunWith(SpringRunner.class) @SpringBootTest public class SpringbootTest { @Autowired RabbitTemplate rabbitTemplate; /** * 获取对应队列的数量; * * @param queue * @return */ @Test public void getMessageCount() { String queue = "direct.screenshot.queue"; AMQP.Queue.DeclareOk declareOk = rabbitTemplate.execute(new ChannelCallback<AMQP.Queue.DeclareOk>() { public AMQP.Queue.DeclareOk doInRabbit(Channel channel) throws Exception { return channel.queueDeclarePassive(queue); } }); System.out.println("-------------"); System.out.println("-------------"); System.out.println("-------------"); System.out.println("-------------"); System.out.println(declareOk.getMessageCount()); } }