The Spring Integration Message
is a generic container for data. Any object can
be provided as the payload, and each Message
also includes a header containing
user-extensible properties as key-value pairs. Here is the definition of the
Message
interface:
public interface Message<T> { Object getId(); MessageHeader getHeader(); T getPayload(); boolean isExpired(); }
And the header provides the following properties:
Table 2.1. Properties of the MessageHeader
Property Name | Property Type |
---|---|
timestamp | java.util.Date |
expiration | java.util.Date |
correlationId | java.lang.Object |
returnAddress | java.lang.Object (can be a String or MessageChannel) |
sequenceNumber | int |
sequenceSize | int |
priority | MessagePriority (an enum) |
properties | java.util.Properties |
attributes | Map<String,Object> |
The base implementation of the Message
interface is
GenericMessage<T>
, and it provides three constructors:
new GenericMessage<T>(Object id, T payload); new GenericMessage<T>(T payload); new GenericMessage<T>(T payload, MessageHeader headerToCopy)
When no id is provided, a random unique id will be generated. The constructor that accepts a
MessageHeader
will copy properties, attributes, and any 'returnAddress' from the
provided header. There are also two convenient subclasses available currently:
StringMessage
and ErrorMessage
. The latter accepts any
Throwable
object as its payload.
The MessagePriority
is only considered when using a PriorityChannel
(as described in the next section). It is defined as an enum with five possible values:
public enum MessagePriority {
HIGHEST,
HIGH,
NORMAL,
LOW,
LOWEST
}
The Message
is obviously a very important part of the API. By encapsulating the
data in a generic wrapper, the messaging system can pass it around without any knowledge of the data's type. As
the system evolves to support new types, or when the types themselves are modified and/or extended, the messaging
system will not be affected by such changes. On the other hand, when some component in the messaging system
does require access to information about the Message
, such
metadata can typically be stored to and retrieved from the metadata in the header (the 'properties' and
'attributes').