RabbitMQ: How to specify the queue to publish to?(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.
请看教程 - http://www.rabbitmq.com/tutorials/tutorial-three-java.html
RabbitMQ 中消息传递模型的核心思想是生产者永远不会将任何消息直接发送到队列.实际上,生产者通常根本不知道消息是否会被传递到任何队列.相反,生产者只能向交换器发送消息
这篇关于RabbitMQ:如何指定要发布到的队列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:RabbitMQ:如何指定要发布到的队列?
基础教程推荐
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01