显然,我在我的应用程序中配置了一个队列来接收用户的帖子。下面是配置。
<!-- Creates a queue for consumers to retrieve messages -->
<rabbit:queue name="UserPostpublishQueue" durable="true">
</rabbit:queue>
<!-- queue for sending notifications to users -->
<rabbit:queue name="notificationQueue" durable="true"/>
<!-- Fanout exchange for a pubsub bound to UserPostpublishQueue -->
<fanout-exchange name="broadcastEvents" durable="true" xmlns="http://www.springframework.org/schema/rabbit">
<bindings>
<binding queue="UserPostpublishQueue"/>
</bindings>
</fanout-exchange>
<rabbit:template id="amqpTemplate" connection-factory="connectionFactory" retry-template="retryTemplate"
exchange="broadcastEvents" channel-transacted="true"/>
在代码中我刚刚 Autowiring 了 AMQP 模板
@Autowired
AmqpTemplate amqpTemplate;
amqpTemplate.convertAndSend(post);
现在我必须介绍另一个通知队列。我不知道该怎么做。我是否将队列与相同的扇出交换绑定(bind),这样做交换会将用户的帖子推送到通知队列中。
或者我是否创建另一个扇出交换,然后将通知队列与该交换绑定(bind),但如何使用 amqp 模板注册这个新交换?
或者我是否为用户发布和通知队列创建直接交换?
我不确定。任何帮助将不胜感激。
最佳答案
您应该从业务需求描述开始。
提到的所有绑定(bind)变体都是有效的。
您确实可以向扇出交换添加一个队列 - 并且相同的消息将被放置到所有队列中。主题行为。
您可以创建另一个交换并将该队列绑定(bind)到那里。在这种情况下,直接交换就足够了。要将消息准确发送到该队列(可能与 post 不同),您应该使用不同的
AmqpTemplate
方法:void convertAndSend(String exchange, String routingKey, Object message) throws AmqpException;
也许对你来说最好去RabbitMQ tutorials并研究所有可能的配置。
我们最近还将这些教程的 Spring AMQP 实现添加到 Spring AMQP Samples项目。
关于spring - 多个队列的 RabbitMQ 配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34162070/