将动态队列列表传递到队列侦听器

用户10776719

我有一个Spring Boot应用程序,该应用程序可旋转使用者队列的一部分,并且我希望能够在运行时向这些使用者添加队列。

我已经安装了事件交换插件(https://www.rabbitmq.com/event-exchange.html),并创建了绑定到amq.rabbitmq.event交换的专用队列。当我静态声明队列时,我可以看到事件的到来。

我将如何完成这种运行时魔术?我见过人们在使用属性文件,但是我宁愿不必在运行时修改属性文件,因为我添加了更多队列

@Component
public class MessageConsumer {

    List<String> allQueues = new ArrayList<String>();

    public MessageConsumer() {
        allQueues.add("queue1");
        allQueues.add("queue2");
        allQueues.add("queue3");
    }

    @RabbitListener(id = "event", queues = {"custom-emp-queue-events"}) // create this queue in rabbitmq management, bound to amqp exchange
    public void processQueueEvents(Message message) {
        ... add the queue to the allQueues list on queue.created ...
    }

    @RabbitListener(id = "process", queues = allQueues.stream().toArray(String[]::new) ) // this is where the "issue" is
    public void processMessageFromQueues(String messageAsJson) {
        ... process message ...
    }
}
阿尔特姆·比兰(Artem Bilan)

这可以通过SpEL表达式完成:

@RabbitListener(id = "process", queues = "#{messageConsumer.allQueues}" )

但是您必须为此添加一个公共获取器allQueues

在参考手册中查看更多信息:https : //docs.spring.io/spring-amqp/docs/2.1.3.RELEASE/reference/html/_reference.html#async-annotation-driven

更新

@Autowired
private RabbitListenerEndpointRegistry listenerEdnpointRegistry;

@RabbitListener(id = "event", queues = {"custom-emp-queue-events"}) // create this queue in rabbitmq management, bound to amqp exchange
public void processQueueEvents(Message message) {
    ((AbstractMessageListenerContainer) this.listenerEdnpointRegistry.getListenerContainer("process")).addQueueNames(...);
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章