Top Description Inners Fields Constructors Methods
org.springframework.messaging.simp.broker

public abstract Class AbstractBrokerMessageHandler

extends Object
implements MessageHandler, ApplicationEventPublisherAware, SmartLifecycle
Class Inheritance
All Implemented Interfaces
org.springframework.context.SmartLifecycle, org.springframework.context.Phased, org.springframework.context.Lifecycle, org.springframework.context.ApplicationEventPublisherAware, org.springframework.beans.factory.Aware, org.springframework.messaging.MessageHandler
Known Direct Subclasses
org.springframework.messaging.simp.broker.SimpleBrokerMessageHandler, org.springframework.messaging.simp.stomp.StompBrokerRelayMessageHandler
Imports
java.util.Collection, .Collections, java.util.concurrent.atomic.AtomicBoolean, java.util.function.Predicate, org.apache.commons.logging.Log, org.springframework.context.ApplicationEventPublisher, .ApplicationEventPublisherAware, .SmartLifecycle, org.springframework.lang.Nullable, org.springframework.messaging.Message, .MessageChannel, .MessageHandler, .SubscribableChannel, org.springframework.messaging.simp.SimpLogging, .SimpMessageHeaderAccessor, .SimpMessageType, org.springframework.messaging.support.ChannelInterceptor, .InterceptableChannel, org.springframework.util.Assert, .CollectionUtils

Abstract base class for a MessageHandler that broker messages to registered subscribers.
Author
Rossen Stoyanchev
Since
4.0

Nested and Inner Type Summary

Modifier and TypeClass and Description
private class
AbstractBrokerMessageHandler.UnsentDisconnectChannelInterceptor

Detect unsent DISCONNECT messages and process them anyway.

Field Summary

Modifier and TypeField and Description
private boolean
private final BrokerAvailabilityEvent
private final AtomicBoolean
private final SubscribableChannel
private final SubscribableChannel
private final MessageChannel
private final Collection<String>
private ApplicationEventPublisher
private final Object
protected final Log
private final BrokerAvailabilityEvent
private Integer
private boolean
private volatile boolean
private final ChannelInterceptor
private Predicate<String>

Constructor Summary

AccessConstructor and Description
public
AbstractBrokerMessageHandler(SubscribableChannel
the channel for receiving messages from clients (e.g. WebSocket clients)
inboundChannel
,
MessageChannel
the channel for sending messages to clients (e.g. WebSocket clients)
outboundChannel
,
SubscribableChannel
the channel for the application to send messages to the broker
brokerChannel
)

Constructor with no destination prefixes (matches all destinations).

public
AbstractBrokerMessageHandler(SubscribableChannel
the channel for receiving messages from clients (e.g. WebSocket clients)
inboundChannel
,
MessageChannel
the channel for sending messages to clients (e.g. WebSocket clients)
outboundChannel
,
SubscribableChannel
the channel for the application to send messages to the broker
brokerChannel
,
Collection<String>
prefixes to use to filter out messages
destinationPrefixes
)

Constructor with destination prefixes to match to destinations of messages.

Method Summary

Modifier and TypeMethod and Description
protected boolean

Returns:

whether to process (true) or skip (false) the destination
checkDestinationPrefix
(String
the destination to check
destination
)

Whether a message with the given destination should be processed.

public ApplicationEventPublisher
public SubscribableChannel
public SubscribableChannel
public MessageChannel
protected MessageChannel
getClientOutboundChannelForSession(String sessionId)

Get the MessageChannel to use for sending messages to clients, possibly a per-session wrapper when preservePublishOrder=true.

public Collection<String>
getDestinationPrefixes()

Return destination prefixes to use to filter messages to forward to the broker.

public int
getPhase()

Overrides default org.springframework.context.SmartLifecycle.getPhase.

Return the phase that this lifecycle object is supposed to run in.

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

public void
handleMessage(Message<?>
the message to be handled
message
)

Implements org.springframework.messaging.MessageHandler.handleMessage.

Handle the given message.

protected abstract void
public boolean
isAutoStartup()

Overrides default org.springframework.context.SmartLifecycle.isAutoStartup.

Returns true if this Lifecycle component should get started automatically by the container at the time that the containing ApplicationContext gets refreshed.

public boolean
isBrokerAvailable()

Whether the message broker is currently available and able to process messages.

public boolean
isPreservePublishOrder()

Whether to ensure messages are received in the order of publication.

public final boolean
isRunning()

Implements org.springframework.context.Lifecycle.isRunning.

Check whether this message handler is currently running.

