Message Types

The Java Message Service defines six Message interface types that must be supported by JMS providers. Although JMS defines the Message interfaces, it doesn’t define their implementation. This allows vendors to implement and transport messages in their own way, while maintaining a consistent and standard interface for the JMS application developer. The six message interfaces are Message and its five subinterfaces: TextMessage, StreamMessage, MapMessage, ObjectMessage, and BytesMessage.

The Message interfaces are defined according to the kind of payload they are designed to carry. In some cases, Message types were included in JMS to support legacy payloads that are common and useful, which is the case with the TextMessage, BytesMessage, and StreamMessage message types. In other cases, the Message types were defined to facilitate emerging needs; for example, ObjectMessage can transport serializable Java objects. Some vendors may provide other proprietary message types. Progress’s SonicMQ, for example, provides an XMLMessage type that extends the TextMessage, allowing developers to deal with the message directly through DOM or SAX interfaces.

Message

The simplest type of message is the javax.jms.Message, which serves as the base interface to the other message types. As shown below, the Message type can be created and used as a JMS message with no payload:

// Create and deliver a Message Message message = session.createMessage(); publisher.publish(message); ... // Receive a ...

Get Java Message Service, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.