Top Description Fields Constructors Methods
org.springframework.messaging.simp

public Class SimpMessagingTemplate

extends AbstractMessageSendingTemplate<String>
implements SimpMessageSendingOperations
Class Inheritance
All Implemented Interfaces
org.springframework.messaging.simp.SimpMessageSendingOperations, org.springframework.messaging.core.MessageSendingOperations
Imports
java.util.Map, org.springframework.lang.Nullable, org.springframework.messaging.Message, .MessageChannel, .MessageDeliveryException, .MessageHeaders, .MessagingException, org.springframework.messaging.core.AbstractMessageSendingTemplate, .MessagePostProcessor, org.springframework.messaging.support.MessageBuilder, .MessageHeaderAccessor, .MessageHeaderInitializer, .NativeMessageHeaderAccessor, org.springframework.util.Assert, .StringUtils

An implementation of org.springframework.messaging.simp.SimpMessageSendingOperations.

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

Author
Rossen Stoyanchev
Since
4.0

Field Summary

Modifier and TypeField and Description
private String
private MessageHeaderInitializer
private final MessageChannel
private volatile long
Inherited from org.springframework.messaging.core.AbstractMessageSendingTemplate:
CONVERSION_HINT_HEADERlogger

Constructor Summary

AccessConstructor and Description
public
SimpMessagingTemplate(MessageChannel
the message channel (never null)
messageChannel
)

Create a new SimpMessagingTemplate instance.

Method Summary

Modifier and TypeMethod and Description
public void
convertAndSendToUser(String
the user that should receive the message.
user
,
String
the destination to send the message to.
destination
,
Object
the payload to send
payload
)

Implements org.springframework.messaging.simp.SimpMessageSendingOperations.convertAndSendToUser.

Send a message to the given user.

public void
convertAndSendToUser(String
the user that should receive the message (must not be null)
user
,
String
the destination to send the message to (must not be null)
destination
,
Object
the payload to send (may be null)
payload
,
Map<String, Object>
the message headers (may be null)
headers
)

Implements org.springframework.messaging.simp.SimpMessageSendingOperations.convertAndSendToUser.

Send a message to the given user.

public void
convertAndSendToUser(String
the user that should receive the message (must not be null)
user
,
String
the destination to send the message to (must not be null)
destination
,
Object
the payload to send (may be null)
payload
,
MessagePostProcessor
a postProcessor to post-process or modify the created message
postProcessor
)

Implements org.springframework.messaging.simp.SimpMessageSendingOperations.convertAndSendToUser.

Send a message to the given user.

public void
convertAndSendToUser(String
the user that should receive the message
user
,
String
the destination to send the message to
destination
,
Object
the payload to send
payload
,
Map<String, Object>
the message headers
headers
,
MessagePostProcessor
a postProcessor to post-process or modify the created message
postProcessor
)

Implements org.springframework.messaging.simp.SimpMessageSendingOperations.convertAndSendToUser.

Send a message to the given user.

protected void
public MessageHeaderInitializer
getHeaderInitializer()

Return the configured header initializer.

public MessageChannel
getMessageChannel()

Return the configured message channel.

public long
getSendTimeout()

Return the configured send timeout (in milliseconds).

public String
getUserDestinationPrefix()

Return the configured user destination prefix.

private void
protected Map<String, Object>
processHeadersToSend(Map<String, Object>
the headers to send (or null if none)
headers
)

Overrides org.springframework.messaging.core.AbstractMessageSendingTemplate.processHeadersToSend.

Creates a new map and puts the given headers under the key NATIVE_HEADERS.

public void
send(Message<?>
the message to send (never null)
message
)

Overrides org.springframework.messaging.core.AbstractMessageSendingTemplate.send.

Implements org.springframework.messaging.core.MessageSendingOperations.send.

If the headers of the given message already contain a SimpMessageHeaderAccessor#DESTINATION_HEADER then the message is sent without further changes.

private void
sendInternal(Message<?> message)

public void
setHeaderInitializer(MessageHeaderInitializer headerInitializer)

Configure a MessageHeaderInitializer to apply to the headers of all messages created through the SimpMessagingTemplate.

public void
setSendTimeout(long sendTimeout)

Specify the timeout value to use for send operations (in milliseconds).

public void
setUserDestinationPrefix(String prefix)

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

Inherited from org.springframework.messaging.core.AbstractMessageSendingTemplate:
convertAndSendconvertAndSendconvertAndSendconvertAndSendconvertAndSendconvertAndSenddoConvertgetDefaultDestinationgetMessageConvertergetRequiredDefaultDestinationsendsetDefaultDestinationsetMessageConverter

Field Detail

destinationPrefixback to summary
private String destinationPrefix
headerInitializerback to summary
private MessageHeaderInitializer headerInitializer
Annotations
@Nullable
messageChannelback to summary
private final MessageChannel messageChannel
sendTimeoutback to summary
private volatile long sendTimeout

Constructor Detail

SimpMessagingTemplateback to summary
public SimpMessagingTemplate(MessageChannel messageChannel)

Create a new SimpMessagingTemplate instance.

Parameters
messageChannel:MessageChannel

the message channel (never null)

Method Detail

convertAndSendToUserback to summary
public void convertAndSendToUser(String user, String destination, Object payload) throws MessagingException

Implements org.springframework.messaging.simp.SimpMessageSendingOperations.convertAndSendToUser.

Doc from org.springframework.messaging.simp.SimpMessageSendingOperations.convertAndSendToUser.

Send a message to the given user.

Parameters
user:String

the user that should receive the message.

destination:String

the destination to send the message to.

payload:Object

the payload to send

Annotations
@Override
convertAndSendToUserback to summary
public void convertAndSendToUser(String user, String destination, Object payload, Map<String, Object> headers) throws MessagingException