private boolean
protected void
protected void
public void
setApplicationEventPublisher(ApplicationEventPublisher
event publisher to be used by this object
publisher
)

Implements org.springframework.context.ApplicationEventPublisherAware.setApplicationEventPublisher.

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.

public void
setAutoStartup(boolean autoStartup)

public void
setPhase(int phase)

Set the phase that this handler should run in.

By default, this is SmartLifecycle#DEFAULT_PHASE.

public void
setPreservePublishOrder(boolean
whether to publish in order
preservePublishOrder
)

Whether the client must receive messages in the order of publication.

public void
setUserDestinationPredicate(Predicate<String>
the predicate to identify user messages with a non-null destination as messages with a user destinations.
predicate
)

Configure a Predicate to identify messages with a user destination.

public void
start()

Implements org.springframework.context.Lifecycle.start.

Start this component.

protected void
public void
stop()

Implements org.springframework.context.Lifecycle.stop.

Stop this component, typically in a synchronous fashion, such that the component is fully stopped upon return of this method.

public final void
stop(Runnable callback)

Overrides default org.springframework.context.SmartLifecycle.stop.

Indicates that a Lifecycle component must stop if it is currently running.

protected void
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

autoStartupback to summary
private boolean autoStartup
availableEventback to summary
private final BrokerAvailabilityEvent availableEvent
brokerAvailableback to summary
private final AtomicBoolean brokerAvailable
brokerChannelback to summary
private final SubscribableChannel brokerChannel
clientInboundChannelback to summary
private final SubscribableChannel clientInboundChannel
clientOutboundChannelback to summary
private final MessageChannel clientOutboundChannel
destinationPrefixesback to summary
private final Collection<String> destinationPrefixes
eventPublisherback to summary
private ApplicationEventPublisher eventPublisher
Annotations
@Nullable
lifecycleMonitorback to summary
private final Object lifecycleMonitor
loggerback to summary
protected final Log logger
notAvailableEventback to summary
private final BrokerAvailabilityEvent notAvailableEvent
phaseback to summary
private Integer phase
Annotations
@Nullable
preservePublishOrderback to summary
private boolean preservePublishOrder
runningback to summary
private volatile boolean running
unsentDisconnectInterceptorback to summary
private final ChannelInterceptor unsentDisconnectInterceptor
userDestinationPredicateback to summary
private Predicate<String> userDestinationPredicate
Annotations
@Nullable

Constructor Detail

AbstractBrokerMessageHandlerback to summary
public AbstractBrokerMessageHandler(SubscribableChannel inboundChannel, MessageChannel outboundChannel, SubscribableChannel brokerChannel)

Constructor with no destination prefixes (matches all destinations).

Parameters
inboundChannel:SubscribableChannel

the channel for receiving messages from clients (e.g. WebSocket clients)

outboundChannel:MessageChannel

the channel for sending messages to clients (e.g. WebSocket clients)

brokerChannel:SubscribableChannel

the channel for the application to send messages to the broker

AbstractBrokerMessageHandlerback to summary
public AbstractBrokerMessageHandler(SubscribableChannel inboundChannel, MessageChannel outboundChannel, SubscribableChannel brokerChannel, Collection<String> destinationPrefixes)

Constructor with destination prefixes to match to destinations of messages.

Parameters
inboundChannel:SubscribableChannel

the channel for receiving messages from clients (e.g. WebSocket clients)

outboundChannel:MessageChannel

the channel for sending messages to clients (e.g. WebSocket clients)

brokerChannel:SubscribableChannel

the channel for the application to send messages to the broker

destinationPrefixes:Collection<String>

prefixes to use to filter out messages

Method Detail

