Null response for spring-amqp Request/Reply Messaging

半腔热情 提交于 2019-12-12 01:42:39

问题


I'm following the instructions for the Stock Trading sample, which outlines how to use request/reply messaging in spring-amqp: http://static.springsource.org/spring-amqp/docs/1.2.x/reference/html/sample-apps.html#d4e742

I've tweaked the sample instructions to create a client which should wait for the reply by using convertSendAndReceive instead of convertAndSend: https://gist.github.com/pulkitsinghal/5774487

Now even though the reply is put on the responseQueue and I've updated the timeout rabbitTemplate.setReplyTimeout(60000); to be longer than the 5 second default ... in my client I receive null back as the reply.

Does anyone know what's going on?


Update#1

I was advised to add a <reply-listener/> to the <rabbit:template/> but I'm not sure how to do that programatically:

@Bean
public RabbitTemplate rabbitTemplate() {
    RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory());
    rabbitTemplate.setMessageConverter(jsonMessageConverter());
    rabbitTemplate.setReplyQueue(responseQueue());
    rabbitTemplate.setReplyTimeout(60000);
    // following is private
    //rabbitTemplate.addListener
    return rabbitTemplate;
}

回答1:


I assume the fact you are not getting an error on the convertsendAndReceive means that you have configured the RabbitTemplate with a fixed reply queue; if you do that, you need a listener container to receive the messages with the template as the 'listener'.

The easiest way to configure this is with xml

<rabbit:template ... reply-queue="foo">
    <reply-listener/>
</rabbit:template>

I suggest you get it working first without a fixed reply queue - let the template create its own reply queue.

You should also remove the MessagePostProcessor in the convertSendAndReceive because the template will take care of its own reply queue and correlation configuration. This is not allowed when there's not a fixed reply queue.

When you switch to using a fixed reply-queue I suggest you use 1.2.0.M1 (or snapshot) because the template used a non-standard correlation technique.

Update: To use @Bean configuration instead of XML simply create a SimpleMessageListenerContainer bean and make its listener the RabbitTemplate. Just be sure to use the same queue in both places (the parser takes care of that when using the namespace).



来源:https://stackoverflow.com/questions/17090852/null-response-for-spring-amqp-request-reply-messaging

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!