Implements org.springframework.messaging.simp.SimpMessageSendingOperations.convertAndSendToUser.

Doc from org.springframework.messaging.simp.SimpMessageSendingOperations.convertAndSendToUser.

Send a message to the given user.

By default headers are interpreted as native headers (e.g. STOMP) and are saved under a special key in the resulting Spring Message. In effect when the message leaves the application, the provided headers are included with it and delivered to the destination (e.g. the STOMP client or broker).

If the map already contains the key "nativeHeaders" or was prepared with SimpMessageHeaderAccessor then the headers are used directly. A common expected case is providing a content type (to influence the message conversion) and native headers. This may be done as follows:

SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.create();
accessor.setContentType(MimeTypeUtils.TEXT_PLAIN);
accessor.setNativeHeader("foo", "bar");
accessor.setLeaveMutable(true);
MessageHeaders headers = accessor.getMessageHeaders();
messagingTemplate.convertAndSendToUser(user, destination, payload, headers);

Note

if the MessageHeaders are mutable as in the above example, implementations of this interface should take notice and update the headers in the same instance (rather than copy or re-create it) and then set it immutable before sending the final message.

Parameters
user:String

the user that should receive the message (must not be null)

destination:String

the destination to send the message to (must not be null)

payload:Object

the payload to send (may be null)

headers:Map<String, Object>

the message headers (may be null)

Annotations
@Override
convertAndSendToUserback to summary
public void convertAndSendToUser(String user, String destination, Object payload, MessagePostProcessor postProcessor) throws MessagingException

Implements org.springframework.messaging.simp.SimpMessageSendingOperations.convertAndSendToUser.

Doc from org.springframework.messaging.simp.SimpMessageSendingOperations.convertAndSendToUser.

Send a message to the given user.

Parameters
user:String

the user that should receive the message (must not be null)

destination:String

the destination to send the message to (must not be null)

payload:Object

the payload to send (may be null)

postProcessor:MessagePostProcessor

a postProcessor to post-process or modify the created message

Annotations
@Override
convertAndSendToUserback to summary
public void convertAndSendToUser(String user, String destination, Object payload, Map<String, Object> headers, MessagePostProcessor postProcessor) throws MessagingException

Implements org.springframework.messaging.simp.SimpMessageSendingOperations.convertAndSendToUser.

Doc from org.springframework.messaging.simp.SimpMessageSendingOperations.convertAndSendToUser.

Send a message to the given user.

See convertAndSend(Object, Object, java.util.Map) for important notes regarding the input headers.

Parameters
user:String

the user that should receive the message

destination:String

the destination to send the message to

payload:Object

the payload to send

headers:Map<String, Object>

the message headers

postProcessor:MessagePostProcessor

a postProcessor to post-process or modify the created message

Annotations
@Override
doSendback to summary
protected void doSend(String destination, Message<?> message)

Implements abstract org.springframework.messaging.core.AbstractMessageSendingTemplate.doSend.

Annotations
@Override
getHeaderInitializerback to summary
public MessageHeaderInitializer getHeaderInitializer()

Return the configured header initializer.

Annotations
@Nullable
getMessageChannelback to summary
public MessageChannel getMessageChannel()

Return the configured message channel.

getSendTimeoutback to summary
public long getSendTimeout()

Return the configured send timeout (in milliseconds).

getUserDestinationPrefixback to summary
public String getUserDestinationPrefix()

Return the configured user destination prefix.

initHeadersback to summary
private void initHeaders(SimpMessageHeaderAccessor simpAccessor)
processHeadersToSendback to summary
protected Map<String, Object> processHeadersToSend(Map<String, Object> headers)

Overrides org.springframework.messaging.core.AbstractMessageSendingTemplate.processHeadersToSend.

Creates a new map and puts the given headers under the key NATIVE_HEADERS.

Effectively treats the input header map as headers to be sent out to the destination.

However if the given headers already contain the key NATIVE_HEADERS then the same headers instance is returned without changes.

Also if the given headers were prepared and obtained with SimpMessageHeaderAccessor#getMessageHeaders() then the same headers instance is also returned without changes.

Parameters
headers:Map<String, Object>

Doc from org.springframework.messaging.core.AbstractMessageSendingTemplate.processHeadersToSend.

the headers to send (or null if none)

Returns:Map<String, Object>

Doc from org.springframework.messaging.core.AbstractMessageSendingTemplate.processHeadersToSend.

the actual headers to send (or null if none)

Annotations
@Override
sendback to summary
public void send(Message<?> message)

Overrides org.springframework.messaging.core.AbstractMessageSendingTemplate.send.

Implements org.springframework.messaging.core.MessageSendingOperations.send.

If the headers of the given message already contain a SimpMessageHeaderAccessor#DESTINATION_HEADER then the message is sent without further changes.

If a destination header is not already present ,the message is sent to the configured defaultDestination or an exception an IllegalStateException is raised if that isn't configured.

Parameters
message:Message<?>

the message to send (never null)

Annotations
@Override
sendInternalback to summary
private void sendInternal(Message<?> message)
setHeaderInitializerback to summary
public void setHeaderInitializer(MessageHeaderInitializer headerInitializer)

Configure a MessageHeaderInitializer to apply to the headers of all messages created through the SimpMessagingTemplate.

By default, this property is not set.

setSendTimeoutback to summary
public void setSendTimeout(long sendTimeout)

Specify the timeout value to use for send operations (in milliseconds).

setUserDestinationPrefixback to summary
public void setUserDestinationPrefix(String prefix)

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

The default value is "/user/".

See Also
org.springframework.messaging.simp.user.UserDestinationMessageHandler