Need to add information to a JMS message without creating the message yourself? Pass a MessagePostProcessor to JmsTemplate.convertAndSend(). JmsTemplate converts your object to a Message, calls the postprocessor to customize that Message, and then sends it. The callback returns the Message it wants sent. section 9.1.2
jms.convertAndSend("tacocloud.order.queue", order, new MessagePostProcessor() {
@Override
public Message postProcessMessage(Message message) throws JMSException {
message.setStringProperty("X_ORDER_SOURCE", "WEB");
return message;
}
});
This order-sending call converts the order, then sets the JMS string property X_ORDER_SOURCE to WEB on the resulting message. postProcessMessage() returns that message. section 9.1.2
Because MessagePostProcessor has one method, the callback can also be a lambda: message -> { message.setStringProperty("X_ORDER_SOURCE", "WEB"); return message; }. A method reference works when a method accepts a Message and returns the modified Message. Without post-processing, convertAndSend() can simply convert and send the order; with send() and a MessageCreator, you create the JMS message yourself. section 9.1.2
RabbitTemplate offers a similar convertAndSend() post-processing option, but its example sets a header through AMQP MessageProperties rather than a JMS string property. RabbitTemplate routes using exchanges and routing keys; JmsTemplate sends to a JMS destination. section 9.2.2 section 9.1.2
