org.springframework.messaging.simp

## Class SimpMessagingTemplate

- java.lang.Object
- - org.springframework.messaging.core.AbstractMessageSendingTemplate<String>
  - - org.springframework.messaging.simp.SimpMessagingTemplate

- All Implemented Interfaces:
    MessageSendingOperations<String>, SimpMessageSendingOperations

      public class SimpMessagingTemplate
      extends AbstractMessageSendingTemplate<String>
      implements SimpMessageSendingOperations

  A specialization of `AbstractMessageSendingTemplate` that interprets a String-based destination as the `DESTINATION_HEADER` to be added to the headers of sent messages.

  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

      `logger`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `SimpMessagingTemplate(MessageChannel messageChannel)` Create a new `SimpMessagingTemplate` instance.

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `void` | `convertAndSendToUser(String user, String destination, Object payload)` Send a message to a specific user.
    `void` | `convertAndSendToUser(String user, String destination, Object payload, Map<String,Object> headers)` Send a message to a specific user.
    `void` | `convertAndSendToUser(String user, String destination, Object payload, Map<String,Object> headers, MessagePostProcessor postProcessor)` Send a message to a specific user.
    `void` | `convertAndSendToUser(String user, String destination, Object payload, MessagePostProcessor postProcessor)` Send a message to a specific user.
    `protected void` | `doSend(String destination, Message<?> message)`
    `MessageChannel` | `getMessageChannel()`
    `long` | `getSendTimeout()`
    `String` | `getUserDestinationPrefix()`
    `protected Map<String,Object>` | `processHeadersToSend(Map<String,Object> headers)` Creates a new map and puts the given headers under the key `NATIVE_HEADERS`.
    `void` | `send(Message<?> message)` Send a message to a default destination.
    `void` | `setSendTimeout(long sendTimeout)` Specify the timeout value to use for send operations.
    `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, 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 Detail

    - #### SimpMessagingTemplate

          public SimpMessagingTemplate(MessageChannel messageChannel)

      Create a new `SimpMessagingTemplate` instance.
      Parameters:
        `messageChannel` - the message channel (must not be `null`)

  - ### Method Detail

    - #### setUserDestinationPrefix

          public void setUserDestinationPrefix(String prefix)

      Configure the prefix to use for destinations targeting a specific user.

      The default value is "/user/".

      See Also:
        `UserDestinationMessageHandler`

    - #### getUserDestinationPrefix

          public String getUserDestinationPrefix()

      Returns:
        the userDestinationPrefix

    - #### getMessageChannel

          public MessageChannel getMessageChannel()

      Returns:
        the messageChannel

    - #### setSendTimeout

          public void setSendTimeout(long sendTimeout)

      Specify the timeout value to use for send operations.
      Parameters:
        `sendTimeout` - the send timeout in milliseconds

    - #### getSendTimeout

          public long getSendTimeout()

      Returns:
        the sendTimeout

    - #### send

          public void send(Message<?> message)

      Description copied from interface: `MessageSendingOperations`
      Send a message to a default destination.
      Specified by:
        `send` in interface `MessageSendingOperations<String>`
      Overrides:
        `send` in class `AbstractMessageSendingTemplate<String>`
      Parameters:
        `message` - the message to send

    - #### doSend

          protected void doSend(String destination,
                                Message<?> message)

      Specified by:
        `doSend` in class `AbstractMessageSendingTemplate<String>`

    - #### convertAndSendToUser

          public void convertAndSendToUser(String user,
                                           String destination,
                                           Object payload)
                                    throws MessagingException

      Description copied from interface: `SimpMessageSendingOperations`
      Send a message to a specific user.
      Specified by:
        `convertAndSendToUser` in 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`

    - #### convertAndSendToUser

          public void convertAndSendToUser(String user,
                                           String destination,
                                           Object payload,
                                           Map<String,Object> headers)
                                    throws MessagingException

      Description copied from interface: `SimpMessageSendingOperations`
      Send a message to a specific user.
      Specified by:
        `convertAndSendToUser` in 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
      Throws:
        `MessagingException`

    - #### convertAndSendToUser

          public void convertAndSendToUser(String user,
                                           String destination,
                                           Object payload,
                                           MessagePostProcessor postProcessor)
                                    throws MessagingException

      Description copied from interface: `SimpMessageSendingOperations`
      Send a message to a specific user.
      Specified by:
        `convertAndSendToUser` in interface `SimpMessageSendingOperations`
      Parameters:
        `user` - the user that should receive the message.
        `destination` - the destination to send the message to.
        `payload` - the payload to send
        `postProcessor` - a postProcessor to post-process or modify the created message
      Throws:
        `MessagingException`

    - #### convertAndSendToUser

          public void convertAndSendToUser(String user,
                                           String destination,
                                           Object payload,
                                           Map<String,Object> headers,
                                           MessagePostProcessor postProcessor)
                                    throws MessagingException

      Description copied from interface: `SimpMessageSendingOperations`
      Send a message to a specific user.
      Specified by:
        `convertAndSendToUser` in 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`

    - #### processHeadersToSend

          protected Map<String,Object> processHeadersToSend(Map<String,Object> headers)

      Creates a new map and puts the given headers under the key `NATIVE_HEADERS`. Effectively this treats all given headers as headers to be sent out to the external source.

      If the given headers already contain the key `NATIVE_HEADERS` then the same header map is returned (i.e. without any changes).

      Overrides:
        `processHeadersToSend` in class `AbstractMessageSendingTemplate<String>`
      Parameters:
        `headers` - the headers to send, possibly `null`
      Returns:
        the actual headers to send