仅使用RabbitMQ和SpringAMQP消费具有某些标头的消息 [英] Only consuming messages with certain headers using RabbitMQ and SpringAMQP
问题描述
我正尝试将消息发布到队列中,然后让某些使用者仅在包含某个标头的情况下使用它,而另一个使用者在包含另一个标头的情况下使用它.
I'm trying to publish a message to a queue and then have certain consumers consume it only if it contains a certain header and another consumer consume it if it contains another header.
到目前为止,我所做的是设置一个headers-exchange,仅当邮件包含该标头时才将消息路由到特定队列.
What I've done so far is to setup a headers-exchange that routes messages to a certain queue only if it contains that header.
这是我用来设置交换,队列和侦听器的配置:
This is the config I'm using to setup the exchange and the queue and the listener:
<!-- Register Queue Listener Beans -->
<bean id="ActionMessageListener" class="com.mycee.Action" />
<!-- Register RabbitMQ Connections -->
<rabbit:connection-factory
id="connectionFactory"
port="${rabbit.port}"
virtual-host="${rabbit.virtual}"
host="${rabbit.host}"
username="${rabbit.username}"
password="${rabbit.password}"
connection-factory="nativeConnectionFactory" />
<!-- Register RabbitMQ Listeners -->
<rabbit:listener-container
connection-factory="connectionFactory"
channel-transacted="true"
requeue-rejected="true"
concurrency="${rabbit.consumers}">
<rabbit:listener queues="${queue.myqueue}" ref="ActionMessageListener" method="handle"/>
</rabbit:listener-container>
<!-- Setup RabbitMQ headers exchange -->
<rabbit:headers-exchange id="${exchange.myexchange}" name="${exchange.myexchange}">
<rabbit:bindings>
<rabbit:binding queue="${queue.myqueue}" key="action" value="action3" />
</rabbit:bindings>
</rabbit:headers-exchange>
<rabbit:admin connection-factory="connectionFactory"/>
<rabbit:queue name="${queue.myqueue}" />
因此,我使用操作键和action3的值将myqueue绑定到myexchange.
So I'm binding myqueue to myexchange using key of action and value of action3.
现在,当我在交易所发布时:
Now when I publish on the exchange:
即使操作设置为action1而不是action3,ChannelAwareMessageListener仍在使用它
the ChannelAwareMessageListener is consuming it even though the action was set to action1 instead of action3
public class Action implements ChannelAwareMessageListener {
@Override
public void onMessage(Message message, Channel channel) throws Exception {
System.out.println(message.toString());
}
}
我没有正确使用headers-exchange或我没有正确配置它-有什么建议吗?
Either I'm not using a headers-exchange correctly or I'm not configuring it correctly - any advice ?
推荐答案
这种方式行不通;您需要为每个使用者分配一个单独的队列.参见教程.
It doesn't work that way; you need a separate queue for each consumer. See the tutorial.
当多个使用者从同一个队列中消费时,他们将争夺所有消息;您不能在消费者方选择消息;通过交换将消息路由到特定队列来完成选择".
When multiple consumers consume from the same queue they compete for all messages; you can't select messages on the consumer side; the "selection" is done by the exchange by routing messages to specific queue(s).
这篇关于仅使用RabbitMQ和SpringAMQP消费具有某些标头的消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!