简体   繁体   English

如何配置侦听器以处理Rabbitmq Spring中来自同一队列的多个交换的消息

[英]How to configure a listener to handle messages from multiple exchanges for the same queue in rabbitmq spring

I have one queue in my application which listens to multiple exchanges , with a different routing key for each exchange. 我的应用程序中有一个队列,它侦听多个交换,每个交换使用不同的路由密钥。

I have configured the listener to handle the message from one of those exchanges in the following manner: 我已将侦听器配置为以以下方式处理来自这些交换之一的消息:

<bean id="exampleMessageConverter"     class="org.springframework.amqp.support.converter.JsonMessageConverter">
    <property name="classMapper">
        <bean class="<packagename>.NamedClassMapper">
            <constructor-arg value="<packagename>.exampleDTO" />
        </bean>
    </property>
</bean>

<bean id="organizationUpdateEventListener" class="org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer">
    <property name="connectionFactory" ref="connectionFactory" />
    <property name="queueNames" value="${main.queue}" />
    <property name="messageListener">
        <bean class="org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter">
            <constructor-arg>
                <bean class="<packageName>.exampleMessageEventHandler" />
            </constructor-arg>
            <constructor-arg ref="exampleMessageConverter" />
        </bean>
    </property>
    <property name="adviceChain">
        <list>
            <ref bean="retryAdvice" />
        </list>
    </property>
</bean>

I want to configure the same listener to handle messages from the other exchanges, but the DTO sent from each of that exchange is different. 我想配置相同的侦听器来处理来自其他交换的消息,但是从每个交换发送的DTO是不同的。

EDIT: The second constructor argument needs to be different for every routing-key. 编辑:第二个构造函数参数需要为每个路由键都不同。 How can I add that configuration 如何添加该配置

How do I proceed? 我该如何进行?

Looks like you don't understand AMQP protocol a bit. 看来您不太了解AMQP协议。

Producer sends message to the Exchange via routingKey . 生产者通过routingKey将消息发送到Exchange

You must bind the queue to that Exchange with that routingKey . 您必须使用该routingKeyqueue bind到该Exchange

Of course, you can have several bindings for the same queue with different Exchange s and different routingKey s. 当然,对于具有不同Exchange和不同routingKey的同一queue ,您可以具有多个绑定。 But anyway it will be the same queue. 但是无论如何它将是同一队列。

So, your listener should be configured to receive messages only from that queue. 因此,应将您的侦听器配置为仅从该队列接收消息。 Because for him it doesn't matter how messages are appeared in the queue. 因为对他来说,消息在队列中的显示方式无关紧要。

Sorry, if I didn't understand your question properly... 抱歉,如果我不能正确理解您的问题...

UPDATE UPDATE

I want is that the second constructor argument <constructor-arg ref="exampleMessageConverter" /> needs to be different for every routing key. 我想要的是,每个路由键的第二个构造函数参数<constructor-arg ref="exampleMessageConverter" />都必须不同。

You can't do that from SimpleMessageListenerContainer , You can achieve the desired routing logic somewhere downstream in your target exampleMessageEventHandler , the MessageProperties of Spring AMQP Message has properties getReceivedExchange() and getReceivedRoutingKey() 您不能从SimpleMessageListenerContainer做到这一点,您可以在目标exampleMessageEventHandler下游某处实现所需的routing逻辑,Spring AMQP MessageMessageProperties具有属性getReceivedExchange()getReceivedRoutingKey()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在XML文件中配置RabbitMQ使其具有1)侦听来自多个队列的侦听器2)2个侦听器侦听同一队列? - How to configure RabbitMQ in XML file to have 1) listener listening from multiple queues 2) 2 listeners listening to same queue? Spring RabbitMQ - 是一种无需交换的队列配置 - Spring RabbitMQ - is a queue configuration with no exchanges possible 将多个 RabbitMQ 交换绑定到单个队列会在 Spring Cloud Stream 中引发错误 - Binding multiple RabbitMQ exchanges to single queue throws error in Spring Cloud Stream 如何使用java和spring 3.0从JMS主题(而不是队列)同时处理多个消息? - How can I handle multiple messages concurrently from a JMS topic (not queue) with java and spring 3.0? spring rabbitmq - 同时消费多条消息 - spring rabbitmq - consume multiple messages at the same time 来自一个Queue RabbitMQ Spring的多个监听器 - Multiple Listeners from one Queue RabbitMQ Spring 从RabbitMQ侦听器类使用Spring Messaging(Websockets)发送消息 - Sending messages with Spring Messaging (Websockets) from a RabbitMQ Listener class 从 Java/Spring 检索 RabbitMQ 队列中未确认消息的数量 - Retrieving number of unacknowledged messages in RabbitMQ queue from Java/ Spring RabbitMQ侦听来自多个服务器的队列中的消息 - RabbitMQ Listen messages from queue from multiple servers Spring RabbitMQ将新队列附加到现有侦听器 - Spring rabbitmq attaching new queue to existing listener
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM