org.springframework.messaging.simp.broker

## Class AbstractBrokerMessageHandler

- java.lang.Object
- - org.springframework.messaging.simp.broker.AbstractBrokerMessageHandler

All Implemented Interfaces:
  org.springframework.beans.factory.Aware, org.springframework.context.ApplicationEventPublisherAware, org.springframework.context.Lifecycle, org.springframework.context.Phased, org.springframework.context.SmartLifecycle, MessageHandler

Direct Known Subclasses:
  SimpleBrokerMessageHandler, StompBrokerRelayMessageHandler

    public abstract class AbstractBrokerMessageHandler
    extends Object
    implements MessageHandler, org.springframework.context.ApplicationEventPublisherAware, org.springframework.context.SmartLifecycle

Abstract base class for a `MessageHandler` that broker messages to registered subscribers.

Since:
  4.0
Author:
  Rossen Stoyanchev

### Field Summary

Fields
`protected Log` `logger`

### Fields inherited from interface org.springframework.context.SmartLifecycle

`DEFAULT_PHASE`

### Constructor Summary

Constructors
Constructor and Description
`AbstractBrokerMessageHandler(SubscribableChannel inboundChannel, MessageChannel outboundChannel, SubscribableChannel brokerChannel)` Constructor with no destination prefixes (matches all destinations).
`AbstractBrokerMessageHandler(SubscribableChannel inboundChannel, MessageChannel outboundChannel, SubscribableChannel brokerChannel, Collection<String> destinationPrefixes)` Constructor with destination prefixes to match to destinations of messages.

### Method Summary

All Methods Instance Methods Abstract Methods Concrete Methods
`protected boolean` `checkDestinationPrefix(String destination)`
`org.springframework.context.ApplicationEventPublisher` `getApplicationEventPublisher()`
`SubscribableChannel` `getBrokerChannel()`
`SubscribableChannel` `getClientInboundChannel()`
`MessageChannel` `getClientOutboundChannel()`
`protected MessageChannel` `getClientOutboundChannelForSession(String sessionId)` Get the MessageChannel to use for sending messages to clients, possibly a per-session wrapper when `preservePublishOrder=true`.
`Collection<String>` `getDestinationPrefixes()`
`void` `handleMessage(Message<?> message)` Handle the given message.
`protected abstract void` `handleMessageInternal(Message<?> message)`
`boolean` `isAutoStartup()`
`boolean` `isBrokerAvailable()` Whether the message broker is currently available and able to process messages.
`boolean` `isPreservePublishOrder()` Whether to ensure messages are received in the order of publication.
`boolean` `isRunning()` Check whether this message handler is currently running.
`protected void` `publishBrokerAvailableEvent()`
`protected void` `publishBrokerUnavailableEvent()`
`void` `setApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher publisher)`
`void` `setAutoStartup(boolean autoStartup)`
`void` `setPreservePublishOrder(boolean preservePublishOrder)` Whether the client must receive messages in the order of publication.
`void` `start()`
`protected void` `startInternal()`
`void` `stop()`
`void` `stop(Runnable callback)`
`protected void` `stopInternal()`

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

### Methods inherited from interface org.springframework.context.SmartLifecycle

`getPhase`

### Field Detail

#### logger

    protected final Log logger

### Constructor Detail

#### 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 Detail

#### getClientInboundChannel

    public SubscribableChannel getClientInboundChannel()

#### getClientOutboundChannel

    public MessageChannel getClientOutboundChannel()

#### getBrokerChannel

    public SubscribableChannel getBrokerChannel()

#### getDestinationPrefixes

    public Collection<String> getDestinationPrefixes()

#### 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
                                             org.springframework.context.ApplicationEventPublisher publisher)

Specified by:
  `setApplicationEventPublisher` in interface `org.springframework.context.ApplicationEventPublisherAware`

#### getApplicationEventPublisher

    @Nullable
    public org.springframework.context.ApplicationEventPublisher getApplicationEventPublisher()

#### setAutoStartup

    public void setAutoStartup(boolean autoStartup)

#### isAutoStartup

    public boolean isAutoStartup()

Specified by:
  `isAutoStartup` in interface `org.springframework.context.SmartLifecycle`

#### start

    public void start()

Specified by:
  `start` in interface `org.springframework.context.Lifecycle`

#### startInternal

    protected void startInternal()

#### stop

    public void stop()

Specified by:
  `stop` in interface `org.springframework.context.Lifecycle`

#### stopInternal

    protected void stopInternal()

#### stop

    public final void stop(Runnable callback)

Specified by:
  `stop` in interface `org.springframework.context.SmartLifecycle`

#### 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 sub-class implementation.

Specified by:
  `isRunning` in interface `org.springframework.context.Lifecycle`

#### 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 sub-class implementation.

Application components may implement `org.springframework.context.ApplicationListener&lt;BrokerAvailabilityEvent&gt;` 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)

#### 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