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

public Class UserDestinationMessageHandler

extends Object
implements MessageHandler, SmartLifecycle
Class Inheritance
All Implemented Interfaces
org.springframework.context.SmartLifecycle, org.springframework.context.Phased, org.springframework.context.Lifecycle, org.springframework.messaging.MessageHandler
Imports
java.util.Arrays, .Iterator, .List, .Map, .Set, java.util.concurrent.ConcurrentHashMap, org.apache.commons.logging.Log, org.springframework.context.SmartLifecycle, org.springframework.lang.Nullable, org.springframework.messaging.Message, .MessageChannel, .MessageHandler, .MessageHeaders, .MessagingException, .SubscribableChannel, org.springframework.messaging.core.MessageSendingOperations, org.springframework.messaging.simp.SimpLogging, .SimpMessageHeaderAccessor, .SimpMessageType, .SimpMessagingTemplate, org.springframework.messaging.simp.broker.OrderedMessageChannelDecorator, org.springframework.messaging.support.MessageBuilder, .MessageHeaderAccessor, .MessageHeaderInitializer, .NativeMessageHeaderAccessor, org.springframework.util.Assert, .StringUtils

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.

Author
Rossen Stoyanchev
Since
4.0

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
UserDestinationMessageHandler.BroadcastHandler

A handler that broadcasts locally unresolved messages to the broker and also handles similar broadcasts received from the broker.

private static class

Field Summary

Modifier and TypeField and Description
private UserDestinationMessageHandler.BroadcastHandler
private final SubscribableChannel
private final SubscribableChannel
private final UserDestinationResolver
private MessageHeaderInitializer
private final Object
private static final Log
private Integer
private volatile boolean
private final UserDestinationMessageHandler.SendHelper

Constructor Summary

AccessConstructor and Description
public
UserDestinationMessageHandler(SubscribableChannel
messages received from clients.
clientInboundChannel
,
SubscribableChannel
messages sent to the broker.
brokerChannel
,
UserDestinationResolver
the resolver for "user" destinations.
destinationResolver
)

Create an instance with the given client and broker channels to subscribe to, and then send resolved messages to the broker channel.

Method Summary

Modifier and TypeMethod and Description
public String
getBroadcastDestination()

Return the configured destination for unresolved messages.

public MessageSendingOperations<String>
getBrokerMessagingTemplate()

Return the messaging template used to send resolved messages to the broker channel.

public MessageHeaderInitializer
getHeaderInitializer()

Return the configured header initializer.

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 UserDestinationResolver
public void
handleMessage(Message<?>
the message to be handled
sourceMessage
)

Implements org.springframework.messaging.MessageHandler.handleMessage.

Handle the given message.
private void
public final boolean
isRunning()

Implements org.springframework.context.Lifecycle.isRunning.

Check whether this component is currently running.
public void
setBroadcastDestination(String
the target destination.
destination
)

Set a destination to broadcast messages to that remain unresolved because the user is not connected.

public void
setHeaderInitializer(MessageHeaderInitializer headerInitializer)

Configure a custom MessageHeaderInitializer to initialize the headers of resolved target messages.

public void
setPhase(int phase)

Set the phase that this handler should run in.

By default, this is SmartLifecycle#DEFAULT_PHASE.

public final void
start()

Implements org.springframework.context.Lifecycle.start.

Start this component.
public final 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.
public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

broadcastHandlerback to summary
private UserDestinationMessageHandler.BroadcastHandler broadcastHandler
Annotations
@Nullable
brokerChannelback to summary
private final SubscribableChannel brokerChannel
clientInboundChannelback to summary
private final SubscribableChannel clientInboundChannel
destinationResolverback to summary
private final UserDestinationResolver destinationResolver
headerInitializerback to summary
private MessageHeaderInitializer headerInitializer
Annotations
@Nullable
lifecycleMonitorback to summary
private final Object lifecycleMonitor
loggerback to summary
private static final Log logger
phaseback to summary
private Integer phase
Annotations
@Nullable
runningback to summary
private volatile boolean running
sendHelperback to summary
private final UserDestinationMessageHandler.SendHelper sendHelper

Constructor Detail

UserDestinationMessageHandlerback to summary
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:SubscribableChannel

messages received from clients.

brokerChannel:SubscribableChannel

messages sent to the broker.

destinationResolver:UserDestinationResolver

the resolver for "user" destinations.

Method Detail

