Class SimpMessagingTemplate
- All Implemented Interfaces:
MessageSendingOperations<String>
,SimpMessageSendingOperations
SimpMessageSendingOperations
.
Also provides methods for sending messages to a user. See
UserDestinationResolver
for more on user destinations.
- Since:
- 4.0
- Author:
- Rossen Stoyanchev
-
Field Summary
Fields inherited from class org.springframework.messaging.core.AbstractMessageSendingTemplate
CONVERSION_HINT_HEADER, logger
-
Constructor Summary
ConstructorDescriptionSimpMessagingTemplate
(MessageChannel messageChannel) Create a newSimpMessagingTemplate
instance. -
Method Summary
Modifier and TypeMethodDescriptionvoid
convertAndSendToUser
(String user, String destination, Object payload) Send a message to the given user.void
convertAndSendToUser
(String user, String destination, Object payload, @Nullable Map<String, Object> headers) Send a message to the given user.void
convertAndSendToUser
(String user, String destination, Object payload, @Nullable Map<String, Object> headers, @Nullable MessagePostProcessor postProcessor) Send a message to the given user.void
convertAndSendToUser
(String user, String destination, Object payload, @Nullable MessagePostProcessor postProcessor) Send a message to the given user.protected void
Return the configured header initializer.Return the configured message channel.long
Return the configured send timeout (in milliseconds).Return the configured user destination prefix.processHeadersToSend
(@Nullable Map<String, Object> headers) Creates a new map and puts the given headers under the keyNATIVE_HEADERS
.void
If the headers of the given message already contain aSimpMessageHeaderAccessor#DESTINATION_HEADER
then the message is sent without further changes.void
setHeaderInitializer
(@Nullable MessageHeaderInitializer headerInitializer) Configure aMessageHeaderInitializer
to apply to the headers of all messages created through theSimpMessagingTemplate
.void
setSendTimeout
(long sendTimeout) Specify the timeout value to use for send operations (in milliseconds).void
setUserDestinationPrefix
(String prefix) Configure the prefix to use for destinations targeting a specific user.Methods inherited from class org.springframework.messaging.core.AbstractMessageSendingTemplate
convertAndSend, convertAndSend, convertAndSend, convertAndSend, convertAndSend, convertAndSend, doConvert, getDefaultDestination, getMessageConverter, getRequiredDefaultDestination, send, setDefaultDestination, setMessageConverter
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.springframework.messaging.core.MessageSendingOperations
convertAndSend, convertAndSend, convertAndSend, convertAndSend, convertAndSend, convertAndSend, send
-
Constructor Details
-
SimpMessagingTemplate
Create a newSimpMessagingTemplate
instance.- Parameters:
messageChannel
- the message channel (nevernull
)
-
-
Method Details
-
getMessageChannel
Return the configured message channel. -
setUserDestinationPrefix
Configure the prefix to use for destinations targeting a specific user.The default value is "/user/".
- See Also:
-
getUserDestinationPrefix
Return the configured user destination prefix. -
setSendTimeout
public void setSendTimeout(long sendTimeout) Specify the timeout value to use for send operations (in milliseconds). -
getSendTimeout
public long getSendTimeout()Return the configured send timeout (in milliseconds). -
setHeaderInitializer
Configure aMessageHeaderInitializer
to apply to the headers of all messages created through theSimpMessagingTemplate
.By default, this property is not set.
-
getHeaderInitializer
Return the configured header initializer. -
send
If the headers of the given message already contain aSimpMessageHeaderAccessor#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 anIllegalStateException
is raised if that isn't configured.- Specified by:
send
in interfaceMessageSendingOperations<String>
- Overrides:
send
in classAbstractMessageSendingTemplate<String>
- Parameters:
message
- the message to send (nevernull
)
-
doSend
- Specified by:
doSend
in classAbstractMessageSendingTemplate<String>
-
convertAndSendToUser
public void convertAndSendToUser(String user, String destination, Object payload) throws MessagingException Description copied from interface:SimpMessageSendingOperations
Send a message to the given user.- Specified by:
convertAndSendToUser
in interfaceSimpMessageSendingOperations
- Parameters:
user
- the user that should receive the message.destination
- the destination to send the message to.payload
- the payload to send- Throws:
MessagingException
-
convertAndSendToUser
public void convertAndSendToUser(String user, String destination, Object payload, @Nullable Map<String, Object> headers) throws MessagingExceptionDescription copied from interface:SimpMessageSendingOperations
Send a message to the given user.By default headers are interpreted as native headers (for example, 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 (for example, the STOMP client or broker).If the map already contains the key
"nativeHeaders"
or was prepared withSimpMessageHeaderAccessor
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.- Specified by:
convertAndSendToUser
in interfaceSimpMessageSendingOperations
- Parameters:
user
- the user that should receive the message (must not benull
)destination
- the destination to send the message to (must not benull
)payload
- the payload to send (may benull
)headers
- the message headers (may benull
)- Throws:
MessagingException
-
convertAndSendToUser
public void convertAndSendToUser(String user, String destination, Object payload, @Nullable MessagePostProcessor postProcessor) throws MessagingException Description copied from interface:SimpMessageSendingOperations
Send a message to the given user.- Specified by:
convertAndSendToUser
in interfaceSimpMessageSendingOperations
- Parameters:
user
- the user that should receive the message (must not benull
)destination
- the destination to send the message to (must not benull
)payload
- the payload to send (may benull
)postProcessor
- a postProcessor to post-process or modify the created message- Throws:
MessagingException
-
convertAndSendToUser
public void convertAndSendToUser(String user, String destination, Object payload, @Nullable Map<String, Object> headers, @Nullable MessagePostProcessor postProcessor) throws MessagingExceptionDescription copied from interface:SimpMessageSendingOperations
Send a message to the given user.See
MessageSendingOperations.convertAndSend(Object, Object, java.util.Map)
for important notes regarding the input headers.- Specified by:
convertAndSendToUser
in interfaceSimpMessageSendingOperations
- Parameters:
user
- the user that should receive the messagedestination
- the destination to send the message topayload
- the payload to sendheaders
- the message headerspostProcessor
- a postProcessor to post-process or modify the created message- Throws:
MessagingException
-
processHeadersToSend
Creates a new map and puts the given headers under the keyNATIVE_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
MessageHeaderAccessor.getMessageHeaders()
then the same headers instance is also returned without changes.- Overrides:
processHeadersToSend
in classAbstractMessageSendingTemplate<String>
- Parameters:
headers
- the headers to send (ornull
if none)- Returns:
- the actual headers to send (or
null
if none)
-