RabbitMQ:如何指定要发布到的队列?

编程入门 行业动态 更新时间:2024-10-24 14:19:59
本文介绍了RabbitMQ:如何指定要发布到的队列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

RabbitMQ 的 Channel#basicConsume 方法为我们提供了以下参数:

RabbitMQ's Channel#basicConsume method gives us the following arguments:

channel.basicConsume(queueName, autoAck, consumerTag, noLocal, exclusive, arguments, callback);

让我们能够准确地告诉 RabbitMQ 我们想从哪个队列消费.

Giving us the ability to tell RabbitMQ exactly which queue we want to consume from.

但是 Channel#basicPublish 没有这样的等价性:

But Channel#basicPublish has no such equivalency:

channel.basicPublish(exchangeName, routingKey, mandatory, immediateFlag, basicProperties, messageAsBytes);

为什么我不能在这里指定要发布到的队列?!?如何让 Channel 发布到名为 logging 的队列?提前致谢!

Why can't I specify the queue to publish to here?!? How do I get a Channel publishing to, say, a queue named logging? Thanks in advance!

推荐答案

队列基本上可以基于routingKeys绑定到一个exchange.

Basically queues can be binded to an exchange based on routingKeys.

假设您有 3 个不同的发布商.Publisher1发送消息与routingKey事件"进行交换Publisher2 发送消息与 routingKey tasks"进行交换Publisher3 发送消息与 routingKey jobs"进行交换

Assume that you have 3 different publishers. Publisher1 sending message to exchange with routingKey "events" Publisher2 sending message to exchange with routingKey "tasks" Publisher3 sending message to exchange with routingKey "jobs"

您可以有一个只使用具有特定 routhingKey 的消息的消费者.例如,为了让您声明这样的事件"消息的消费者

You can have a consumer that consumes only messages with specific routhingKey. For example in order to have a consumer for "events" messages you declare like this

channel.queueBind(queueName, exchangeName, "events");

如果你想消耗所有进入交换的消息,你将路由指定为'#'

If you want to consume all the messages coming to the exchange you give the routing as '#'

总之我能说的是,1. 消息将发布到交易所.2. 队列会根据routingKeys绑定交换.3. RabbitMQ 会将路由键匹配的消息转发到对应的队列中.

So in short what i can say is, 1. Messages will be published to an exchange. 2. Queues will be bound to exchange based on routingKeys. 3. RabbitMQ will forward messages with matching routing keys to the corresponding queues.

请看教程 - www.rabbitmq/tutorials/tutorial-three-java.html

RabbitMQ 中消息传递模型的核心思想是生产者永远不会将任何消息直接发送到队列.实际上,生产者通常根本不知道消息是否会被传递到任何队列.相反,生产者只能向交换器发送消息

更多推荐

RabbitMQ:如何指定要发布到的队列?

本文发布于:2023-11-05 09:28:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1560467.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:队列   RabbitMQ

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!