getBroadcastDestinationback to summary
public String getBroadcastDestination()

Return the configured destination for unresolved messages.

Annotations
@Nullable
getBrokerMessagingTemplateback to summary
public MessageSendingOperations<String> getBrokerMessagingTemplate()

Return the messaging template used to send resolved messages to the broker channel.

getHeaderInitializerback to summary
public MessageHeaderInitializer getHeaderInitializer()

Return the configured header initializer.

Annotations
@Nullable
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
getUserDestinationResolverback to summary
public UserDestinationResolver getUserDestinationResolver()

Return the configured UserDestinationResolver.

handleMessageback to summary
public void handleMessage(Message<?> sourceMessage) throws MessagingException

Implements org.springframework.messaging.MessageHandler.handleMessage.

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

Handle the given message.

Parameters
sourceMessage:Message<?>

the message to be handled

Annotations
@Override
Exceptions
MessagingException:
if the handler failed to process the message
initHeadersback to summary
private void initHeaders(SimpMessageHeaderAccessor headerAccessor)
isRunningback to summary
public final boolean isRunning()

Implements org.springframework.context.Lifecycle.isRunning.

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

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.

Returns:boolean

whether the component is currently running

Annotations
@Override
setBroadcastDestinationback to summary
public void setBroadcastDestination(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:String

the target destination.

setHeaderInitializerback to summary
public void setHeaderInitializer(MessageHeaderInitializer headerInitializer)

Configure a custom MessageHeaderInitializer to initialize the headers of resolved target messages.

By default this is not set.

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
startback to summary
public final 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
stopback to summary
public final 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
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object.

Returns:String

a string representation of the object.

Annotations
@Override
org.springframework.messaging.simp.user back to summary

private Class UserDestinationMessageHandler.BroadcastHandler

extends Object
Class Inheritance
  • java.lang.Object
  • org.springframework.messaging.simp.user.UserDestinationMessageHandler.BroadcastHandler

A handler that broadcasts locally unresolved messages to the broker and also handles similar broadcasts received from the broker.

Field Summary

Modifier and TypeField and Description
private final String
private final MessageSendingOperations<String>
private static final List<String>

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public String
public void
public Message<?>
preHandle(Message<?> message)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

broadcastDestinationback to summary
private final String broadcastDestination
messagingTemplateback to summary
private final MessageSendingOperations<String> messagingTemplate
NO_COPY_LISTback to summary
private static final List<String> NO_COPY_LIST

Constructor Detail

BroadcastHandlerback to summary
public BroadcastHandler(MessageSendingOperations<String> template, String destination)

Method Detail

getBroadcastDestinationback to summary
public String getBroadcastDestination()
handleUnresolvedback to summary
public void handleUnresolved(Message<?> message)
preHandleback to summary
public Message<?> preHandle(Message<?> message) throws MessagingException
Annotations
@Nullable
org.springframework.messaging.simp.user back to summary

private Class UserDestinationMessageHandler.SendHelper

extends Object
Class Inheritance
  • java.lang.Object
  • org.springframework.messaging.simp.user.UserDestinationMessageHandler.SendHelper

Field Summary

Modifier and TypeField and Description
private final MessageChannel
private final MessageSendingOperations<String>
private final Map<String, MessageSendingOperations<String>>

Constructor Summary

AccessConstructor and Description
pack-priv
SendHelper(MessageChannel clientInboundChannel, MessageChannel brokerChannel)

Method Summary

Modifier and TypeMethod and Description
public void
public MessageSendingOperations<String>
private MessageSendingOperations<String>
public void
send(UserDestinationResult destinationResult, Message<?> message)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

brokerChannelback to summary
private final MessageChannel brokerChannel
messagingTemplateback to summary
private final MessageSendingOperations<String> messagingTemplate
orderedMessagingTemplatesback to summary
private final Map<String, MessageSendingOperations<String>> orderedMessagingTemplates
Annotations
@Nullable

Constructor Detail

SendHelperback to summary
pack-priv SendHelper(MessageChannel clientInboundChannel, MessageChannel brokerChannel)

Method Detail

checkDisconnectback to summary
public void checkDisconnect(Message<?> message)
getMessagingTemplateback to summary
public MessageSendingOperations<String> getMessagingTemplate()
getTemplateToUseback to summary
private MessageSendingOperations<String> getTemplateToUse(String sessionId)
sendback to summary
public void send(UserDestinationResult destinationResult, Message<?> message) throws MessagingException