RabbitMQ配置多个队列

时间:2015-12-08 17:18:58

标签: spring rabbitmq amqp spring-amqp spring-rabbit

显然我已经配置了一个队列来接收我的应用程序中的用户帖子。以下是配置。

<!-- 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"/>

在代码中,我只是自动安装了AMQP模板

@Autowired
    AmqpTemplate amqpTemplate;
    amqpTemplate.convertAndSend(post);

现在我必须引入另一个通知队列。我不知道该怎么做。我是否使用相同的扇出交换绑定队列,这样交换就会将用户的帖子推送到通知队列中。

或者我是否会创建另一个扇出交换&amp;然后将通知队列与该交换绑定,但是如何使用amqp模板注册这个新交换?

或者我为用户帖子和&amp;创建直接交换。通知队列?

我不确定。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:2)

您应该从业务需求说明开始。

提及的所有绑定变体均有效。

  • 您真的可以向扇出交换添加一个队列 - 同样的消息将被放置到所有队列。主题行为。

  • 您可以创建另一个交换并在那里绑定该队列。在这种情况下,直接交换就足够了。要将消息准确地发送到该队列(可能与post不同),您应该使用不同的AmqpTemplate方法:

    void convertAndSend(String exchange, String routingKey, Object message) throws AmqpException;
    

也许对你来说,最好去RabbitMQ tutorials并研究所有可能的配置。

我们最近还为这些教程添加了Spring AMQP实现到Spring AMQP Samples项目。

相关问题