Just as Spring Integration allows any Object to be used as the payload of a Message, it also supports any Object
types as header values. In fact, the MessageHeaders
class implements the
java.util.Map interface:
public final class MessageHeaders implements Map<String, Object>, Serializable { ... }
Note | |
---|---|
Even though the MessageHeaders implements Map, it is effectively a read-only implementation. Any attempt to
put a value in the Map will result in an UnsupportedOperationException .
The same applies for remove and clear. Since Messages may be passed to
multiple consumers, the structure of the Map cannot be modified. Likewise, the Message's payload Object can not
be set after the initial creation. However, the mutability of the header values themselves
(or the payload Object) is intentionally left as a decision for the framework user.
|
As an implementation of Map, the headers can obviously be retrieved by calling get(..)
with the name of the header. Alternatively, you can provide the expected Class as an
additional parameter. Even better, when retrieving one of the pre-defined values, convenient getters are
available. Here is an example of each of these three options:
Object someValue = message.getHeaders().get("someKey"); CustomerId customerId = message.getHeaders().get("customerId", CustomerId.class); Long timestamp = message.getHeaders().getTimestamp();
The following Message headers are pre-defined:
Table 2.1. Pre-defined Message Headers
Header Name | Header Type |
---|---|
ID | java.util.UUID |
TIMESTAMP | java.lang.Long |
EXPIRATION_DATE | java.lang.Long |
CORRELATION_ID | java.lang.Object |
REPLY_CHANNEL | java.lang.Object (can be a String or MessageChannel) |
ERROR_CHANNEL | java.lang.Object (can be a String or MessageChannel) |
SEQUENCE_NUMBER | java.lang.Integer |
SEQUENCE_SIZE | java.lang.Integer |
PRIORITY | MessagePriority (an enum) |
Many inbound and outbound adapter implementations will also provide and/or expect certain headers, and additional user-defined headers can also be configured.