public class SimpMessagingTemplate extends AbstractMessageSendingTemplate<String> implements SimpMessageSendingOperations
SimpMessageSendingOperations
.
Also provides methods for sending messages to a user. See
UserDestinationResolver
for more on user destinations.
CONVERSION_HINT_HEADER, logger
Constructor and Description |
---|
SimpMessagingTemplate(MessageChannel messageChannel)
Create a new
SimpMessagingTemplate instance. |
Modifier and Type | Method and Description |
---|---|
void |
convertAndSendToUser(String user,
String destination,
Object payload)
Send a message to the given user.
|
void |
convertAndSendToUser(String user,
String destination,
Object payload,
Map<String,Object> headers)
Send a message to the given user.
|
void |
convertAndSendToUser(String user,
String destination,
Object payload,
Map<String,Object> headers,
MessagePostProcessor postProcessor)
Send a message to the given user.
|
void |
convertAndSendToUser(String user,
String destination,
Object payload,
MessagePostProcessor postProcessor)
Send a message to the given user.
|
protected void |
doSend(String destination,
Message<?> message) |
MessageHeaderInitializer |
getHeaderInitializer()
Return the configured header initializer.
|
MessageChannel |
getMessageChannel()
Return the configured message channel.
|
long |
getSendTimeout()
Return the configured send timeout (in milliseconds).
|
String |
getUserDestinationPrefix()
Return the configured user destination prefix.
|
protected Map<String,Object> |
processHeadersToSend(Map<String,Object> headers)
Creates a new map and puts the given headers under the key
NATIVE_HEADERS NATIVE_HEADERS NATIVE_HEADERS . |
void |
send(Message<?> message)
If the headers of the given message already contain a
SimpMessageHeaderAccessor#DESTINATION_HEADER then the message is sent without
further changes. |
void |
setHeaderInitializer(MessageHeaderInitializer headerInitializer)
Configure a
MessageHeaderInitializer to apply to the headers of all
messages created through the SimpMessagingTemplate . |
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.
|
convertAndSend, convertAndSend, convertAndSend, convertAndSend, convertAndSend, convertAndSend, doConvert, getDefaultDestination, getMessageConverter, getRequiredDefaultDestination, send, setDefaultDestination, setMessageConverter
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
convertAndSend, convertAndSend, convertAndSend, convertAndSend, convertAndSend, convertAndSend, send
public SimpMessagingTemplate(MessageChannel messageChannel)
SimpMessagingTemplate
instance.messageChannel
- the message channel (never null
)public MessageChannel getMessageChannel()
public void setUserDestinationPrefix(String prefix)
The default value is "/user/".
UserDestinationMessageHandler
public String getUserDestinationPrefix()
public void setSendTimeout(long sendTimeout)
public long getSendTimeout()
public void setHeaderInitializer(@Nullable MessageHeaderInitializer headerInitializer)
MessageHeaderInitializer
to apply to the headers of all
messages created through the SimpMessagingTemplate
.
By default, this property is not set.
@Nullable public MessageHeaderInitializer getHeaderInitializer()
public void send(Message<?> message)
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.
send
in interface MessageSendingOperations<String>
send
in class AbstractMessageSendingTemplate<String>
message
- the message to send (never null
)protected void doSend(String destination, Message<?> message)
doSend
in class AbstractMessageSendingTemplate<String>
public void convertAndSendToUser(String user, String destination, Object payload) throws MessagingException
SimpMessageSendingOperations
convertAndSendToUser
in interface SimpMessageSendingOperations
user
- the user that should receive the message.destination
- the destination to send the message to.payload
- the payload to sendMessagingException
public void convertAndSendToUser(String user, String destination, Object payload, @Nullable Map<String,Object> headers) throws MessagingException
SimpMessageSendingOperations
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.
convertAndSendToUser
in interface SimpMessageSendingOperations
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
)MessagingException
public void convertAndSendToUser(String user, String destination, Object payload, @Nullable MessagePostProcessor postProcessor) throws MessagingException
SimpMessageSendingOperations
convertAndSendToUser
in interface SimpMessageSendingOperations
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 messageMessagingException
public void convertAndSendToUser(String user, String destination, Object payload, @Nullable Map<String,Object> headers, @Nullable MessagePostProcessor postProcessor) throws MessagingException
SimpMessageSendingOperations
See MessageSendingOperations.convertAndSend(Object, Object, java.util.Map)
for important
notes regarding the input headers.
convertAndSendToUser
in interface SimpMessageSendingOperations
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 messageMessagingException
protected Map<String,Object> processHeadersToSend(@Nullable Map<String,Object> headers)
NATIVE_HEADERS NATIVE_HEADERS 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 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.
processHeadersToSend
in class AbstractMessageSendingTemplate<String>
headers
- the headers to send (or null
if none)null
if none)