仅使用 RabbitMQ 和 SpringAMQP 使用具有某些标头的消息 [英] Only consuming messages with certain headers using RabbitMQ and SpringAMQP

查看:30
本文介绍了仅使用 RabbitMQ 和 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.

到目前为止,我所做的是设置一个标头交换,仅当消息包含该标头时才将消息路由到某个队列.

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

所以我使用 action 的键和 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