类 SimpMessagingTemplate
- java.lang.Object
- org.springframework.messaging.core.AbstractMessageSendingTemplate<String>
- org.springframework.messaging.simp.SimpMessagingTemplate
public class SimpMessagingTemplate extends AbstractMessageSendingTemplate<String> implements SimpMessageSendingOperations
An implementation ofSimpMessageSendingOperations.Also provides methods for sending messages to a user. See
UserDestinationResolverfor more on user destinations.- 从以下版本开始:
- 4.0
- 作者:
- Rossen Stoyanchev
字段概要
从类继承的字段 org.springframework.messaging.core.AbstractMessageSendingTemplate
CONVERSION_HINT_HEADER, logger
构造器概要
构造器 构造器 说明 SimpMessagingTemplate(MessageChannel messageChannel)Create a newSimpMessagingTemplateinstance.
方法概要
所有方法 实例方法 具体方法 修饰符和类型 方法 说明 voidconvertAndSendToUser(String user, String destination, Object payload)Send a message to the given user.voidconvertAndSendToUser(String user, String destination, Object payload, Map<String,Object> headers)Send a message to the given user.voidconvertAndSendToUser(String user, String destination, Object payload, Map<String,Object> headers, MessagePostProcessor postProcessor)Send a message to the given user.voidconvertAndSendToUser(String user, String destination, Object payload, MessagePostProcessor postProcessor)Send a message to the given user.protected voiddoSend(String destination, Message<?> message)MessageHeaderInitializergetHeaderInitializer()Return the configured header initializer.MessageChannelgetMessageChannel()Return the configured message channel.longgetSendTimeout()Return the configured send timeout (in milliseconds).StringgetUserDestinationPrefix()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 keyNATIVE_HEADERS NATIVE_HEADERS NATIVE_HEADERS.voidsend(Message<?> message)If the headers of the given message already contain aSimpMessageHeaderAccessor#DESTINATION_HEADERthen the message is sent without further changes.voidsetHeaderInitializer(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.从类继承的方法 org.springframework.messaging.core.AbstractMessageSendingTemplate
convertAndSend, convertAndSend, convertAndSend, convertAndSend, convertAndSend, convertAndSend, doConvert, getDefaultDestination, getMessageConverter, getRequiredDefaultDestination, send, setDefaultDestination, setMessageConverter
从类继承的方法 java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
从接口继承的方法 org.springframework.messaging.core.MessageSendingOperations
convertAndSend, convertAndSend, convertAndSend, convertAndSend, convertAndSend, convertAndSend, send
构造器详细资料
SimpMessagingTemplate
public SimpMessagingTemplate(MessageChannel messageChannel)
Create a newSimpMessagingTemplateinstance.- 参数:
messageChannel- the message channel (nevernull)
方法详细资料
getMessageChannel
public MessageChannel getMessageChannel()
Return the configured message channel.
setUserDestinationPrefix
public void setUserDestinationPrefix(String prefix)
Configure the prefix to use for destinations targeting a specific user.The default value is "/user/".
getUserDestinationPrefix
public String 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
public void setHeaderInitializer(@Nullable MessageHeaderInitializer headerInitializer)
Configure aMessageHeaderInitializerto apply to the headers of all messages created through theSimpMessagingTemplate.By default, this property is not set.
getHeaderInitializer
@Nullable public MessageHeaderInitializer getHeaderInitializer()
Return the configured header initializer.
send
public void send(Message<?> message)
If 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 an exception anIllegalStateExceptionis raised if that isn't configured.- 指定者:
send在接口中MessageSendingOperations<String>- 覆盖:
send在类中AbstractMessageSendingTemplate<String>- 参数:
message- the message to send (nevernull)
doSend
protected void doSend(String destination, Message<?> message)
- 指定者:
doSend在类中AbstractMessageSendingTemplate<String>
convertAndSendToUser
public void convertAndSendToUser(String user, String destination, Object payload) throws MessagingException
从接口复制的说明:SimpMessageSendingOperationsSend a message to the given user.- 指定者:
convertAndSendToUser在接口中SimpMessageSendingOperations- 参数:
user- the user that should receive the message.destination- the destination to send the message to.payload- the payload to send- 抛出:
MessagingException
convertAndSendToUser
public void convertAndSendToUser(String user, String destination, Object payload, @Nullable Map<String,Object> headers) throws MessagingException
从接口复制的说明:SimpMessageSendingOperationsSend 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 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.- 指定者:
convertAndSendToUser在接口中SimpMessageSendingOperations- 参数:
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)- 抛出:
MessagingException
convertAndSendToUser
public void convertAndSendToUser(String user, String destination, Object payload, @Nullable MessagePostProcessor postProcessor) throws MessagingException
从接口复制的说明:SimpMessageSendingOperationsSend a message to the given user.- 指定者:
convertAndSendToUser在接口中SimpMessageSendingOperations- 参数:
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- 抛出:
MessagingException
convertAndSendToUser
public void convertAndSendToUser(String user, String destination, Object payload, @Nullable Map<String,Object> headers, @Nullable MessagePostProcessor postProcessor) throws MessagingException
从接口复制的说明:SimpMessageSendingOperationsSend a message to the given user.See
MessageSendingOperations.convertAndSend(Object, Object, java.util.Map)for important notes regarding the input headers.- 指定者:
convertAndSendToUser在接口中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 message- 抛出:
MessagingException
processHeadersToSend
protected Map<String,Object> processHeadersToSend(@Nullable Map<String,Object> headers)
Creates a new map and puts the given headers under the keyNATIVE_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_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.- 覆盖:
processHeadersToSend在类中AbstractMessageSendingTemplate<String>- 参数:
headers- the headers to send (ornullif none)- 返回:
- the actual headers to send (or
nullif none)