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

public Class SimpleBrokerMessageHandler

extends AbstractBrokerMessageHandler
Class Inheritance
Imports
java.security.Principal, java.time.Duration, java.util.Arrays, .Collection, .Map, java.util.concurrent.ConcurrentHashMap, .ScheduledFuture, org.springframework.lang.Nullable, org.springframework.messaging.Message, .MessageChannel, .MessageHeaders, .SubscribableChannel, org.springframework.messaging.simp.SimpMessageHeaderAccessor, .SimpMessageType, org.springframework.messaging.support.MessageBuilder, .MessageHeaderAccessor, .MessageHeaderInitializer, org.springframework.scheduling.TaskScheduler, org.springframework.util.Assert, .MultiValueMap, .PathMatcher

A "simple" message broker that recognizes the message types defined in SimpMessageType, keeps track of subscriptions with the help of a SubscriptionRegistry, and sends messages to subscribers.
Authors
Rossen Stoyanchev, Juergen Hoeller, Sam Brannen
Since
4.0

Nested and Inner Type Summary

Modifier and TypeClass and Description
private class
private static class

Field Summary

Modifier and TypeField and Description
private Integer
private static final byte[]
private MessageHeaderInitializer
private ScheduledFuture<?>
private long[]
private PathMatcher
private String
private final Map<String, SimpleBrokerMessageHandler.SessionInfo>
private SubscriptionRegistry
private TaskScheduler
Inherited from org.springframework.messaging.simp.broker.AbstractBrokerMessageHandler:
logger

Constructor Summary

AccessConstructor and Description
public
SimpleBrokerMessageHandler(SubscribableChannel
the channel for receiving messages from clients (e.g. WebSocket clients)
clientInboundChannel
,
MessageChannel
the channel for sending messages to clients (e.g. WebSocket clients)
clientOutboundChannel
,
SubscribableChannel
the channel for the application to send messages to the broker
brokerChannel
,
Collection<String>
prefixes to use to filter out messages
destinationPrefixes
)

Create a SimpleBrokerMessageHandler instance with the given message channels and destination prefixes.

Method Summary

Modifier and TypeMethod and Description
public MessageHeaderInitializer
getHeaderInitializer()

Return the configured header initializer.

public long[]
getHeartbeatValue()

The configured value for the heart-beat settings.

public SubscriptionRegistry
public TaskScheduler
getTaskScheduler()

Return the configured TaskScheduler.

private void
handleDisconnect(String sessionId, Principal user, Message<?> origMessage)

protected void
private void
private void
private Duration
private void
private void
private void
logMessage(Message<?> message)

protected void
sendMessageToSubscribers(String destination, Message<?> message)

public void
setCacheLimit(Integer cacheLimit)

When configured, the specified cache limit is passed down to the underlying SubscriptionRegistry, overriding any default there.

public void
setHeaderInitializer(MessageHeaderInitializer headerInitializer)

Configure a MessageHeaderInitializer to apply to the headers of all messages sent to the client outbound channel.

public void
setHeartbeatValue(long[] heartbeat)

Configure the value for the heart-beat settings.

public void
setPathMatcher(PathMatcher pathMatcher)

When configured, the given PathMatcher is passed down to the underlying SubscriptionRegistry to use for matching destination to subscriptions.

public void
setSelectorHeaderName(String
the name to use for a selector header, or null or blank to disable selector header support
selectorHeaderName
)

Configure the name of a header that a subscription message can have for the purpose of filtering messages matched to the subscription.

public void
setSubscriptionRegistry(SubscriptionRegistry subscriptionRegistry)

Configure a custom SubscriptionRegistry to use for storing subscriptions.

public void
setTaskScheduler(TaskScheduler taskScheduler)

Configure the org.springframework.scheduling.TaskScheduler to use for providing heartbeat support.

public void
public void
public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

private void
Inherited from org.springframework.messaging.simp.broker.AbstractBrokerMessageHandler:
checkDestinationPrefixgetApplicationEventPublishergetBrokerChannelgetClientInboundChannelgetClientOutboundChannelgetClientOutboundChannelForSessiongetDestinationPrefixesgetPhasehandleMessageisAutoStartupisBrokerAvailableisPreservePublishOrderisRunningpublishBrokerAvailableEventpublishBrokerUnavailableEventsetApplicationEventPublishersetAutoStartupsetPhasesetPreservePublishOrdersetUserDestinationPredicatestartstopstop

Field Detail

