Search code examples
springrabbitmqspring-amqp

How to use RabbitTemplate sendAndReceive function with custom message?


I'm new at Spring AMQP. I wanna use sendAndReceive() with using custom message.

public void send (String exchange, String routingKey, MyCustomMessage message){
    CorrelationData correlationData = new CorrelationData("correlation-data");
    rabbitTemplate.sendAndReceive(exchange, routingKey, message, correlationData);
}

But this code occur error message

Make 'MyCustomMessage'extends org.springframework.amqp.core.Message'

Extending Message is the only way to use CustomMessage? I didn't extend when I using convertAndSend function.


Solution

  • No; as long as it is compatible with the MessageConverter in the template (default SimpleMessageConverter can handle Serializable or you can use a Jackson2JsonMessageConverter for JSON-friendly classes, or you can use a custom message converter).

    Then, use template.convertSendAndReceive(...) instead of sendAndReceive.