liveBook · Manning

Message

As explained in Spring in Action, Sixth Edition

An integration message is a data-bearing unit that moves through components of an integration flow.

A message is a data-bearing unit that moves between components. In an integration flow, messages carry data through a pipeline that can transform, route, split, aggregate, or otherwise process it. section 10.0 section 10.4

A message can carry a payload, such as a taco order, along with metadata. For example, headers can identify an order’s source or provide Kafka partition and timestamp information. A consumer can work with the payload directly or access the message when it needs metadata. section 9.2.2 section 9.3.3

Sending and receiving an order

An order service can use JmsTemplate.convertAndSend() to convert a TacoOrder into a JMS message and send it to tacocloud.order.queue. A kitchen service can then use receiveAndConvert() to pull the message and convert it back into a TacoOrder. section 9.1.2 section 9.1.3

@Override
public void sendOrder(TacoOrder order) {
  jms.convertAndSend("tacocloud.order.queue", order);
}

This sender supplies the queue name and order object; JmsTemplate handles the conversion to a message. section 9.1.2

Listing 9.3 Receiving an already-converted TacoOrder object
package tacos.kitchen.messaging.jms;
 
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Component;
import tacos.TacoOrder;
import tacos.kitchen.OrderReceiver;
 
@Component
public class JmsOrderReceiver implements OrderReceiver {
 
  private JmsTemplate jms;
 
  public JmsOrderReceiver(JmsTemplate jms) {
    this.jms = jms;
  }
  
  @Override
  public TacoOrder receiveOrder() {
    return (TacoOrder) jms.receiveAndConvert("tacocloud.order.queue");
  }
  
}

The receiver pulls from the same queue and converts the message back to an order. section 9.1.3

For more control, JmsTemplate.send() accepts a MessageCreator to construct the message. With convertAndSend(), a MessagePostProcessor can customize the converted message before sending—for example, by adding an X_ORDER_SOURCE property. RabbitTemplate follows a similar pattern, but sends using an exchange and routing key instead of a JMS destination. section 9.1.2 section 9.2.2

A consumer can pull a JMS message with JmsTemplate or use a listener that is invoked when a message arrives. Pulling lets the consumer decide when it is ready for another message, but the receiving thread waits for one. A Kafka listener can receive the order directly or accept a Message to inspect its headers. section 9.1.3 section 9.3.3

Within a Spring Integration flow, message channels connect components. Channel choice affects delivery: a PublishSubscribeChannel passes a message to all its consumers, while a QueueChannel stores messages until a consumer pulls them and delivers each message to only one consumer. section 10.2.1 section 10.4

How Spring in Action, Sixth Edition covers it

How deep it goes
Explains message creation, conversion, metadata, routing, and consumption with framework APIs rather than message-format internals.
Examples
Uses taco orders sent to a queue and delivered to a kitchen, with an optional order-source property.
What you can do after
Apply it via JmsTemplate or RabbitTemplate to send converted objects and customize message metadata.
How it connects
Connects messages to channels, message converters, queues, routing keys, and listeners.

Also explained in

Compare how 8 books explain message →

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.