liveBook · Manning

MessageCreator

As explained in Spring in Action, Sixth Edition

MessageCreator is a Spring JMS callback interface whose implementation creates a JMS Message for sending.

MessageCreator is a Spring JMS callback that creates the Message passed to JmsTemplate.send(). JmsTemplate handles the sending work; the callback focuses on constructing the message. section 9.1.2

Send an order

To send an order, implement createMessage(Session session) and return session.createObjectMessage(order). Because MessageCreator has one method, the same callback can be written as a lambda: session -> session.createObjectMessage(order). section 9.1.2

@Override
public void sendOrder(TacoOrder order) {
  jms.send(session -> session.createObjectMessage(order));
}

The lambda creates an ObjectMessage from the order; send() sends it to the configured default destination. section 9.1.2

Use send() with MessageCreator when you want to construct the JMS message yourself. Use convertAndSend() when you want JmsTemplate to convert an object into a message instead. Either approach can target a default destination, a Destination object, or a destination name. section 9.1.2

How Spring in Action, Sixth Edition covers it

How deep it goes
Shows createMessage() as an anonymous implementation and a lambda, then contrasts it with automatic conversion.
Examples
Creates an ObjectMessage from an order and sends it to a default destination.
What you can do after
Apply it via JmsTemplate.send() to construct and send an order message.
How it connects
Links JmsTemplate, JMS Session, ObjectMessage, destinations, and convertAndSend().

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.