Class 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
MessageHandler that broker messages to
registered subscribers.- Since:
- 4.0
- Author:
- Rossen Stoyanchev
-
Field Summary
FieldsFields inherited from interface org.springframework.context.SmartLifecycle
DEFAULT_PHASE -
Constructor Summary
ConstructorsConstructorDescriptionAbstractBrokerMessageHandler(SubscribableChannel inboundChannel, MessageChannel outboundChannel, SubscribableChannel brokerChannel) Constructor with no destination prefixes (matches all destinations).AbstractBrokerMessageHandler(SubscribableChannel inboundChannel, MessageChannel outboundChannel, SubscribableChannel brokerChannel, @Nullable Collection<String> destinationPrefixes) Constructor with destination prefixes to match to destinations of messages. -
Method Summary
Modifier and TypeMethodDescriptionprotected booleancheckDestinationPrefix(@Nullable String destination) Whether a message with the given destination should be processed.@Nullable org.springframework.context.ApplicationEventPublisherprotected MessageChannelgetClientOutboundChannelForSession(String sessionId) Get the MessageChannel to use for sending messages to clients, possibly a per-session wrapper whenpreservePublishOrder=true.Return destination prefixes to use to filter messages to forward to the broker.intgetPhase()voidhandleMessage(Message<?> message) Handle the given message.protected abstract voidhandleMessageInternal(Message<?> message) booleanbooleanWhether the message broker is currently available and able to process messages.booleanWhether to ensure messages are received in the order of publication.final booleanCheck whether this message handler is currently running.protected voidprotected voidvoidsetApplicationEventPublisher(@Nullable org.springframework.context.ApplicationEventPublisher publisher) voidsetAutoStartup(boolean autoStartup) voidsetPhase(int phase) Set the phase that this handler should run in.voidsetPreservePublishOrder(boolean preservePublishOrder) Whether the client must receive messages in the order of publication.voidsetUserDestinationPredicate(@Nullable Predicate<String> predicate) Configure a Predicate to identify messages with a user destination.voidstart()protected voidvoidstop()final voidprotected voidMethods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.springframework.context.SmartLifecycle
isPauseable
-
Field Details
-
logger
protected final org.apache.commons.logging.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 (for example, WebSocket clients)outboundChannel- the channel for sending messages to clients (for example, 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 (for example, WebSocket clients)outboundChannel- the channel for sending messages to clients (for example, WebSocket clients)brokerChannel- the channel for the application to send messages to the brokerdestinationPrefixes- prefixes to use to filter out messages
-
-
Method Details
-
getClientInboundChannel
-
getClientOutboundChannel
-
getBrokerChannel
-
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
Configure a Predicate to identify messages with a user destination. When nodestination prefixesare configured, this helps to recognize and skip user destination messages that need to be pre-processed by theUserDestinationMessageHandlerbefore 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
truemessages within the same session will be sent to the"clientOutboundChannel"one at a time in order to preserve the order of publication.- 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:
setApplicationEventPublisherin interfaceorg.springframework.context.ApplicationEventPublisherAware
-
getApplicationEventPublisher
public @Nullable org.springframework.context.ApplicationEventPublisher getApplicationEventPublisher() -
setAutoStartup
public void setAutoStartup(boolean autoStartup) -
isAutoStartup
public boolean isAutoStartup()- Specified by:
isAutoStartupin interfaceorg.springframework.context.SmartLifecycle
-
setPhase
public void setPhase(int phase) Set the phase that this handler should run in.By default, this is
SmartLifecycle.DEFAULT_PHASE, but with@EnableWebSocketMessageBrokerconfiguration it is set to 0.- Since:
- 6.1.4
-
getPhase
public int getPhase()- Specified by:
getPhasein interfaceorg.springframework.context.Phased- Specified by:
getPhasein interfaceorg.springframework.context.SmartLifecycle
-
start
public void start()- Specified by:
startin interfaceorg.springframework.context.Lifecycle
-
startInternal
protected void startInternal() -
stop
public void stop()- Specified by:
stopin interfaceorg.springframework.context.Lifecycle
-
stopInternal
protected void stopInternal() -
stop
- Specified by:
stopin interfaceorg.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 subclass implementation.- Specified by:
isRunningin interfaceorg.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 subclass implementation.Application components may implement
org.springframework.context.ApplicationListener<BrokerAvailabilityEvent>to receive notifications when broker becomes available and unavailable. -
handleMessage
Description copied from interface:MessageHandlerHandle the given message.- Specified by:
handleMessagein interfaceMessageHandler- Parameters:
message- the message to be handled
-
handleMessageInternal
-
checkDestinationPrefix
Whether a message with the given destination should be processed. This is the case if one of the following conditions is true:- The destination starts with one of the configured
destination prefixes. - No prefixes are configured and the destination isn't matched
by the
userDestinationPredicate. - The message has no destination.
- Parameters:
destination- the destination to check- Returns:
- whether to process (true) or skip (false) the destination
- The destination starts with one of the configured
-
publishBrokerAvailableEvent
protected void publishBrokerAvailableEvent() -
getClientOutboundChannelForSession
Get the MessageChannel to use for sending messages to clients, possibly a per-session wrapper whenpreservePublishOrder=true.- Since:
- 5.1
-