Top Description Fields Constructors Methods
org.springframework.messaging

public Class MessageHeaders

extends Object
implements Map<String, Object>, Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable, java.util.Map
Known Direct Subclasses
org.springframework.messaging.support.MessageHeaderAccessor.MutableMessageHeaders
Imports
java.io.IOException, .ObjectInputStream, .ObjectOutputStream, .Serializable, java.util.Collection, .Collections, .HashMap, .HashSet, .Map, .Set, .UUID, org.apache.commons.logging.Log, .LogFactory, org.springframework.lang.Nullable, org.springframework.util.AlternativeJdkIdGenerator, .CollectionUtils, .IdGenerator

The headers for a Message.

Important Note

This class is immutable. Any mutating operation such as put(..), putAll(..) and others will throw UnsupportedOperationException.

Subclasses do have access to the raw headers, however, via getRawHeaders().

One way to create message headers is to use the MessageBuilder:

MessageBuilder.withPayload("foo").setHeader("key1", "value1").setHeader("key2", "value2");
A second option is to create org.springframework.messaging.support.GenericMessage passing a payload as Object and headers as a java.util.Map:
Map headers = new HashMap();
headers.put("key1", "value1");
headers.put("key2", "value2");
new GenericMessage("foo", headers);
A third option is to use org.springframework.messaging.support.MessageHeaderAccessor or one of its subclasses to create specific categories of headers.
Authors
Arjen Poutsma, Mark Fisher, Gary Russell, Juergen Hoeller
Since
4.0
See Also
org.springframework.messaging.support.MessageBuilder, org.springframework.messaging.support.MessageHeaderAccessor

Field Summary

Modifier and TypeField and Description
public static final String
CONTENT_TYPE

The key for the message content type.

private static final IdGenerator
public static final String
ERROR_CHANNEL

The key for the message error channel.

private final Map<String, Object>
public static final String
ID

The key for the Message ID. This is an automatically generated UUID and should never be explicitly set in the header map except in the case of Message deserialization where the serialized Message's generated UUID is being restored.

public static final UUID
ID_VALUE_NONE

UUID for none.

private static volatile IdGenerator
private static final Log
public static final String
REPLY_CHANNEL

The key for the message reply channel.

private static final long
public static final String
TIMESTAMP

The key for the message timestamp.

Constructor Summary

AccessConstructor and Description
public
MessageHeaders(Map<String, Object>
a map with headers to add
headers
)

Construct a MessageHeaders with the given headers.

protected
MessageHeaders(Map<String, Object>
a map with headers to add
headers
,
UUID
the ID header value
id
,
Long
the TIMESTAMP header value
timestamp
)

Constructor providing control over the ID and TIMESTAMP header values.

private
MessageHeaders(MessageHeaders
the MessageHeaders to copy
original
,
Set<String>
the keys of the entries to ignore
keysToIgnore
)

Copy constructor which allows for ignoring certain entries.

Method Summary

Modifier and TypeMethod and Description
public void
clear()

Implements java.util.Map.clear.

Since MessageHeaders are immutable, the call to this method will result in UnsupportedOperationException.
public boolean
containsKey(Object
key whose presence in this map is to be tested
key
)

Implements java.util.Map.containsKey.

Returns true if this map contains a mapping for the specified key.
public boolean
containsValue(Object
value whose presence in this map is to be tested
value
)

Implements java.util.Map.containsValue.

Returns true if this map maps one or more keys to the specified value.
public Set<Map.Entry<String, Object>>
entrySet()

Implements java.util.Map.entrySet.

Returns a Set view of the mappings contained in this map.
public boolean
equals(Object
object to be compared for equality with this map
other
)

Overrides java.lang.Object.equals.

Implements java.util.Map.equals.

Compares the specified object with this map for equality.
public <T> T
get(Object key, Class<T> type)

public Object
get(Object
the key whose associated value is to be returned
key
)

Implements java.util.Map.get.

Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.
public Object
public UUID
protected static IdGenerator
protected Map<String, Object>
public Object
public Long
public int
hashCode()

Overrides java.lang.Object.hashCode.

Implements java.util.Map.hashCode.

Returns the hash code value for this map.
public boolean
isEmpty()

Implements java.util.Map.isEmpty.

Returns true if this map contains no key-value mappings.
public Set<String>
keySet()

Implements java.util.Map.keySet.

Returns a Set view of the keys contained in this map.
public Object
put(String
key with which the specified value is to be associated
key
,
Object
value to be associated with the specified key
value
)

Implements java.util.Map.put.

Since MessageHeaders are immutable, the call to this method will result in UnsupportedOperationException.
public void
putAll(Map<? extends String, ? extends Object>
mappings to be stored in this map
map
)

Implements java.util.Map.putAll.

Since MessageHeaders are immutable, the call to this method will result in UnsupportedOperationException.
private void
public Object
remove(Object
key whose mapping is to be removed from the map
key
)

