简体   繁体   中英

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 Exchange s and different routingKey s. 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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM