Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
   * Copyright 2002-2014 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
  * 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.simp;
 import java.util.Map;
A specialization of org.springframework.messaging.core.AbstractMessageSendingTemplate that interprets a String-based destination as the DESTINATION_HEADER to be added to the headers of sent messages.

Also provides methods for sending messages to a user. See UserDestinationResolver for more on user destinations.

Rossen Stoyanchev
 	private final MessageChannel messageChannel;
 	private String userDestinationPrefix = "/user/";
 	private volatile long sendTimeout = -1;

Create a new SimpMessagingTemplate instance.

messageChannel the message channel (must not be null)
 	public SimpMessagingTemplate(MessageChannel messageChannel) {
 		Assert.notNull(messageChannel"MessageChannel must not be null");
 		this. = messageChannel;

Return the configured message channel.
 		return this.;

Configure the prefix to use for destinations targeting a specific user.

The default value is "/user/".

 	public void setUserDestinationPrefix(String prefix) {
 		Assert.notNull(prefix"UserDestinationPrefix must not be null");
 		this. = prefix;

Return the configured user destination prefix.
 		return this.;

Specify the timeout value to use for send operations (in milliseconds).
 	public void setSendTimeout(long sendTimeout) {
 		this. = sendTimeout;

Return the configured send timeout (in milliseconds).
	public long getSendTimeout() {
		return this.;
	public void send(Message<?> message) {
		SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.wrap(message);
		String destination = headers.getDestination();
		destination = (destination != null ? destination : getRequiredDefaultDestination());
	protected void doSend(String destinationMessage<?> message) {
		Assert.notNull(destination"Destination must not be null");
		SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.wrap(message);
		message = MessageBuilder.withPayload(message.getPayload()).setHeaders(headers).build();
		long timeout = this.;
		boolean sent = (timeout >= 0 ? this..send(messagetimeout) : this..send(message));
		if (!sent) {
			throw new MessageDeliveryException(message,
					"Failed to send message to destination '" + destination + "' within timeout: " + timeout);
	public void convertAndSendToUser(String userString destinationObject payloadthrows MessagingException {
		convertAndSendToUser(userdestinationpayload, (MessagePostProcessornull);
	public void convertAndSendToUser(String userString destinationObject payload,
			Map<StringObjectheadersthrows MessagingException {
	public void convertAndSendToUser(String userString destinationObject payload,
			MessagePostProcessor postProcessorthrows MessagingException {
	public void convertAndSendToUser(String userString destinationObject payloadMap<StringObjectheaders,
			MessagePostProcessor postProcessorthrows MessagingException {
		Assert.notNull(user"User must not be null");
		user = StringUtils.replace(user"/""%2F");
		super.convertAndSend(this.userDestinationPrefix + user + destinationpayloadheaderspostProcessor);

Creates a new map and puts the given headers under the key NATIVE_HEADERS. Effectively this treats all given headers as headers to be sent out to the external source.

If the given headers already contain the key NATIVE_HEADERS then the same header map is returned (i.e. without any changes).

	protected Map<StringObjectprocessHeadersToSend(Map<StringObjectheaders) {
		if (headers == null) {
			return null;
			return headers;
		MultiValueMap<StringStringnativeHeaders = new LinkedMultiValueMap<StringString>(headers.size());
		for (String key : headers.keySet()) {
			Object value = headers.get(key);
			nativeHeaders.set(key, (value != null ? value.toString() : null));
		headers = new HashMap<StringObject>(1);
		return headers;
New to GrepCode? Check out our FAQ X