checkDestinationPrefixback to summary
protected boolean checkDestinationPrefix(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:String

the destination to check

Returns:boolean

whether to process (true) or skip (false) the destination

getApplicationEventPublisherback to summary
public ApplicationEventPublisher getApplicationEventPublisher()
Annotations
@Nullable
getBrokerChannelback to summary
public SubscribableChannel getBrokerChannel()
getClientInboundChannelback to summary
public SubscribableChannel getClientInboundChannel()
getClientOutboundChannelback to summary
public MessageChannel getClientOutboundChannel()
getClientOutboundChannelForSessionback to summary
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
getDestinationPrefixesback to summary
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.

getPhaseback to summary
public int getPhase()

Overrides default org.springframework.context.SmartLifecycle.getPhase.

Doc from org.springframework.context.SmartLifecycle.getPhase.

Return the phase that this lifecycle object is supposed to run in.

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

Annotations
@Override
handleMessageback to summary
public void handleMessage(Message<?> message)

Implements org.springframework.messaging.MessageHandler.handleMessage.

Doc from org.springframework.messaging.MessageHandler.handleMessage.

Handle the given message.

Parameters
message:Message<?>

the message to be handled

Annotations
@Override
handleMessageInternalback to summary
protected abstract void handleMessageInternal(Message<?> message)
isAutoStartupback to summary
public boolean isAutoStartup()

Overrides default org.springframework.context.SmartLifecycle.isAutoStartup.

Doc from org.springframework.context.SmartLifecycle.isAutoStartup.

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 start() call instead, analogous to a plain Lifecycle implementation.

The default implementation returns true.

Annotations
@Override
isBrokerAvailableback to summary
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.

isPreservePublishOrderback to summary
public boolean isPreservePublishOrder()

Whether to ensure messages are received in the order of publication.

Since
5.1
isRunningback to summary
public final boolean isRunning()

Implements org.springframework.context.Lifecycle.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.

Returns:boolean

Doc from org.springframework.context.Lifecycle.isRunning.

whether the component is currently running

Annotations
@Override
isUserDestinationback to summary
private boolean isUserDestination(String destination)
publishBrokerAvailableEventback to summary
protected void publishBrokerAvailableEvent()
publishBrokerUnavailableEventback to summary
protected void publishBrokerUnavailableEvent()
setApplicationEventPublisherback to summary
public void setApplicationEventPublisher(ApplicationEventPublisher publisher)

Implements org.springframework.context.ApplicationEventPublisherAware.setApplicationEventPublisher.

Doc from org.springframework.context.ApplicationEventPublisherAware.setApplicationEventPublisher.

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.

Parameters
publisher:ApplicationEventPublisher

event publisher to be used by this object

Annotations
@Override
setAutoStartupback to summary
public void setAutoStartup(boolean autoStartup)
setPhaseback to summary
public void setPhase(int phase)

Set the phase that this handler should run in.

By default, this is SmartLifecycle#DEFAULT_PHASE.

Since
6.1.4
setPreservePublishOrderback to summary
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.

Parameters
preservePublishOrder:boolean

whether to publish in order

Since
5.1
setUserDestinationPredicateback to summary
public void setUserDestinationPredicate(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 org.springframework.messaging.simp.user.UserDestinationMessageHandler before they reach the broker.

Parameters
predicate:Predicate<String>

the predicate to identify user messages with a non-null destination as messages with a user destinations.

Since
5.3.4
startback to summary
public void start()

Implements org.springframework.context.Lifecycle.start.

Doc from org.springframework.context.Lifecycle.start.

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.

Annotations
@Override
startInternalback to summary
protected void startInternal()
stopback to summary
public void stop()

Implements org.springframework.context.Lifecycle.stop.

Doc from org.springframework.context.Lifecycle.stop.

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.

Annotations
@Override
stopback to summary
public final void stop(Runnable callback)

Overrides default org.springframework.context.SmartLifecycle.stop.

Doc from org.springframework.context.SmartLifecycle.stop.

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 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).

Annotations
@Override
stopInternalback to summary
protected void stopInternal()
org.springframework.messaging.simp.broker back to summary

private Class AbstractBrokerMessageHandler.UnsentDisconnectChannelInterceptor

extends Object
implements ChannelInterceptor
Class Inheritance
  • java.lang.Object
  • org.springframework.messaging.simp.broker.AbstractBrokerMessageHandler.UnsentDisconnectChannelInterceptor
All Implemented Interfaces
org.springframework.messaging.support.ChannelInterceptor

Detect unsent DISCONNECT messages and process them anyway.

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public void
afterSendCompletion(Message<?> message, MessageChannel channel, boolean sent, Exception ex)

Overrides default org.springframework.messaging.support.ChannelInterceptor.afterSendCompletion.

Invoked after the completion of a send regardless of any exception that have been raised thus allowing for proper resource cleanup.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

UnsentDisconnectChannelInterceptorback to summary
private UnsentDisconnectChannelInterceptor()

Method Detail

afterSendCompletionback to summary
public void afterSendCompletion(Message<?> message, MessageChannel channel, boolean sent, Exception ex)

Overrides default org.springframework.messaging.support.ChannelInterceptor.afterSendCompletion.

Doc from org.springframework.messaging.support.ChannelInterceptor.afterSendCompletion.

Invoked after the completion of a send regardless of any exception that have been raised thus allowing for proper resource cleanup.

Note that this will be invoked only if preSend successfully completed and returned a Message, i.e. it did not return null.

Annotations
@Override