[英]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
. 您必须使用该
routingKey
将queue
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 Message
的MessageProperties
具有属性getReceivedExchange()
和getReceivedRoutingKey()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.