2.2 Message Headers

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]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 NameHeader Type
IDjava.util.UUID
TIMESTAMPjava.lang.Long
EXPIRATION_DATEjava.lang.Long
CORRELATION_IDjava.lang.Object
REPLY_CHANNELjava.lang.Object (can be a String or MessageChannel)
ERROR_CHANNELjava.lang.Object (can be a String or MessageChannel)
SEQUENCE_NUMBERjava.lang.Integer
SEQUENCE_SIZEjava.lang.Integer
PRIORITYMessagePriority (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.