简体   繁体   English

在春季启动时使用路由键消费DirectExchange消息并进行交换

[英]Consume DirectExchange messages using routing key and exchange in spring boot

Am trying to consume the message from exiting queue which is of type Direct Exchange (created with the help of exchange and routing key). 我正在尝试从直接交换类型(借助交换和路由密钥创建)的退出队列中使用消息。 I have only the exchange name and routing key and not the queue name . 我只有交换名称路由密钥,没有队列名称 There were support for plain Java, but there was no place where I can find it for Spring boot. 有对纯Java的支持,但是没有地方可以在Spring引导中找到它。

@RabbitListener
    @RabbitHandler
    public void consumeMessage(Object message) {
        LOGGER.debug("Message Consumed.... : {}", message.toString());
    }

How can I consume messages with routing key and exchange name not the queue name as @RabbitListener asks for queue . 我如何使用带有路由键和交换名而不是队列名的消息来使用消息,而不是@RabbitListener要求queue

Consumers consume from queues not exchanges. 消费者从队列而不是交换中消费。 You must bind a queue to the exchange with the routing key. 您必须使用路由密钥将队列绑定到交换机。

EDIT 编辑

There are several ways to automatically declare a queue on the broker. 有几种方法可以在代理上自动声明队列。

    @RabbitListener(bindings = 
        @QueueBinding(exchange = @Exchange("myExchange"), 
            key = "myRk", value = @Queue("")))
    public void listen(String in) {
        System.out.println(in);
    }

This will bind an anonymous queue (auto-delete) which will be deleted when the application is stopped. 这将绑定一个匿名队列(自动删除),该匿名队列在应用程序停止时将被删除。

    @RabbitListener(bindings = 
        @QueueBinding(exchange = @Exchange("myExchange"), 
            key = "myRk", value = @Queue("foo")))
    public void listen(String in) {
        System.out.println(in);
    }

Will bind a permanent queue foo to the exchange with the routing key. 将使用路由键将永久队列foo绑定到交换。

You can also simply declare #Bean s for the queue, exchange and binding. 您也可以简单地声明#Bean来进行队列,交换和绑定。

See Configuring the broker . 请参阅配置代理

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Spring Boot通过扇出交换在RabbitMQ上发布消息 - How to publish messages on RabbitMQ with fanout exchange using Spring Boot 在RabbitMQ中,如何使用特定的密钥消费多条消息或读取队列中的所有消息或交换的所有消息? - In RabbitMQ how to consume multiple message or read all messages in a queue or all messages in exchange using specific key? 使用Spring RabbitMessagingTemplate发布消息时验证交换,路由密钥 - Validate exchange, routing key when publishing message using Spring RabbitMessagingTemplate Spring Boot + RabbitMQ 消息在延迟交换时丢失 - Spring Boot + RabbitMQ messages lost on delayed exchange Apache Beam:是否可以使用交换和路由密钥来消耗RabbitMQ的消息 - Apache Beam : is it possible to comsume messages of RabbitMQ with exchange and routing key Spring 云 stream RabbitMQ - 使用一个路由密钥将 DLQ 与交换绑定 - Spring cloud stream RabbitMQ - bind DLQ with an exchange using one routing key 消耗来自交换的多个动态键的消息 - Consume messages from multiple dynamic keys of an exchange Nodejs:在主题交换中使用 rabbitmq 消息 - Nodejs: Consume rabbitmq messages in topic exchange 使用 pika 时消息队列中的 exchange 和 routing_key 是什么? - What is exchange and routing_key in a message queue while using pika? Spring 启动应用程序有多个实例,但只有一个实例应该使用来自 RabbitMQ 的消息 - Spring boot application has multiple instances but only one instance should consume messages from RabbitMQ
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM