【发布时间】:2017-02-25 17:28:22
【问题描述】:
我需要从名为 Metadata 的队列中侦听消息 - 然后基于该消息,我将不得不读取一些队列,我们将其称为 dataQ(该队列的名称将在元数据消息中)。要读取元数据,我可以使用兔子侦听器,但之后我必须从 dataQ 读取其他消息,因此我可以通过一种方式进行手动拉取 - 但我希望拥有更清洁的东西,比如兔子侦听器,这样我就不必管理频道,确认等等。但是由于直到我们从元数据队列中读取消息后才知道队列名称,因此尝试探索其他解决方案。这个 dataQ 可以是 1000 个不同的队列名称,所以我们必须动态监听这个 dataQ。
ack 也应该像这样工作 - 从元数据队列中读取消息,处理给定的 dataQ - 为 dataQ 中的消息发送 ack(dataQ 可能有超过 1 条消息)并为元数据队列发送 ack。
(如果这对单个消费者有效,那么我可以添加容器模型并处理来自元数据队列的多个消息,这意味着我将能够同时处理多个数据队列。)
按照建议进行更新,对如何在主侦听器中获取事件感到困惑,以及该标志如何与并发一起工作(抱歉,到目前为止尚未广泛使用应用程序事件)
package com.example;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.EventListener;
@Configuration
public class MyListener {
@Autowired
ConnectionFactory connectionFactory;
@Autowired
private ApplicationEventPublisher applicationEventPublisher;
@RabbitListener(queues = "Metadata")
public void messageProcessing(String c) {
System.out.println(c);
SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
container.setQueueNames(c);
container.setMessageListener(new MessageListenerAdapter(new DataHandler()));
container.setApplicationEventPublisher(applicationEventPublisher);
container.setIdleEventInterval(5000);
container.start();
// how to get container idle event here
// so we can call container.stop();
}
public class DataHandler {
public void handleMessage(byte[] text) {
System.out.println("Data Received: " + text);
}
}
@EventListener
public void onApplicationEvent(ApplicationEvent event) {
//I am getting idle event here
System.out.println(event.getSource());
}
}
【问题讨论】:
标签: rabbitmq spring-amqp