Implements java.util.Map.remove.

Since MessageHeaders are immutable, the call to this method will result in UnsupportedOperationException.
public int
size()

Implements java.util.Map.size.

Returns the number of key-value mappings in this map.
public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.
public Collection<Object>
values()

Implements java.util.Map.values.

Returns a Collection view of the values contained in this map.
private void
Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait

Field Detail

CONTENT_TYPEback to summary
public static final String CONTENT_TYPE

The key for the message content type.

defaultIdGeneratorback to summary
private static final IdGenerator defaultIdGenerator
ERROR_CHANNELback to summary
public static final String ERROR_CHANNEL

The key for the message error channel.

headersback to summary
private final Map<String, Object> headers
Annotations
@SuppressWarnings:serial
IDback to summary
public static final String ID

The key for the Message ID. This is an automatically generated UUID and should never be explicitly set in the header map except in the case of Message deserialization where the serialized Message's generated UUID is being restored.

ID_VALUE_NONEback to summary
public static final UUID ID_VALUE_NONE

UUID for none.

idGeneratorback to summary
private static volatile IdGenerator idGenerator
Annotations
@Nullable
loggerback to summary
private static final Log logger
REPLY_CHANNELback to summary
public static final String REPLY_CHANNEL

The key for the message reply channel.

serialVersionUIDback to summary
private static final long serialVersionUID
TIMESTAMPback to summary
public static final String TIMESTAMP

The key for the message timestamp.

Constructor Detail

MessageHeadersback to summary
public MessageHeaders(Map<String, Object> headers)

Construct a MessageHeaders with the given headers. An ID and TIMESTAMP headers will also be added, overriding any existing values.

Parameters
headers:Map<String, Object>

a map with headers to add

MessageHeadersback to summary
protected MessageHeaders(Map<String, Object> headers, UUID id, Long timestamp)

Constructor providing control over the ID and TIMESTAMP header values.

Parameters
headers:Map<String, Object>

a map with headers to add

id:UUID

the ID header value

timestamp:Long

the TIMESTAMP header value

MessageHeadersback to summary
private MessageHeaders(MessageHeaders original, Set<String> keysToIgnore)

Copy constructor which allows for ignoring certain entries. Used for serialization without non-serializable entries.

Parameters
original:MessageHeaders

the MessageHeaders to copy

keysToIgnore:Set<String>

the keys of the entries to ignore

Method Detail

clearback to summary
public void clear()

Implements java.util.Map.clear.

Since MessageHeaders are immutable, the call to this method will result in UnsupportedOperationException.

Annotations
@Override
containsKeyback to summary
public boolean containsKey(Object key)

Implements java.util.Map.containsKey.

Doc from java.util.Map.containsKey.

Returns true if this map contains a mapping for the specified key. More formally, returns true if and only if this map contains a mapping for a key k such that Objects.equals(key, k). (There can be at most one such mapping.)

Parameters
key:Object

key whose presence in this map is to be tested

Returns:boolean

true if this map contains a mapping for the specified key

Annotations
@Override
containsValueback to summary
public boolean containsValue(Object value)

Implements java.util.Map.containsValue.

Doc from java.util.Map.containsValue.

Returns true if this map maps one or more keys to the specified value. More formally, returns true if and only if this map contains at least one mapping to a value v such that Objects.equals(value, v). This operation will probably require time linear in the map size for most implementations of the Map interface.

Parameters
value:Object

value whose presence in this map is to be tested

Returns:boolean

true if this map maps one or more keys to the specified value

Annotations
@Override
entrySetback to summary
public Set<Map.Entry<String, Object>> entrySet()

Implements java.util.Map.entrySet.

Doc from java.util.Map.entrySet.

