Spring AMQP和队列中的消息

时间:2017-09-04 07:01:27

标签: spring-amqp

在Spring AMQP项目中,我想在RabbitMQ中实时获取特定队列中的消息数(以便根据该消息数做出决策)(我无法使用管理插件)。 基本配置如下:

@Bean(name="managementServerHandler")
    public ManagementServerHandler managementServerHandler(){
        return new ManagementServerHandler();
    }
    @Bean 
    public MessageListenerAdapter broadcastManagementServerHandler() {
        return new MessageListenerAdapter(managementServerHandler(), "handleMessage");
    }
    @Bean(name="broadcastManagementMessageListenerContainer") 
    public SimpleMessageListenerContainer broadcastManagementMessageListenerContainer() 
    {
        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(_connectionFactory());
        container.setQueueNames( REQUEST_MANAGEMENT_QUEUE );
        container.setMessageListener(broadcastManagementServerHandler());
        container.setAcknowledgeMode(AcknowledgeMode.AUTO);
        container.setAutoDeclare(true);
        container.setAutoStartup(true);
        container.setConcurrentConsumers(1);
        container.setRabbitAdmin((RabbitAdmin)_amqpAdmin());
        container.setPrefetchCount(50);

        container.setDeclarationRetries(3);
        container.setMissingQueuesFatal(true);
        container.setFailedDeclarationRetryInterval(1000);
        container.setRecoveryInterval(400);

        return container;
    }

“ManagementServerHandler”只是:

 public class ManagementServerHandler implements ServletContextAware, MessageListener
    {
     @Override
        public void onMessage(Message msg) 
        {....}
}

我需要onMessage方法中的排队消息数,但我找不到这样做的方法。 我问了这个问题,但我不知道如何获得AMQP频道:

RabbitMQ and queue data

谢谢!

1 个答案:

答案 0 :(得分:2)

使用RabbitAdmin.getQueueProperties(queue)

/**
 * Returns 3 properties {@link #QUEUE_NAME}, {@link #QUEUE_MESSAGE_COUNT},
 * {@link #QUEUE_CONSUMER_COUNT}, or null if the queue doesn't exist.
 */
@Override
public Properties getQueueProperties(final String queueName) {