How to reply in case of synchronous messaging usin

2019-05-26 04:28发布

问题:

I have code using Spring AMQP for synchronous messaging as below. My scenario is when producer sends messages it is expecting response. So, can any one help on how to send response back in case of synchronous messages.

Producer end - rabbitTemplate.convertAndSend(object);
Consumer end - rabbitTemplate.receiveAndconvert();

and I came know that we can use handleMessage() in case of asynchronous messaging. But, using above API how to send response back in case of synchronous messaging? Can you please help me on this?

Thanks in advance.

回答1:

How about to use this one convertSendAndReceive? In this case the RabbitTemplate takes care about synchronius send request and receive a reply. Try it, please and let us know



回答2:

See the reference documentation for options.

The convertSendAndReceive on the producer side adds a replyTo header (default is a new temporary queue but you can configure a fixed reply queue).

On the consumer side, the simplest solution is to a message listener container and the MessageListenerAdapter will call your simple POJO and take care of sending the reply for you.

If you must use a rabbit template on the consumer side, you would need to use a lower level receive to get access to the message properties, so you can access the replyTo information in order to send the reply.

Take a look at the source code for the MessageListenerAdapter to see how he does it.