Dynamically selecting a RabbitMq queue in Exchange

2019-08-31 06:00发布

问题:

From producer I have to send message to an RabbitMQ Exchange. this message will contain specific attribute, for example , queue name, based on this attribute, I have to dynamically decide the queue to send this message.[queue to bind from exchange, to send this particular message].

is there any way to intercept the message arriving to a RabbitMQ Exchange, using spring integration, At present , I have the following spring integration config file.

I don't know to how to create a bean to get Exchange Messages and route the message to smsQueue, emailQueue etc., queues.

thanks for you suggestions and replies.

http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit-1.0.xsd

http://www.springframework.org/schema/integration         
http://www.springframework.org/schema/integration/spring-integration.xsd      
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/integration/amqp
http://www.springframework.org/schema/integration/amqp/spring-integration-amqp.xsd
">

<context:annotation-config></context:annotation-config>
<context:component-scan base-package="com.rabbit"></context:component-scan>

<rabbit:connection-factory id="connectionFactory"
    host="localhost" username="guest" password="guest" />   
<rabbit:admin connection-factory="connectionFactory" />
<rabbit:template id="exchnageTemplate"
    connection-factory="connectionFactory" exchange="COMMUNICATION-EXCHANGE" />

<rabbit:queue id="smsQueue" auto-delete="true" durable="false" />
<rabbit:queue id="emailQueue" auto-delete="true" durable="false" />
<rabbit:queue id="dvbQueue" auto-delete="true" durable="false" />
<rabbit:queue id="pbxQueue" auto-delete="true" durable="false" />
<rabbit:queue id="medsensorQueue" auto-delete="true"
    durable="false" />


<int:gateway id="gateway" service-interface="com.rabbit.mq.ProducerGatewayInterface"
    default-request-channel="producerChannel" />

<int:channel id="producerChannel" />
<int:channel id="errorChannel" />

<bean id="communicationInterface" class="com.rabbit.mq.CommunicationInterface" />

<amqp:outbound-channel-adapter channel="producerChannel"
    amqp-template="exchnageTemplate" exchange-name="COMMUNICATION-EXCHANGE">
    <int:service-activator input-channel="input"
        ref="communicationInterface" method="optimalRoutingOfMessage" />
</amqp:outbound-channel-adapter>

回答1:

With RabbitMQ (AMQP) you don't send to queues, you send to exchanges with a routing key, and bindings determine which queue(s) get the message.

<rabbit:direct-exchange name="si.test.exchange">
    <rabbit:bindings>
        <rabbit:binding queue="si_test_queue" key="si.test.binding" />
    </rabbit:bindings>
</rabbit:direct-exchange>

<int-amqp:outbound-channel-adapter
    channel="toRabbit" amqp-template="amqpTemplate" exchange-name="si.test.exchange"
    routing-key="si.test.binding" />

Instead of routing-key you can use routing-key-expression with something like headers['foo'] or @someBean.determineRoutingKeyFor(payload).