Returns a Set view of the mappings contained in this map. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa. If the map is modified while an iteration over the set is in progress (except through the iterator's own remove operation, or through the setValue operation on a map entry returned by the iterator) the results of the iteration are undefined. The set supports element removal, which removes the corresponding mapping from the map, via the Iterator.remove, Set.remove, removeAll, retainAll and clear operations. It does not support the add or addAll operations.

Returns:Set<Map.Entry<String, Object>>

a set view of the mappings contained in this map

Annotations
@Override
equalsback to summary
public boolean equals(Object other)

Overrides java.lang.Object.equals.

Implements java.util.Map.equals.

Doc from java.util.Map.equals.

Compares the specified object with this map for equality. Returns true if the given object is also a map and the two maps represent the same mappings. More formally, two maps m1 and m2 represent the same mappings if m1.entrySet().equals(m2.entrySet()). This ensures that the equals method works properly across different implementations of the Map interface.

Parameters
other:Object

object to be compared for equality with this map

Returns:boolean

true if the specified object is equal to this map

Annotations
@Override
getback to summary
public <T> T get(Object key, Class<T> type)
Annotations
@SuppressWarnings:unchecked
@Nullable
getback to summary
public Object get(Object key)

Implements java.util.Map.get.

Doc from java.util.Map.get.

Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.

More formally, if this map contains a mapping from a key k to a value v such that Objects.equals(key, k), then this method returns v; otherwise it returns null. (There can be at most one such mapping.)

If this map permits null values, then a return value of null does not necessarily indicate that the map contains no mapping for the key; it's also possible that the map explicitly maps the key to null. The containsKey operation may be used to distinguish these two cases.

Parameters
key:Object

the key whose associated value is to be returned

Returns:Object

the value to which the specified key is mapped, or null if this map contains no mapping for the key

Annotations
@Override
@Nullable
getErrorChannelback to summary
public Object getErrorChannel()
Annotations
@Nullable
getIdback to summary
public UUID getId()
Annotations
@Nullable
getIdGeneratorback to summary
protected static IdGenerator getIdGenerator()
getRawHeadersback to summary
protected Map<String, Object> getRawHeaders()
getReplyChannelback to summary
public Object getReplyChannel()
Annotations
@Nullable
getTimestampback to summary
public Long getTimestamp()
Annotations
@Nullable
hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Implements java.util.Map.hashCode.

Doc from java.util.Map.hashCode.

Returns the hash code value for this map. The hash code of a map is defined to be the sum of the hash codes of each entry in the map's entrySet() view. This ensures that m1.equals(m2) implies that m1.hashCode()==m2.hashCode() for any two maps m1 and m2, as required by the general contract of Object#hashCode.

Returns:int

the hash code value for this map

Annotations
@Override
isEmptyback to summary
public boolean isEmpty()

Implements java.util.Map.isEmpty.

Doc from java.util.Map.isEmpty.

Returns true if this map contains no key-value mappings.

Returns:boolean

true if this map contains no key-value mappings

Annotations
@Override
keySetback to summary
public Set<String> keySet()

Implements java.util.Map.keySet.

Doc from java.util.Map.keySet.

Returns a Set view of the keys contained in this map. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa. If the map is modified while an iteration over the set is in progress (except through the iterator's own remove operation), the results of the iteration are undefined. The set supports element removal, which removes the corresponding mapping from the map, via the Iterator.remove, Set.remove, removeAll, retainAll, and clear operations. It does not support the add or addAll operations.

Returns:Set<String>

a set view of the keys contained in this map

Annotations
@Override
putback to summary
public Object put(String key, Object value)

Implements java.util.Map.put.

Since MessageHeaders are immutable, the call to this method will result in UnsupportedOperationException.

Parameters
key:String

Doc from java.util.Map.put.

key with which the specified value is to be associated

value:Object

Doc from java.util.Map.put.

value to be associated with the specified key

Returns:Object

Doc from java.util.Map.put.

the previous value associated with key, or null if there was no mapping for key. (A null return can also indicate that the map previously associated null with key, if the implementation supports null values.)

Annotations
@Override
putAllback to summary
public void putAll(Map<? extends String, ? extends Object> map)

Implements java.util.Map.putAll.

Since MessageHeaders are immutable, the call to this method will result in UnsupportedOperationException.

Parameters
map:Map<? extends String, ? extends Object>

Doc from java.util.Map.putAll.

mappings to be stored in this map

Annotations
@Override
readObjectback to summary
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException
removeback to summary
public Object remove(Object key)

Implements java.util.Map.remove.

Since MessageHeaders are immutable, the call to this method will result in UnsupportedOperationException.

Parameters
key:Object

Doc from java.util.Map.remove.

key whose mapping is to be removed from the map

Returns:Object

Doc from java.util.Map.remove.

the previous value associated with key, or null if there was no mapping for key.

Annotations
@Override
sizeback to summary
public int size()

Implements java.util.Map.size.

Doc from java.util.Map.size.

Returns the number of key-value mappings in this map. If the map contains more than Integer.MAX_VALUE elements, returns Integer.MAX_VALUE.

Returns:int

the number of key-value mappings in this map

Annotations
@Override
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object.

Returns:String

a string representation of the object.

Annotations
@Override
valuesback to summary
public Collection<Object> values()

Implements java.util.Map.values.

Doc from java.util.Map.values.

Returns a Collection view of the values contained in this map. The collection is backed by the map, so changes to the map are reflected in the collection, and vice-versa. If the map is modified while an iteration over the collection is in progress (except through the iterator's own remove operation), the results of the iteration are undefined. The collection supports element removal, which removes the corresponding mapping from the map, via the Iterator.remove, Collection.remove, removeAll, retainAll and clear operations. It does not support the add or addAll operations.

Returns:Collection<Object>

a collection view of the values contained in this map

Annotations
@Override
writeObjectback to summary
private void writeObject(ObjectOutputStream out) throws IOException