org.springframework.messaging

Class MessageHeaders

  • java.lang.Object
    • org.springframework.messaging.MessageHeaders
  • All Implemented Interfaces:
    java.io.Serializable, java.util.Map<java.lang.String,java.lang.Object>


    public class MessageHeaders
    extends java.lang.Object
    implements java.util.Map<java.lang.String,java.lang.Object>, java.io.Serializable
    The headers for a Message.

    IMPORTANT: 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 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 MessageHeaderAccessor or one of its subclasses to create specific categories of headers.
    Since:
    4.0
    Author:
    Arjen Poutsma, Mark Fisher, Gary Russell, Juergen Hoeller
    See Also:
    MessageBuilder, MessageHeaderAccessor, Serialized Form
    • Nested Class Summary

      • Nested classes/interfaces inherited from interface java.util.Map

        java.util.Map.Entry<K,V>
    • Field Summary

      Fields 
      Modifier and Type Field and Description
      static java.lang.String CONTENT_TYPE
      The key for the message content type.
      static java.lang.String ERROR_CHANNEL
      The key for the message error channel.
      static java.lang.String ID
      The key for the Message ID.
      static java.util.UUID ID_VALUE_NONE
      UUID for none.
      static java.lang.String REPLY_CHANNEL
      The key for the message reply channel.
      static java.lang.String TIMESTAMP
      The key for the message timestamp.
    • Constructor Summary

      Constructors 
      Modifier Constructor and Description
        MessageHeaders(java.util.Map<java.lang.String,java.lang.Object> headers)
      Construct a MessageHeaders with the given headers.
      protected MessageHeaders(java.util.Map<java.lang.String,java.lang.Object> headers, java.util.UUID id, java.lang.Long timestamp)
      Constructor providing control over the ID and TIMESTAMP header values.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method and Description
      void clear()
      Since MessageHeaders are immutable, the call to this method will result in UnsupportedOperationException.
      boolean containsKey(java.lang.Object key) 
      boolean containsValue(java.lang.Object value) 
      java.util.Set<java.util.Map.Entry<java.lang.String,java.lang.Object>> entrySet() 
      boolean equals(java.lang.Object other) 
      java.lang.Object get(java.lang.Object key) 
      <T> T get(java.lang.Object key, java.lang.Class<T> type) 
      java.lang.Object getErrorChannel() 
      java.util.UUID getId() 
      protected static IdGenerator getIdGenerator() 
      protected java.util.Map<java.lang.String,java.lang.Object> getRawHeaders() 
      java.lang.Object getReplyChannel() 
      java.lang.Long getTimestamp() 
      int hashCode() 
      boolean isEmpty() 
      java.util.Set<java.lang.String> keySet() 
      java.lang.Object put(java.lang.String key, java.lang.Object value)
      Since MessageHeaders are immutable, the call to this method will result in UnsupportedOperationException.
      void putAll(java.util.Map<? extends java.lang.String,? extends java.lang.Object> map)
      Since MessageHeaders are immutable, the call to this method will result in UnsupportedOperationException.
      java.lang.Object remove(java.lang.Object key)
      Since MessageHeaders are immutable, the call to this method will result in UnsupportedOperationException.
      int size() 
      java.lang.String toString() 
      java.util.Collection<java.lang.Object> values() 
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface java.util.Map

        compute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, putIfAbsent, remove, replace, replace, replaceAll
    • Field Detail

      • ID_VALUE_NONE

        public static final java.util.UUID ID_VALUE_NONE
        UUID for none.
      • ID

        public static final java.lang.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.
        See Also:
        Constant Field Values
      • TIMESTAMP

        public static final java.lang.String TIMESTAMP
        The key for the message timestamp.
        See Also:
        Constant Field Values
      • CONTENT_TYPE

        public static final java.lang.String CONTENT_TYPE
        The key for the message content type.
        See Also:
        Constant Field Values
      • REPLY_CHANNEL

        public static final java.lang.String REPLY_CHANNEL
        The key for the message reply channel.
        See Also:
        Constant Field Values
      • ERROR_CHANNEL

        public static final java.lang.String ERROR_CHANNEL
        The key for the message error channel.
        See Also:
        Constant Field Values
    • Constructor Detail

      • MessageHeaders

        public MessageHeaders(@Nullable
                              java.util.Map<java.lang.String,java.lang.Object> headers)
        Construct a MessageHeaders with the given headers. An ID and TIMESTAMP headers will also be added, overriding any existing values.
        Parameters:
        headers - a map with headers to add
      • MessageHeaders

        protected MessageHeaders(@Nullable
                                 java.util.Map<java.lang.String,java.lang.Object> headers,
                                 @Nullable
                                 java.util.UUID id,
                                 @Nullable
                                 java.lang.Long timestamp)
        Constructor providing control over the ID and TIMESTAMP header values.
        Parameters:
        headers - a map with headers to add
        id - the ID header value
        timestamp - the TIMESTAMP header value
    • Method Detail

      • getRawHeaders

        protected java.util.Map<java.lang.String,java.lang.Object> getRawHeaders()
      • getIdGenerator

        protected static IdGenerator getIdGenerator()
      • getId

        @Nullable
        public java.util.UUID getId()
      • getTimestamp

        @Nullable
        public java.lang.Long getTimestamp()
      • getReplyChannel

        @Nullable
        public java.lang.Object getReplyChannel()
      • getErrorChannel

        @Nullable
        public java.lang.Object getErrorChannel()
      • get

        @Nullable
        public <T> T get(java.lang.Object key,
                                   java.lang.Class<T> type)
      • containsKey

        public boolean containsKey(java.lang.Object key)
        Specified by:
        containsKey in interface java.util.Map<java.lang.String,java.lang.Object>
      • containsValue

        public boolean containsValue(java.lang.Object value)
        Specified by:
        containsValue in interface java.util.Map<java.lang.String,java.lang.Object>
      • entrySet

        public java.util.Set<java.util.Map.Entry<java.lang.String,java.lang.Object>> entrySet()
        Specified by:
        entrySet in interface java.util.Map<java.lang.String,java.lang.Object>
      • get

        @Nullable
        public java.lang.Object get(java.lang.Object key)
        Specified by:
        get in interface java.util.Map<java.lang.String,java.lang.Object>
      • isEmpty

        public boolean isEmpty()
        Specified by:
        isEmpty in interface java.util.Map<java.lang.String,java.lang.Object>
      • keySet

        public java.util.Set<java.lang.String> keySet()
        Specified by:
        keySet in interface java.util.Map<java.lang.String,java.lang.Object>
      • size

        public int size()
        Specified by:
        size in interface java.util.Map<java.lang.String,java.lang.Object>
      • values

        public java.util.Collection<java.lang.Object> values()
        Specified by:
        values in interface java.util.Map<java.lang.String,java.lang.Object>
      • put

        public java.lang.Object put(java.lang.String key,
                                    java.lang.Object value)
        Since MessageHeaders are immutable, the call to this method will result in UnsupportedOperationException.
        Specified by:
        put in interface java.util.Map<java.lang.String,java.lang.Object>
      • putAll

        public void putAll(java.util.Map<? extends java.lang.String,? extends java.lang.Object> map)
        Since MessageHeaders are immutable, the call to this method will result in UnsupportedOperationException.
        Specified by:
        putAll in interface java.util.Map<java.lang.String,java.lang.Object>
      • remove

        public java.lang.Object remove(java.lang.Object key)
        Since MessageHeaders are immutable, the call to this method will result in UnsupportedOperationException.
        Specified by:
        remove in interface java.util.Map<java.lang.String,java.lang.Object>
      • clear

        public void clear()
        Since MessageHeaders are immutable, the call to this method will result in UnsupportedOperationException.
        Specified by:
        clear in interface java.util.Map<java.lang.String,java.lang.Object>
      • equals

        public boolean equals(java.lang.Object other)
        Specified by:
        equals in interface java.util.Map<java.lang.String,java.lang.Object>
        Overrides:
        equals in class java.lang.Object
      • hashCode

        public int hashCode()
        Specified by:
        hashCode in interface java.util.Map<java.lang.String,java.lang.Object>
        Overrides:
        hashCode in class java.lang.Object
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object