MessagePostProcessor is a Spring JMS callback for customizing a message after an object has been converted and before the message is sent. It lets you add message properties without creating the JMS message yourself. section 9.1.2
Pass a processor to a JmsTemplate.convertAndSend() overload along with the object to send. The overload can use a default destination, a Destination object, or a destination name. By contrast, send() uses a MessageCreator to make the message explicitly. 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 example adds the X_ORDER_SOURCE property with the value WEB. The callback receives the converted message, sets the property, and returns the message for sending. section 9.1.2
jms.convertAndSend("tacocloud.order.queue", order,
message -> {
message.setStringProperty("X_ORDER_SOURCE", "WEB");
return message;
});
The same callback can be written as a lambda, keeping the property change close to the send operation. section 9.1.2
RabbitTemplate offers a similar post-processing option for converted AMQP messages. Its routing uses exchanges and routing keys rather than JMS destinations; its message and processor types are AMQP counterparts, not the JMS types. section 9.2.2
public void sendOrder(TacoOrder order) {
rabbit.convertAndSend("tacocloud.order.queue", order,
new MessagePostProcessor() {
@Override
public Message postProcessMessage(Message message)
throws AmqpException {
MessageProperties props = message.getMessageProperties();
props.setHeader("X_ORDER_SOURCE", "WEB");
return message;
}
});
}
The RabbitTemplate example adds X_ORDER_SOURCE to the converted message’s properties before returning it. section 9.2.2
