当前位置: 动力学知识库 > 问答 > 编程问答 >

java - 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.

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.

Producer sends message to the Exchange via routingKey.

You must bind the queue to that Exchange with that routingKey.

Of course, you can have several bindings for the same queue with different Exchanges and different routingKeys. 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

I want is that the second constructor argument <constructor-arg ref="exampleMessageConverter" /> needs to be different for every routing key.

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()

分享给朋友:
您可能感兴趣的文章:
随机阅读: