rabbitListener 在队列中没有消息时执行任务

rabbitListener execute task when no message in the queue

我们正在使用@RabbitListener 来监听队列,当有消息时我们会处理它们。但是我想在队列为空时执行一些报告任务,当我们的应用程序刚刚处理队列中的大量消息并且有一段时间没有更多消息时,就会发生这种情况。那是我要报告的时间。我如何使用@RabbitListener 做到这一点?

这是我的代码:

@RabbitListener(queues = "${consumer.queue}", containerFactory = "ListenerContainerFactory")
    public void handleMessage(Message message) {
        processEvent(message);
    }

因为我 AMQP(因此 Spring AMQP)没有机制来通知消费者队列中当前没有消息。

我们可以修改容器以在处理消息后未找到消息时触发一次 ApplicationEvent,但这需要修改框架。您的侦听器必须实施 ApplicationListener 才能获得此类事件。

如果需要,请随时打开 New Feature JIRA Issue,我们也许可以看一看。