Search code examples
rabbitmqspring-amqp

Use of RabbitTemplate.convertSendAndReceive with org.springframework.messaging.Message


I have successfully used the following to send an org.springframework.amqp.core.Message and receive a byte []

import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.rabbit.core.RabbitTemplate;

Message message = 
MessageBuilder.withBody(payload)..setCorrelationIdString(id).build();
byte [] response = (byte[]) rabbitTemplate.convertSendAndReceive(message,m -> {
        m.getMessageProperties().setCorrelationIdString(id);

This works fine if the queues are set up to handle the message correctly for Message<?>. But I have a series of queues that use the message type org.springframework.messaging.Message specifically Message<String>.

Is there a way I can use rabbitTemplate.convertSendAndReceive to send the org.springframework.messaging.Message Message< String>. Such that the following would work.

import org.springframework.messaging.Message; 
import org.springframework.integration.support.MessageBuilder;
import org.springframework.amqp.rabbit.core.RabbitTemplate;

Message<String> message = 
MessageBuilder.withPayload(payload).setCorrelationId(id).build();
Object returnObject = rabbitTemplate.convertSendAndReceive(message);

I have looked at the MessageConverter but I am unsure if I can use that.

Alternatively, should I use org.springframework.messaging.core.GenericMessagingTemplate.convertSendAndReceive

UPDATE.

I can make it work if I change what I have on the queues from

@Transformer(inputChannel = Processor.INPUT, outputChannel = Processor.OUTPUT) public Message<String> transform(Message<String> inMessage) {

to

@Transformer(inputChannel = Processor.INPUT, outputChannel = Processor.OUTPUT) public Message<String> transform(Message<?> inMessage) { GenericMessage<?> genericMessage = (GenericMessage<?>)inMessage.getPayload(); String payload = (String)genericMessage.getPayload();

but I would rather not have to change the transformers to make this work as the code in question is for integration tests and existing code already works with what I already have.

END UPDATE

I think I have given enough information but please let me know if more details are required. Ideally, I am looking for a code example or to point me to the documentation that answers my question.


Solution

  • Use the RabbitMessagingTemplate documentation here.

    public Message<?> sendAndReceive(String exchange, String routingKey, Message<?> requestMessage)