Class AbstractBrokerMessageHandler

java.lang.Object
org.springframework.messaging.simp.broker.AbstractBrokerMessageHandler
All Implemented Interfaces:
Aware, ApplicationEventPublisherAware, Lifecycle, Phased, SmartLifecycle, MessageHandler
Direct Known Subclasses:
SimpleBrokerMessageHandler, StompBrokerRelayMessageHandler

public abstract class AbstractBrokerMessageHandler extends Object implements MessageHandler, ApplicationEventPublisherAware, SmartLifecycle
Abstract base class for a MessageHandler that broker messages to registered subscribers.
Since:
4.0
Author:
Rossen Stoyanchev
  • Field Details

    • logger

      protected final Log logger
  • Constructor Details

    • AbstractBrokerMessageHandler

      public AbstractBrokerMessageHandler(SubscribableChannel inboundChannel, MessageChannel outboundChannel, SubscribableChannel brokerChannel)
      Constructor with no destination prefixes (matches all destinations).
      Parameters:
      inboundChannel - the channel for receiving messages from clients (e.g. WebSocket clients)
      outboundChannel - the channel for sending messages to clients (e.g. WebSocket clients)
      brokerChannel - the channel for the application to send messages to the broker
    • AbstractBrokerMessageHandler

      public AbstractBrokerMessageHandler(SubscribableChannel inboundChannel, MessageChannel outboundChannel, SubscribableChannel brokerChannel, @Nullable Collection<String> destinationPrefixes)
      Constructor with destination prefixes to match to destinations of messages.
      Parameters:
      inboundChannel - the channel for receiving messages from clients (e.g. WebSocket clients)
      outboundChannel - the channel for sending messages to clients (e.g. WebSocket clients)
      brokerChannel - the channel for the application to send messages to the broker
      destinationPrefixes - prefixes to use to filter out messages
  • Method Details

    • getClientInboundChannel

      public SubscribableChannel getClientInboundChannel()
    • getClientOutboundChannel

      public MessageChannel getClientOutboundChannel()
    • getBrokerChannel

      public SubscribableChannel getBrokerChannel()
    • getDestinationPrefixes

      public Collection<String> getDestinationPrefixes()
      Return destination prefixes to use to filter messages to forward to the broker. Messages that have a destination and where the destination doesn't match are ignored.

      By default this is not set.

    • setUserDestinationPredicate

      public void setUserDestinationPredicate(@Nullable Predicate<String> predicate)
      Configure a Predicate to identify messages with a user destination. When no destination prefixes are configured, this helps to recognize and skip user destination messages that need to be pre-processed by the UserDestinationMessageHandler before they reach the broker.
      Parameters:
      predicate - the predicate to identify user messages with a non-null destination as messages with a user destinations.
      Since:
      5.3.4
    • setPreservePublishOrder

      public void setPreservePublishOrder(boolean preservePublishOrder)
      Whether the client must receive messages in the order of publication.

      By default messages sent to the "clientOutboundChannel" may not be processed in the same order because the channel is backed by a ThreadPoolExecutor that in turn does not guarantee processing in order.

      When this flag is set to true messages within the same session will be sent to the "clientOutboundChannel" one at a time in order to preserve the order of publication. Enable this only if needed since there is some performance overhead to keep messages in order.

      Parameters:
      preservePublishOrder - whether to publish in order
      Since:
      5.1
    • isPreservePublishOrder

      public boolean isPreservePublishOrder()
      Whether to ensure messages are received in the order of publication.
      Since:
      5.1
    • setApplicationEventPublisher

      public void setApplicationEventPublisher(@Nullable ApplicationEventPublisher publisher)
      Description copied from interface: ApplicationEventPublisherAware
      Set the ApplicationEventPublisher that this object runs in.

      Invoked after population of normal bean properties but before an init callback like InitializingBean's afterPropertiesSet or a custom init-method. Invoked before ApplicationContextAware's setApplicationContext.

      Specified by:
      setApplicationEventPublisher in interface ApplicationEventPublisherAware
      Parameters:
      publisher - event publisher to be used by this object
    • getApplicationEventPublisher

      @Nullable public ApplicationEventPublisher getApplicationEventPublisher()
    • setAutoStartup

      public void setAutoStartup(boolean autoStartup)
    • isAutoStartup

      public boolean isAutoStartup()
      Description copied from interface: SmartLifecycle
      Returns true if this Lifecycle component should get started automatically by the container at the time that the containing ApplicationContext gets refreshed.

      A value of false indicates that the component is intended to be started through an explicit Lifecycle.start() call instead, analogous to a plain Lifecycle implementation.

      The default implementation returns true.

      Specified by:
      isAutoStartup in interface SmartLifecycle
      See Also:
    • start

      public void start()
      Description copied from interface: Lifecycle
      Start this component.

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

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

      Specified by:
      start in interface Lifecycle
      See Also:
    • startInternal

      protected void startInternal()
    • stop

      public void stop()
      Description copied from interface: 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 in interface Lifecycle
      See Also:
    • stopInternal

      protected void stopInternal()
    • stop

      public final void stop(Runnable callback)
      Description copied from interface: 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 in interface SmartLifecycle
      See Also:
    • isRunning

      public final boolean isRunning()
      Check whether this message handler is currently running.

      Note that even when this message handler is running the isBrokerAvailable() flag may still independently alternate between being on and off depending on the concrete subclass implementation.

      Specified by:
      isRunning in interface Lifecycle
      Returns:
      whether the component is currently running
    • isBrokerAvailable

      public boolean isBrokerAvailable()
      Whether the message broker is currently available and able to process messages.

      Note that this is in addition to the isRunning() flag, which indicates whether this message handler is running. In other words the message handler must first be running and then the #isBrokerAvailable() flag may still independently alternate between being on and off depending on the concrete subclass implementation.

      Application components may implement org.springframework.context.ApplicationListener<BrokerAvailabilityEvent> to receive notifications when broker becomes available and unavailable.

    • handleMessage

      public void handleMessage(Message<?> message)
      Description copied from interface: MessageHandler
      Handle the given message.
      Specified by:
      handleMessage in interface MessageHandler
      Parameters:
      message - the message to be handled
    • handleMessageInternal

      protected abstract void handleMessageInternal(Message<?> message)
    • checkDestinationPrefix

      protected boolean checkDestinationPrefix(@Nullable String destination)
      Whether a message with the given destination should be processed. This is the case if one of the following conditions is true:
      1. The destination starts with one of the configured destination prefixes.
      2. No prefixes are configured and the destination isn't matched by the userDestinationPredicate.
      3. The message has no destination.
      Parameters:
      destination - the destination to check
      Returns:
      whether to process (true) or skip (false) the destination
    • publishBrokerAvailableEvent

      protected void publishBrokerAvailableEvent()
    • publishBrokerUnavailableEvent

      protected void publishBrokerUnavailableEvent()
    • getClientOutboundChannelForSession

      protected MessageChannel getClientOutboundChannelForSession(String sessionId)
      Get the MessageChannel to use for sending messages to clients, possibly a per-session wrapper when preservePublishOrder=true.
      Since:
      5.1