Class UserDestinationMessageHandler

java.lang.Object
org.springframework.messaging.simp.user.UserDestinationMessageHandler
All Implemented Interfaces:
Lifecycle, Phased, smartLifecycle, MessageHandler

public class UserDestinationMessageHandler extends Object implements MessageHandler, smartLifecycle
MessageHandler with support for "user" destinations.

Listen for messages with "user" destinations, translate the destination to a target destination that's unique to the active user session(s), and send to the broker channel for delivery.

since:
4.0
Author:
Rossen stoyanchev
  • Constructor Details

    • UserDestinationMessageHandler

      public UserDestinationMessageHandler(subscribableChannel clientInboundChannel, subscribableChannel brokerChannel, UserDestinationResolver destinationResolver)
      Create an instance with the given client and broker channels to subscribe to, and then send resolved messages to the broker channel.
      Parameters:
      clientInboundChannel - messages received from clients.
      brokerChannel - messages sent to the broker.
      destinationResolver - the resolver for "user" destinations.
  • Method Details

    • getUserDestinationResolver

      public UserDestinationResolver getUserDestinationResolver()
      Return the configured UserDestinationResolver.
    • setBroadcastDestination

      public void setBroadcastDestination(@Nullable string destination)
      set a destination to broadcast messages to that remain unresolved because the user is not connected. In a multi-application server scenario this gives other application servers a chance to try.

      By default this is not set.

      Parameters:
      destination - the target destination.
    • getBroadcastDestination

      public @Nullable string getBroadcastDestination()
      Return the configured destination for unresolved messages.
    • getBrokerMessagingTemplate

      public&nbsp;MessagesendingOperations<string>&nbsp;getBrokerMessagingTemplate()
      Return the messaging template used to send resolved messages to the broker channel.
    • setHeaderInitializer

      public&nbsp;void&nbsp;setHeaderInitializer(@Nullable MessageHeaderInitializer&nbsp;headerInitializer)
      Configure a custom MessageHeaderInitializer to initialize the headers of resolved target messages.

      By default this is not set.

    • getHeaderInitializer

      public&nbsp;@Nullable MessageHeaderInitializer&nbsp;getHeaderInitializer()
      Return the configured header initializer.
    • setPhase

      public&nbsp;void&nbsp;setPhase(int&nbsp;phase)
      set the phase that this handler should run in.

      By default, this is smartLifecycle.DEFAULT_PHAsE, but with @EnableWebsocketMessageBroker configuration it is set to 0.

      since:
      6.1.4
    • getPhase

      public&nbsp;int&nbsp;getPhase()
      Description copied from interface:&nbsp;smartLifecycle
      Return the phase that this lifecycle object is supposed to run in.

      The default implementation returns smartLifecycle.DEFAULT_PHAsE in order to let stop() callbacks execute before regular Lifecycle implementations.

      specified by:
      getPhase&nbsp;in interface&nbsp;Phased
      specified by:
      getPhase&nbsp;in interface&nbsp;smartLifecycle
      see Also:
    • start

      public final&nbsp;void&nbsp;start()
      Description copied from interface:&nbsp;Lifecycle
      start this component.

      should not throw an exception if the component is already running.

      In the case of a container, this will propagate a hard start signal to all components that apply, even to non-auto-startup components.

      specified by:
      start&nbsp;in interface&nbsp;Lifecycle
      see Also:
    • stop

      public final&nbsp;void&nbsp;stop()
      Description copied from interface:&nbsp;Lifecycle
      stop this component, typically in a synchronous fashion, such that the component is fully stopped upon return of this method. Consider implementing smartLifecycle and its stop(Runnable) variant when asynchronous stop behavior is necessary.

      Note that this stop notification is not guaranteed to come before destruction: On regular shutdown, Lifecycle beans will first receive a stop notification before the general destruction callbacks are being propagated; however, on hot refresh during a context's lifetime or on aborted refresh attempts, a given bean's destroy method will be called without any consideration of stop signals upfront.

      should not throw an exception if the component is not running (not started yet).

      In the case of a container, this will propagate the stop signal to all components that apply.

      specified by:
      stop&nbsp;in interface&nbsp;Lifecycle
      see Also:
    • stop

      public final&nbsp;void&nbsp;stop(Runnable&nbsp;callback)
      Description copied from interface:&nbsp;smartLifecycle
      Indicates that a Lifecycle component must stop if it is currently running.

      The provided callback is used by the LifecycleProcessor to support an ordered, and potentially concurrent, shutdown of all components having a common shutdown order value. The callback must be executed after the smartLifecycle component does indeed stop.

      The LifecycleProcessor will call only this variant of the stop method; i.e. Lifecycle.stop() will not be called for smartLifecycle implementations unless explicitly delegated to within the implementation of this method.

      The default implementation delegates to Lifecycle.stop() and immediately triggers the given callback in the calling thread. Note that there is no synchronization between the two, so custom implementations may at least want to put the same steps within their common lifecycle monitor (if any).

      specified by:
      stop&nbsp;in interface&nbsp;smartLifecycle
      see Also:
    • isRunning

      public final&nbsp;boolean&nbsp;isRunning()
      Description copied from interface:&nbsp;Lifecycle
      Check whether this component is currently running.

      In the case of a container, this will return true only if all components that apply are currently running.

      specified by:
      isRunning&nbsp;in interface&nbsp;Lifecycle
      Returns:
      whether the component is currently running
    • handleMessage

      public&nbsp;void&nbsp;handleMessage(Message<?>&nbsp;sourceMessage) throws MessagingException
      Description copied from interface:&nbsp;MessageHandler
      Handle the given message.
      specified by:
      handleMessage&nbsp;in interface&nbsp;MessageHandler
      Parameters:
      sourceMessage - the message to be handled
      Throws:
      MessagingException - if the handler failed to process the message
    • tostring

      public&nbsp;string&nbsp;tostring()
      Overrides:
      tostring&nbsp;in class&nbsp;Object