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.

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

How Spring in Action, Sixth Edition covers it

How deep it goes
Shows an anonymous callback and lambda, including the returned message, without tracing framework internals.
Examples
Adds an X_ORDER_SOURCE property with value WEB to an order message.
What you can do after
Apply it via JmsTemplate to customize a converted message before sending.
How it connects
Connects JmsTemplate, MessageConverter, JMS destinations, and the analogous RabbitTemplate post-processing pattern.

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.