cacheLimitback to summary
private Integer cacheLimit
Annotations
@Nullable
EMPTY_PAYLOADback to summary
private static final byte[] EMPTY_PAYLOAD
headerInitializerback to summary
private MessageHeaderInitializer headerInitializer
Annotations
@Nullable
heartbeatFutureback to summary
private ScheduledFuture<?> heartbeatFuture
Annotations
@Nullable
heartbeatValueback to summary
private long[] heartbeatValue
Annotations
@Nullable
pathMatcherback to summary
private PathMatcher pathMatcher
Annotations
@Nullable
selectorHeaderNameback to summary
private String selectorHeaderName
Annotations
@Nullable
sessionsback to summary
private final Map<String, SimpleBrokerMessageHandler.SessionInfo> sessions
subscriptionRegistryback to summary
private SubscriptionRegistry subscriptionRegistry
taskSchedulerback to summary
private TaskScheduler taskScheduler
Annotations
@Nullable

Constructor Detail

SimpleBrokerMessageHandlerback to summary
public SimpleBrokerMessageHandler(SubscribableChannel clientInboundChannel, MessageChannel clientOutboundChannel, SubscribableChannel brokerChannel, Collection<String> destinationPrefixes)

Create a SimpleBrokerMessageHandler instance with the given message channels and destination prefixes.

Parameters
clientInboundChannel:SubscribableChannel

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

clientOutboundChannel: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

getHeaderInitializerback to summary
public MessageHeaderInitializer getHeaderInitializer()

Return the configured header initializer.

Annotations
@Nullable
Since
4.1
getHeartbeatValueback to summary
public long[] getHeartbeatValue()

The configured value for the heart-beat settings.

Annotations
@Nullable
Since
4.2
getSubscriptionRegistryback to summary
public SubscriptionRegistry getSubscriptionRegistry()
getTaskSchedulerback to summary
public TaskScheduler getTaskScheduler()

Return the configured TaskScheduler.

Annotations
@Nullable
Since
4.2
handleDisconnectback to summary
private void handleDisconnect(String sessionId, Principal user, Message<?> origMessage)
handleMessageInternalback to summary
protected void handleMessageInternal(Message<?> message)

Implements abstract org.springframework.messaging.simp.broker.AbstractBrokerMessageHandler.handleMessageInternal.

Annotations
@Override
initCacheLimitToUseback to summary
private void initCacheLimitToUse()
initHeadersback to summary
private void initHeaders(SimpMessageHeaderAccessor accessor)
initHeartbeatTaskDelayback to summary
private Duration initHeartbeatTaskDelay()
initPathMatcherToUseback to summary
private void initPathMatcherToUse()
initSelectorHeaderNameToUseback to summary
private void initSelectorHeaderNameToUse()
logMessageback to summary
private void logMessage(Message<?> message)
sendMessageToSubscribersback to summary
protected void sendMessageToSubscribers(String destination, Message<?> message)
setCacheLimitback to summary
public void setCacheLimit(Integer cacheLimit)

When configured, the specified cache limit is passed down to the underlying SubscriptionRegistry, overriding any default there.

With a standard DefaultSubscriptionRegistry, the default cache limit is 1024.

Setting this property has no effect if the underlying SubscriptionRegistry is not an instance of DefaultSubscriptionRegistry.

Since
4.3.2
See Also
setSubscriptionRegistry, DefaultSubscriptionRegistry#setCacheLimit, DefaultSubscriptionRegistry#DEFAULT_CACHE_LIMIT
setHeaderInitializerback to summary
public void setHeaderInitializer(MessageHeaderInitializer headerInitializer)

Configure a MessageHeaderInitializer to apply to the headers of all messages sent to the client outbound channel.

By default this property is not set.

Since
4.1
setHeartbeatValueback to summary
public void setHeartbeatValue(long[] heartbeat)

Configure the value for the heart-beat settings. The first number represents how often the server will write or send a heartbeat. The second is how often the client should write. 0 means no heartbeats.

By default this is set to "0, 0" unless the taskScheduler in which case the default becomes "10000,10000" (in milliseconds).

Since
4.2
setPathMatcherback to summary
public void setPathMatcher(PathMatcher pathMatcher)

When configured, the given PathMatcher is passed down to the underlying SubscriptionRegistry to use for matching destination to subscriptions.

Default is a standard org.springframework.util.AntPathMatcher.

Setting this property has no effect if the underlying SubscriptionRegistry is not an instance of DefaultSubscriptionRegistry.

Since
4.1
See Also
setSubscriptionRegistry, DefaultSubscriptionRegistry#setPathMatcher, org.springframework.util.AntPathMatcher
setSelectorHeaderNameback to summary
public void setSelectorHeaderName(String selectorHeaderName)

Configure the name of a header that a subscription message can have for the purpose of filtering messages matched to the subscription.

