public class SimpMessagingTemplate extends AbstractMessageSendingTemplate<java.lang.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(java.lang.String user,
java.lang.String destination,
java.lang.Object payload)
Send a message to the given user.
|
void |
convertAndSendToUser(java.lang.String user,
java.lang.String destination,
java.lang.Object payload,
java.util.Map<java.lang.String,java.lang.Object> headers)
Send a message to the given user.
|
void |
convertAndSendToUser(java.lang.String user,
java.lang.String destination,
java.lang.Object payload,
java.util.Map<java.lang.String,java.lang.Object> headers,
MessagePostProcessor postProcessor)
Send a message to the given user.
|
void |
convertAndSendToUser(java.lang.String user,
java.lang.String destination,
java.lang.Object payload,
MessagePostProcessor postProcessor)
Send a message to the given user.
|
protected void |
doSend(java.lang.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).
|
java.lang.String |
getUserDestinationPrefix()
Return the configured user destination prefix.
|
protected java.util.Map<java.lang.String,java.lang.Object> |
processHeadersToSend(java.util.Map<java.lang.String,java.lang.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(java.lang.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(java.lang.String prefix)
The default value is "/user/".
UserDestinationMessageHandler
public java.lang.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<java.lang.String>
send
in class AbstractMessageSendingTemplate<java.lang.String>
message
- the message to send (never null
)protected void doSend(java.lang.String destination, Message<?> message)
doSend
in class AbstractMessageSendingTemplate<java.lang.String>
public void convertAndSendToUser(java.lang.String user, java.lang.String destination, java.lang.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(java.lang.String user, java.lang.String destination, java.lang.Object payload, @Nullable java.util.Map<java.lang.String,java.lang.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(java.lang.String user, java.lang.String destination, java.lang.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(java.lang.String user, java.lang.String destination, java.lang.Object payload, @Nullable java.util.Map<java.lang.String,java.lang.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 java.util.Map<java.lang.String,java.lang.Object> processHeadersToSend(@Nullable java.util.Map<java.lang.String,java.lang.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<java.lang.String>
headers
- the headers to send (or null
if none)null
if none)