liveBook · Manning

MessageConverter

As explained in Spring in Action, Sixth Edition

MessageConverter is a Spring interface for converting between application objects and JMS Message objects.

A MessageConverter translates between an application object and a JMS Message. It lets messaging code work with objects such as TacoOrder without manually building or unpacking every message. section 9.1.2 section 9.1.3

The JMS interface has two operations: toMessage() converts an object using a JMS Session, and fromMessage() converts a message back to an object. JmsTemplate.convertAndSend() uses a converter automatically; it accepts a destination name, a Destination, or the default destination. section 9.1.2

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

Send an order directly: convertAndSend() converts it before sending it to the named queue. section 9.1.2

SimpleMessageConverter is the default. It handles strings, byte arrays, maps, and Serializable objects. For JSON, use MappingJackson2MessageConverter instead; a custom converter is not usually necessary. section 9.1.2

@Bean
public MappingJackson2MessageConverter messageConverter() {
  MappingJackson2MessageConverter messageConverter =
                          new MappingJackson2MessageConverter();
  messageConverter.setTypeIdPropertyName("_typeId");
  
  Map<String, Class<?>> typeIdMappings = new HashMap<String, Class<?>>();
  typeIdMappings.put("order", TacoOrder.class);
  messageConverter.setTypeIdMappings(typeIdMappings);
  
  return messageConverter;
}

Declare a JSON converter bean and set a type ID property so the receiver can identify the target type. This example maps the ID “order” to TacoOrder. section 9.1.2

On receipt, JmsTemplate.receive() returns a raw Message, which is useful when you need to inspect headers or properties. If you need only the payload, receiveAndConvert() uses the configured converter and returns the converted object; cast it to the expected type. section 9.1.3

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");
  }
  
}

Receive and convert an order in one call. section 9.1.3

Spring AMQP has a separate MessageConverter interface for RabbitTemplate. You can obtain it with getMessageConverter() and convert explicitly, or use convertAndSend() and receiveAndConvert() to let the template perform conversion. A Jackson2JsonMessageConverter bean enables JSON conversion. section 9.2.2 section 9.2.3

Listing 9.6 Pulling orders from RabbitMQ with RabbitTemplate
package tacos.kitchen.messaging.rabbit;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
@Component
public class RabbitOrderReceiver {
  private RabbitTemplate rabbit;
  private MessageConverter converter;
 
  @Autowired
  public RabbitOrderReceiver(RabbitTemplate rabbit) {
    this.rabbit = rabbit;
    this.converter = rabbit.getMessageConverter();
  }
 
  public TacoOrder receiveOrder() {
    Message message = rabbit.receive("tacocloud.order");
    return message != null
           ? (TacoOrder) converter.fromMessage(message)
           : null;
  }
}

For explicit RabbitMQ conversion, receive the raw message, check for null, and call fromMessage(). section 9.2.3

How Spring in Action, Sixth Edition covers it

How deep it goes
Defines the two JMS conversion operations and demonstrates built-in converter configuration and template use.
Examples
Uses taco orders, a JSON converter bean with a type ID mapping, and queue send and receive calls.
What you can do after
Apply it via JmsTemplate to send and receive converted domain objects and configure a JSON converter.
How it connects
Connects JMS messages with JmsTemplate, JSON serialization, type IDs, and Spring AMQP message conversion.

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.