Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2002-2013 the original author or authors.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package org.springframework.messaging;
 
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
 import java.util.UUID;
 
 
The headers for a Message

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

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 java.lang.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 sub-classes to create specific categories of headers.

 
 public final class MessageHeaders implements Map<StringObject>, Serializable {
 
 	private static final long serialVersionUID = -4615750558355702881L;
 
 	private static final Log logger = LogFactory.getLog(MessageHeaders.class);
 
 	private static volatile IdGenerator idGenerator = null;
 
 	private static final IdGenerator defaultIdGenerator = new AlternativeJdkIdGenerator();

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 String ID = "id";
 
 	public static final String TIMESTAMP = "timestamp";
 
 	public static final String REPLY_CHANNEL = "replyChannel";
 
 	public static final String ERROR_CHANNEL = "errorChannel";
 
 	public static final String CONTENT_TYPE = "contentType";
 
 
 	private final Map<StringObjectheaders;
 
 
 	public MessageHeaders(Map<StringObjectheaders) {
		this. = (headers != null) ? new HashMap<StringObject>(headers) : new HashMap<StringObject>();
	}
	public UUID getId() {
		return this.get(UUID.class);
	}
	public Long getTimestamp() {
		return this.get(Long.class);
	}
	public Object getReplyChannel() {
		return this.get();
	}
	public Object getErrorChannel() {
		return this.get();
	}
	@SuppressWarnings("unchecked")
	public <T> T get(Object keyClass<T> type) {
		Object value = this..get(key);
		if (value == null) {
			return null;
		}
		if (!type.isAssignableFrom(value.getClass())) {
			throw new IllegalArgumentException("Incorrect type specified for header '" +
					key + "'. Expected [" + type + "] but actual type is [" + value.getClass() + "]");
		}
		return (T) value;
	}
	public int hashCode() {
		return this..hashCode();
	}
	public boolean equals(Object object) {
		if (this == object) {
			return true;
		}
		if (object != null && object instanceof MessageHeaders) {
			MessageHeaders other = (MessageHeadersobject;
			return this..equals(other.headers);
		}
		return false;
	}
	public String toString() {
		Map<StringObjectmap = new LinkedHashMap<StringObject>(this.);
		map.put(,  map.remove()); // remove and add again at the end
		return map.toString();
	}
	/*
	 * Map implementation
	 */
	public boolean containsKey(Object key) {
		return this..containsKey(key);
	}
	public boolean containsValue(Object value) {
		return this..containsValue(value);
	}
	public Set<Map.Entry<StringObject>> entrySet() {
		return Collections.unmodifiableSet(this..entrySet());
	}
	public Object get(Object key) {
		return this..get(key);
	}
	public boolean isEmpty() {
		return this..isEmpty();
	}
	public Set<StringkeySet() {
		return Collections.unmodifiableSet(this..keySet());
	}
	public int size() {
		return this..size();
	}
	public Collection<Objectvalues() {
		return Collections.unmodifiableCollection(this..values());
	}
	// Unsupported operations
Since MessageHeaders are immutable, the call to this method will result in java.lang.UnsupportedOperationException.
	public Object put(String keyObject value) {
		throw new UnsupportedOperationException("MessageHeaders is immutable");
	}

Since MessageHeaders are immutable, the call to this method will result in java.lang.UnsupportedOperationException.
	public void putAll(Map<? extends String, ? extends Objectt) {
		throw new UnsupportedOperationException("MessageHeaders is immutable");
	}

Since MessageHeaders are immutable, the call to this method will result in java.lang.UnsupportedOperationException.
	public Object remove(Object key) {
		throw new UnsupportedOperationException("MessageHeaders is immutable");
	}

Since MessageHeaders are immutable, the call to this method will result in java.lang.UnsupportedOperationException.
	public void clear() {
		throw new UnsupportedOperationException("MessageHeaders is immutable");
	}
	// Serialization methods
	private void writeObject(ObjectOutputStream outthrows IOException {
		List<StringkeysToRemove = new ArrayList<String>();
		for (Map.Entry<StringObjectentry : this..entrySet()) {
			if (!(entry.getValue() instanceof Serializable)) {
				keysToRemove.add(entry.getKey());
			}
		}
		for (String key : keysToRemove) {
				.info("removing non-serializable header: " + key);
			}
			this..remove(key);
		}
	}
	}
New to GrepCode? Check out our FAQ X