liveBook · Manning

MessagePostProcessor

As explained in Spring in Action, Sixth Edition

MessagePostProcessor is a Spring JMS callback interface that modifies a Message after conversion and before sending.

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

How Spring in Action, Sixth Edition covers it

How deep it goes
Shows the callback method, anonymous implementation, lambda, and method reference, but does not explore callback internals.
Examples
Adds X_ORDER_SOURCE=WEB to a converted order message using an anonymous callback, lambda, or method reference.
What it assumes
Introduces the callback alongside JmsTemplate sending methods and an order example rather than assuming knowledge of the callback.
What you can do after
Apply it via JmsTemplate.convertAndSend() to set a JMS string property on a converted order message.
How it connects
Connects JmsTemplate.convertAndSend(), message conversion, JMS properties, and the analogous RabbitTemplate post-processing option.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Spring in Action, Sixth Edition. The section links point to the parts of the book it draws on.