使用Spring集成在Exchange中动态选择RabbitMq队列 [英] Dynamically selecting a RabbitMq queue in Exchange using spring integration

查看:173
本文介绍了使用Spring集成在Exchange中动态选择RabbitMq队列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从生产者那里我必须将消息发送到RabbitMQ交易所.该消息将包含特定的属性,例如队列名称,基于此属性,我必须动态地确定队列以发送此消息.[从交换绑定到发送此特定消息的队列].

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

是否可以使用spring集成来拦截到达RabbitMQ Exchange的消息,目前,我有以下spring集成配置文件.

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.

我不知道如何创建一个bean来获取Exchange消息并将消息路由到smsQueue,emailQueue等队列.

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

感谢您的建议和答复.

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/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>

推荐答案

使用RabbitMQ(AMQP),您不发送到队列,使用路由键发送到交换,绑定决定哪个队列接收消息

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" />

您可以使用routing-key-expression代替routing-key,例如headers['foo']@someBean.determineRoutingKeyFor(payload).

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

这篇关于使用Spring集成在Exchange中动态选择RabbitMq队列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