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 SummaryFields inherited from class org.springframework.messaging.core.AbstractMessageSendingTemplateCONVERSION_HINT_HEADER, logger
- 
Constructor SummaryConstructorsConstructorDescriptionSimpMessagingTemplate(MessageChannel messageChannel) Create a newSimpMessagingTemplateinstance.
- 
Method SummaryModifier and TypeMethodDescriptionvoidconvertAndSendToUser(String user, String destination, Object payload) Send a message to the given user.voidconvertAndSendToUser(String user, String destination, Object payload, @Nullable Map<String, Object> headers) Send a message to the given user.voidconvertAndSendToUser(String user, String destination, Object payload, @Nullable Map<String, Object> headers, @Nullable MessagePostProcessor postProcessor) Send a message to the given user.voidconvertAndSendToUser(String user, String destination, Object payload, @Nullable MessagePostProcessor postProcessor) Send a message to the given user.protected voidReturn the configured header initializer.Return the configured message channel.longReturn 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.voidIf the headers of the given message already contain aSimpMessageHeaderAccessor#DESTINATION_HEADERthen the message is sent without further changes.voidsetHeaderInitializer(@Nullable MessageHeaderInitializer headerInitializer) Configure aMessageHeaderInitializerto apply to the headers of all messages created through theSimpMessagingTemplate.voidsetSendTimeout(long sendTimeout) Specify the timeout value to use for send operations (in milliseconds).voidsetUserDestinationPrefix(String prefix) Configure the prefix to use for destinations targeting a specific user.Methods inherited from class org.springframework.messaging.core.AbstractMessageSendingTemplateconvertAndSend, convertAndSend, convertAndSend, convertAndSend, convertAndSend, convertAndSend, doConvert, getDefaultDestination, getMessageConverter, getRequiredDefaultDestination, send, setDefaultDestination, setMessageConverterMethods inherited from class java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.springframework.messaging.core.MessageSendingOperationsconvertAndSend, convertAndSend, convertAndSend, convertAndSend, convertAndSend, convertAndSend, send
- 
Constructor Details- 
SimpMessagingTemplateCreate a newSimpMessagingTemplateinstance.- Parameters:
- messageChannel- the message channel (never- null)
 
 
- 
- 
Method Details- 
getMessageChannelReturn the configured message channel.
- 
setUserDestinationPrefixConfigure the prefix to use for destinations targeting a specific user.The default value is "/user/". - See Also:
 
- 
getUserDestinationPrefixReturn the configured user destination prefix.
- 
setSendTimeoutpublic void setSendTimeout(long sendTimeout) Specify the timeout value to use for send operations (in milliseconds).
- 
getSendTimeoutpublic long getSendTimeout()Return the configured send timeout (in milliseconds).
- 
setHeaderInitializerConfigure aMessageHeaderInitializerto apply to the headers of all messages created through theSimpMessagingTemplate.By default, this property is not set. 
- 
getHeaderInitializerReturn the configured header initializer.
- 
sendIf the headers of the given message already contain aSimpMessageHeaderAccessor#DESTINATION_HEADERthen the message is sent without further changes.If a destination header is not already present ,the message is sent to the configured defaultDestinationor anIllegalStateExceptionis raised if that isn't configured.- Specified by:
- sendin interface- MessageSendingOperations<String>
- Overrides:
- sendin class- AbstractMessageSendingTemplate<String>
- Parameters:
- message- the message to send (never- null)
 
- 
doSend- Specified by:
- doSendin class- AbstractMessageSendingTemplate<String>
 
- 
convertAndSendToUserpublic void convertAndSendToUser(String user, String destination, Object payload) throws MessagingException Description copied from interface:SimpMessageSendingOperationsSend a message to the given user.- Specified by:
- convertAndSendToUserin interface- SimpMessageSendingOperations
- Parameters:
- user- the user that should receive the message.
- destination- the destination to send the message to.
- payload- the payload to send
- Throws:
- MessagingException
 
- 
convertAndSendToUserpublic void convertAndSendToUser(String user, String destination, Object payload, @Nullable Map<String, Object> headers) throws MessagingExceptionDescription copied from interface:SimpMessageSendingOperationsSend 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 withSimpMessageHeaderAccessorthen 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 MessageHeadersare 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:
- convertAndSendToUserin interface- SimpMessageSendingOperations
- Parameters:
- user- the user that should receive the message (must not be- null)
- destination- the destination to send the message to (must not be- null)
- payload- the payload to send (may be- null)
- headers- the message headers (may be- null)
- Throws:
- MessagingException
 
- 
convertAndSendToUserpublic void convertAndSendToUser(String user, String destination, Object payload, @Nullable MessagePostProcessor postProcessor) throws MessagingException Description copied from interface:SimpMessageSendingOperationsSend a message to the given user.- Specified by:
- convertAndSendToUserin interface- SimpMessageSendingOperations
- Parameters:
- user- the user that should receive the message (must not be- null)
- destination- the destination to send the message to (must not be- null)
- payload- the payload to send (may be- null)
- postProcessor- a postProcessor to post-process or modify the created message
- Throws:
- MessagingException
 
- 
convertAndSendToUserpublic void convertAndSendToUser(String user, String destination, Object payload, @Nullable Map<String, Object> headers, @Nullable MessagePostProcessor postProcessor) throws MessagingExceptionDescription copied from interface:SimpMessageSendingOperationsSend a message to the given user.See MessageSendingOperations.convertAndSend(Object, Object, java.util.Map)for important notes regarding the input headers.- Specified by:
- convertAndSendToUserin interface- SimpMessageSendingOperations
- Parameters:
- user- the user that should receive the message
- destination- the destination to send the message to
- payload- the payload to send
- headers- the message headers
- postProcessor- a postProcessor to post-process or modify the created message
- Throws:
- MessagingException
 
- 
processHeadersToSendCreates 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_HEADERSthen 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:
- processHeadersToSendin class- AbstractMessageSendingTemplate<String>
- Parameters:
- headers- the headers to send (or- nullif none)
- Returns:
- the actual headers to send (or nullif none)
 
 
-