【问题标题】:Writing sample producer and consumer with spring integration and rabbit mq使用 spring 集成和 rabbit mq 编写示例生产者和消费者
【发布时间】:2014-02-12 16:36:42
【问题描述】:

我尝试使用 SpringIntegration 和 RabbitMQ 编写示例生产者和消费者。

这里是我的 rabbit.xml 配置文件:

<rabbit:connection-factory id="connectionFactory" />
<rabbit:template id="amqpTemplate" connection-factory="connectionFactory" />
<rabbit:admin connection-factory="connectionFactory" />
<rabbit:queue name="queue.request" />
<rabbit:queue name="queue.response" />
<rabbit:direct-exchange name="exchange.main">
    <rabbit:bindings>
        <rabbit:binding queue="queue.request" key="binding.queue.request"/>
        <rabbit:binding queue="queue.response" key="binding.queue.response" />
    </rabbit:bindings>
</rabbit:direct-exchange>

producer.xml 文件:

<context:annotation-config />
<import resource="classpath:META-INF/zcore/integration/rabbit.xml" />
<int:channel id="request" />
<int:channel id="response" />
<int-amqp:outbound-channel-adapter channel="request" amqp-template="amqpTemplate" 
    exchange-name="exchange.main" routing-key="binding.queue.request"       
<int-amqp:inbound-channel-adapter channel="response" queue-names="queue.response"
     connection-factory="connectionFactory" />  
<int:gateway id="baseGateway" service-interface="org.zcoreframework.integration.gateway.BaseGateway"
    default-request-channel="request" default-reply-channel="response" />   

consumer.xml 文件:

<import resource="classpath:META-INF/zcore/integration/rabbit.xml" />
<context:annotation-config />
<context:component-scan base-package="org.zcoreframework" />
<int:channel id="request" /> 
<int:channel id="response" />
<int-amqp:inbound-channel-adapter channel="request" queue-names="queue.request"
     connection-factory="connectionFactory" />  
<int-amqp:outbound-channel-adapter channel="response" amqp-template="amqpTemplate" 
    exchange-name="exchange.main" routing-key="binding.queue.response" />
<int:service-activator ref="messageConsumer" method="onMessage" input-channel="request"
    output-channel="response" />

我为发送消息编写了这段代码并得到了响应:

@Autowired
BaseGateway baseGateway;

@Test
public void testHelloWorld() {

        CallMethodMessage callMethodMessage = new CallMethodMessage();
        callMethodMessage.setMethod("test");
        callMethodMessage.setArgs(null);
        System.out.print("send & receive ");            
        ReturnModel returnModel = this.baseGateway.SendWait(callMethodMessage);         
        //this.baseGateway.FireForget(callMethodMessage);
}

这是我的界面

public interface BaseGateway {

    @Gateway
    public void FireForget(CallMethodMessage method);

    @Gateway
    public ReturnModel SendWait(CallMethodMessage method);

}

好的,现在我单独运行消费者,然后对于第一个我使用 FireForget 方法运行生产者,一切正常,消费者获取它并打印一条消息,但是当我使用 SendWait 方法运行时,消费者获取它但不返回任何内容,生产者等待得到响应,我该如何处理这个问题?

【问题讨论】:

    标签: rabbitmq spring-integration


    【解决方案1】:

    回复没有相关性

    <int-amqp:outbound-channel-adapter channel="request" amqp-template="amqpTemplate" 
        exchange-name="exchange.main" routing-key="binding.queue.request"       
    <int-amqp:inbound-channel-adapter channel="response" queue-names="queue.response"
         connection-factory="connectionFactory" />  
    

    改为使用出站网关;您可以使用带有&lt;reply-listener/&gt;discussed in the Spring AMQP documentation 的Rabbit 模板对其进行配置。

    并在消费者端使用入站网关。

    【讨论】:

    • 我改变了我的代码并使用了你提到的出站和入站网关,当生产者和消费者之间传递的对象是原始的(如 String 或 int)时一切正常,但是当我尝试传递 POJO 类时,我得到了这个错误:无法将字节 [[]] 类型的值转换为所需类型 ....
    • 你需要一个合适的MessageConverter;该框架提供了一个方便的JsonMessageConverter,它使用 Jackson 将 POJO 转换为 Json 或从 Json 转换 POJO,只要您的 POJO 对 Jackson 友好。默认转换器 (SimpleMessageConverter) 处理字符串。 byte[] 和 Java 序列化。 Java 序列化需要可序列化的 POJO - 否则会发送空消息。因此,请使用JsonMessageSerializer 或使您的 POJO 可序列化(但 JSON 通常被认为是 AMQP 的更好选择,因为它对多语言友好)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-02-14
    • 2017-07-13
    • 2011-11-08
    • 2016-10-25
    • 1970-01-01
    • 2011-07-23
    • 2018-05-15
    相关资源
    最近更新 更多