仅使用 RabbitMQ 和 SpringAMQP 使用具有某些标头的消息

Only consuming messages with certain headers using RabbitMQ and SpringAMQP(仅使用 RabbitMQ 和 SpringAMQP 使用具有某些标头的消息)
本文介绍了仅使用 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 使用具有某些标头的消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

How to implement RecyclerView with section header depending on category?(如何根据类别实现带有节标题的 RecyclerView?)
How to generate JNI header file in Eclipse(如何在 Eclipse 中生成 JNI 头文件)
Setting a custom HTTP header dynamically with Spring-WS client(使用 Spring-WS 客户端动态设置自定义 HTTP 标头)
Could you technically call the string[] anything in the main method?(从技术上讲,您可以在 main 方法中调用 string[] 吗?)
What is the proper way of setting headers in a URLConnection?(在 URLConnection 中设置标头的正确方法是什么?)
How to overwrite http-header quot;Hostquot; in a HttpURLConnection?(如何覆盖 http-header “主机在 HttpURLConnection 中?)