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

2023-02-20Java开发问题
1

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

The End

相关推荐

如何使用 JAVA 向 COM PORT 发送数据?
How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)...
2024-08-25 Java开发问题
21

如何使报表页面方向更改为“rtl"?
How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)...
2024-08-25 Java开发问题
19

在 Eclipse 项目中使用西里尔文 .properties 文件
Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)...
2024-08-25 Java开发问题
18

有没有办法在 Java 中检测 RTL 语言?
Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)...
2024-08-25 Java开发问题
11

如何在 Java 中从 DB 加载资源包消息?
How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)...
2024-08-25 Java开发问题
13

如何更改 Java 中的默认语言环境设置以使其保持一致?
How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)...
2024-08-25 Java开发问题
13