The header value is expected to be a Spring Expression Language (SpEL) boolean expression to be applied to the headers of messages matched to the subscription.

For example:

headers.foo == 'bar'

By default the selector header name is set to null which disables this feature. You can set it to "selector" or a different name to enable support for a selector header.

Setting this property has no effect if the underlying SubscriptionRegistry is not an instance of DefaultSubscriptionRegistry.

Parameters
selectorHeaderName:String

the name to use for a selector header, or null or blank to disable selector header support

Since
4.3.17
See Also
setSubscriptionRegistry, DefaultSubscriptionRegistry#setSelectorHeaderName(String)
setSubscriptionRegistryback to summary
public void setSubscriptionRegistry(SubscriptionRegistry subscriptionRegistry)

Configure a custom SubscriptionRegistry to use for storing subscriptions.

Note

If the custom registry is not an instance of DefaultSubscriptionRegistry, the configured PathMatcher, cache limit, and selector header name are not used and must be configured directly on the custom registry.

setTaskSchedulerback to summary
public void setTaskScheduler(TaskScheduler taskScheduler)

Configure the org.springframework.scheduling.TaskScheduler to use for providing heartbeat support. Setting this property also sets the heartbeatValue to "10000, 10000".

By default this is not set.

Since
4.2
startInternalback to summary
public void startInternal()

Overrides org.springframework.messaging.simp.broker.AbstractBrokerMessageHandler.startInternal.

Annotations
@Override
stopInternalback to summary
public void stopInternal()

Overrides org.springframework.messaging.simp.broker.AbstractBrokerMessageHandler.stopInternal.

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. Satisfying this method's contract implies a non-null result must be returned.

Returns:String

a string representation of the object

Annotations
@Override
updateSessionReadTimeback to summary
private void updateSessionReadTime(String sessionId)
org.springframework.messaging.simp.broker back to summary

private Class SimpleBrokerMessageHandler.HeartbeatTask

extends Object
implements Runnable
Class Inheritance
  • java.lang.Object
  • org.springframework.messaging.simp.broker.SimpleBrokerMessageHandler.HeartbeatTask
All Implemented Interfaces
java.lang.Runnable

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public void
run()

Implements java.lang.Runnable.run.

Runs this operation.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

HeartbeatTaskback to summary
private HeartbeatTask()

Method Detail

runback to summary
public void run()

Implements java.lang.Runnable.run.

Doc from java.lang.Runnable.run.

Runs this operation.

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

private Class SimpleBrokerMessageHandler.SessionInfo

extends Object
Class Inheritance
  • java.lang.Object
  • org.springframework.messaging.simp.broker.SimpleBrokerMessageHandler.SessionInfo

Field Summary

Modifier and TypeField and Description
private final MessageChannel
private static final long
private volatile long
private volatile long
private final long
private final String
private final Principal
private final long

Constructor Summary

AccessConstructor and Description
public
SessionInfo(String sessionId, Principal user, MessageChannel outboundChannel, long[] clientHeartbeat, long[] serverHeartbeat)

Method Summary

Modifier and TypeMethod and Description
public MessageChannel
public long
public long
public long
public String
public Principal
public long
public void
setLastReadTime(long lastReadTime)

public void
setLastWriteTime(long lastWriteTime)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

clientOutboundChannelback to summary
private final MessageChannel clientOutboundChannel
HEARTBEAT_MULTIPLIERback to summary
private static final long HEARTBEAT_MULTIPLIER
lastReadTimeback to summary
private volatile long lastReadTime
lastWriteTimeback to summary
private volatile long lastWriteTime
readIntervalback to summary
private final long readInterval
sessionIdback to summary
private final String sessionId
userback to summary
private final Principal user
Annotations
@Nullable
writeIntervalback to summary
private final long writeInterval

Constructor Detail

SessionInfoback to summary
public SessionInfo(String sessionId, Principal user, MessageChannel outboundChannel, long[] clientHeartbeat, long[] serverHeartbeat)

Method Detail

getClientOutboundChannelback to summary
public MessageChannel getClientOutboundChannel()
getLastReadTimeback to summary
public long getLastReadTime()
getLastWriteTimeback to summary
public long getLastWriteTime()
getReadIntervalback to summary
public long getReadInterval()
getSessionIdback to summary
public String getSessionId()
getUserback to summary
public Principal getUser()
Annotations
@Nullable
getWriteIntervalback to summary
public long getWriteInterval()
setLastReadTimeback to summary
public void setLastReadTime(long lastReadTime)
setLastWriteTimeback to summary
public void setLastWriteTime(long lastWriteTime)