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

public Class DefaultSubscriptionRegistry

extends AbstractSubscriptionRegistry
Class Inheritance
Imports
java.util.ArrayList, .Collection, .Collections, .List, .Map, .Queue, java.util.concurrent.ConcurrentHashMap, .ConcurrentLinkedQueue, .ConcurrentMap, java.util.concurrent.atomic.AtomicInteger, java.util.function.BiConsumer, org.springframework.expression.EvaluationContext, .Expression, .ExpressionParser, .PropertyAccessor, .TypedValue, org.springframework.expression.spel.SpelEvaluationException, org.springframework.expression.spel.standard.SpelExpressionParser, org.springframework.expression.spel.support.SimpleEvaluationContext, org.springframework.lang.Nullable, org.springframework.messaging.Message, .MessageHeaders, org.springframework.messaging.simp.SimpMessageHeaderAccessor, org.springframework.messaging.support.MessageHeaderAccessor, .NativeMessageHeaderAccessor, org.springframework.util.AntPathMatcher, .Assert, .LinkedMultiValueMap, .MultiValueMap, .PathMatcher, .StringUtils

Implementation of SubscriptionRegistry that stores subscriptions in memory and uses a PathMatcher for matching destinations.

This class also supports an optional selector header on subscription messages with Spring Expression Language (SpEL) expressions evaluated against the headers to filter out messages in addition to destination matching. As of Spring Framework 6.1, the SpEL support is disabled by default, but it can be enabled by setting a selector header name.

Authors
Rossen Stoyanchev, Sebastien Deleuze, Juergen Hoeller, Sam Brannen
Since
4.0

Nested and Inner Type Summary

Modifier and TypeClass and Description
private class
private static class
DefaultSubscriptionRegistry.SessionInfo

Container for the subscriptions of a session.

private static class
DefaultSubscriptionRegistry.SessionRegistry

Registry for all session and their subscriptions.

private static class
private static class
DefaultSubscriptionRegistry.Subscription

Represents a subscription.

Field Summary

Modifier and TypeField and Description
private int
public static final int
DEFAULT_CACHE_LIMIT

Default maximum number of entries for the destination cache: 1024.

private final DefaultSubscriptionRegistry.DestinationCache
private final ExpressionParser
private static final EvaluationContext
messageEvalContext

Static evaluation context to reuse.

private PathMatcher
private volatile boolean
private String
private final DefaultSubscriptionRegistry.SessionRegistry
Inherited from org.springframework.messaging.simp.broker.AbstractSubscriptionRegistry:
logger

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
protected void
private boolean
evaluateExpression(Expression expression, Message<?> message)

protected MultiValueMap<String, String>
public int
getCacheLimit()

Return the maximum number of entries for the resolved destination cache.

public PathMatcher
getPathMatcher()

Return the configured PathMatcher.

private Expression
public String
getSelectorHeaderName()

Return the name of the selector header.

protected void
public void
setCacheLimit(int cacheLimit)

Specify the maximum number of entries for the resolved destination cache.

public void
setPathMatcher(PathMatcher pathMatcher)

Specify the PathMatcher to use.

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
unregisterAllSubscriptions(String sessionId)

Implements org.springframework.messaging.simp.broker.SubscriptionRegistry.unregisterAllSubscriptions.

Remove all subscriptions associated with the given sessionId.

Inherited from org.springframework.messaging.simp.broker.AbstractSubscriptionRegistry:
findSubscriptionsregisterSubscriptionunregisterSubscription

Field Detail

cacheLimitback to summary
private int cacheLimit
DEFAULT_CACHE_LIMITback to summary
public static final int DEFAULT_CACHE_LIMIT

Default maximum number of entries for the destination cache: 1024.

destinationCacheback to summary
private final DefaultSubscriptionRegistry.DestinationCache destinationCache
expressionParserback to summary
private final ExpressionParser expressionParser
messageEvalContextback to summary
private static final EvaluationContext messageEvalContext

Static evaluation context to reuse.

pathMatcherback to summary
private PathMatcher pathMatcher
selectorHeaderInUseback to summary
private volatile boolean selectorHeaderInUse
selectorHeaderNameback to summary
private String selectorHeaderName
Annotations
@Nullable
sessionRegistryback to summary
private final DefaultSubscriptionRegistry.SessionRegistry sessionRegistry

Constructor Detail

DefaultSubscriptionRegistryback to summary
public DefaultSubscriptionRegistry()

Method Detail

addSubscriptionInternalback to summary
protected void addSubscriptionInternal(String sessionId, String subscriptionId, String destination, Message<?> message)

Implements abstract org.springframework.messaging.simp.broker.AbstractSubscriptionRegistry.addSubscriptionInternal.

Annotations
@Override
evaluateExpressionback to summary
private boolean evaluateExpression(Expression expression, Message<?> message)
findSubscriptionsInternalback to summary
protected MultiValueMap<String, String> findSubscriptionsInternal(String destination, Message<?> message)

Implements abstract org.springframework.messaging.simp.broker.AbstractSubscriptionRegistry.findSubscriptionsInternal.

Annotations
@Override
getCacheLimitback to summary
public int getCacheLimit()

Return the maximum number of entries for the resolved destination cache.

getPathMatcherback to summary
public PathMatcher getPathMatcher()

Return the configured PathMatcher.

getSelectorExpressionback to summary
private Expression getSelectorExpression(MessageHeaders headers)
Annotations
@Nullable
getSelectorHeaderNameback to summary
public String getSelectorHeaderName()

Return the name of the selector header.

Annotations
@Nullable
Since
4.2
See Also
setSelectorHeaderName(String)
removeSubscriptionInternalback to summary
protected void removeSubscriptionInternal(String sessionId, String subscriptionId, Message<?> message)

Implements abstract org.springframework.messaging.simp.broker.AbstractSubscriptionRegistry.removeSubscriptionInternal.

Annotations
@Override
setCacheLimitback to summary
public void setCacheLimit(int cacheLimit)

Specify the maximum number of entries for the resolved destination cache. Default is 1024.

setPathMatcherback to summary
public void setPathMatcher(PathMatcher pathMatcher)

Specify the PathMatcher to use.

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.

Parameters
selectorHeaderName:String

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

Since
4.2
unregisterAllSubscriptionsback to summary
public void unregisterAllSubscriptions(String sessionId)

Implements org.springframework.messaging.simp.broker.SubscriptionRegistry.unregisterAllSubscriptions.

Doc from org.springframework.messaging.simp.broker.SubscriptionRegistry.unregisterAllSubscriptions.

Remove all subscriptions associated with the given sessionId.

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

private final Class DefaultSubscriptionRegistry.DestinationCache

extends Object
Class Inheritance
  • java.lang.Object
  • org.springframework.messaging.simp.broker.DefaultSubscriptionRegistry.DestinationCache

Cache for destinations resolved previously via DefaultSubscriptionRegistry#findSubscriptionsInternal(String, Message).

Field Summary

Modifier and TypeField and Description
private final Queue<String>
private final AtomicInteger
private final Map<String, LinkedMultiValueMap<String, String>>

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
private void
addMatchedSubscriptionId(LinkedMultiValueMap<String, String> sessionIdToSubscriptionIds, String sessionId, String subscriptionId)

private void
addToDestination(String destination, String sessionId, String subscriptionId)

private LinkedMultiValueMap<String, String>
private void
public LinkedMultiValueMap<String, String>
getSubscriptions(String destination)

private void
removeInternal(String destination, String sessionId, String subscriptionId)

public void
public void
public void
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

cacheEvictionPolicyback to summary
private final Queue<String> cacheEvictionPolicy
cacheSizeback to summary
private final AtomicInteger cacheSize
destinationCacheback to summary
private final Map<String, LinkedMultiValueMap<String, String>> destinationCache

Constructor Detail

DestinationCacheback to summary
private DestinationCache()

Method Detail

addMatchedSubscriptionIdback to summary
private void addMatchedSubscriptionId(LinkedMultiValueMap<String, String> sessionIdToSubscriptionIds, String sessionId, String subscriptionId)
addToDestinationback to summary
private void addToDestination(String destination, String sessionId, String subscriptionId)
computeMatchingSubscriptionsback to summary
private LinkedMultiValueMap<String, String> computeMatchingSubscriptions(String destination)
ensureCacheLimitback to summary
private void ensureCacheLimit()
getSubscriptionsback to summary
public LinkedMultiValueMap<String, String> getSubscriptions(String destination)
removeInternalback to summary
private void removeInternal(String destination, String sessionId, String subscriptionId)
updateAfterNewSubscriptionback to summary
public void updateAfterNewSubscription(String sessionId, DefaultSubscriptionRegistry.Subscription subscription)
updateAfterRemovedSessionback to summary
public void updateAfterRemovedSession(String sessionId, DefaultSubscriptionRegistry.SessionInfo info)
updateAfterRemovedSubscriptionback to summary
public void updateAfterRemovedSubscription(String sessionId, DefaultSubscriptionRegistry.Subscription subscription)
org.springframework.messaging.simp.broker back to summary

private final Class DefaultSubscriptionRegistry.SessionInfo

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

Container for the subscriptions of a session.

Field Summary

Modifier and TypeField and Description
private final Map<String, DefaultSubscriptionRegistry.Subscription>

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public void
public DefaultSubscriptionRegistry.Subscription
getSubscription(String subscriptionId)

public Collection<DefaultSubscriptionRegistry.Subscription>
public DefaultSubscriptionRegistry.Subscription
removeSubscription(String subscriptionId)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

subscriptionMapback to summary
private final Map<String, DefaultSubscriptionRegistry.Subscription> subscriptionMap

Constructor Detail

SessionInfoback to summary
private SessionInfo()

Method Detail

addSubscriptionback to summary
public void addSubscription(DefaultSubscriptionRegistry.Subscription subscription)
getSubscriptionback to summary
public DefaultSubscriptionRegistry.Subscription getSubscription(String subscriptionId)
Annotations
@Nullable
getSubscriptionsback to summary
public Collection<DefaultSubscriptionRegistry.Subscription> getSubscriptions()
removeSubscriptionback to summary
public DefaultSubscriptionRegistry.Subscription removeSubscription(String subscriptionId)
Annotations
@Nullable
org.springframework.messaging.simp.broker back to summary

private final Class DefaultSubscriptionRegistry.SessionRegistry

extends Object
Class Inheritance
  • java.lang.Object
  • org.springframework.messaging.simp.broker.DefaultSubscriptionRegistry.SessionRegistry

Registry for all session and their subscriptions.

Field Summary

Modifier and TypeField and Description
private final ConcurrentMap<String, DefaultSubscriptionRegistry.SessionInfo>

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public void
public void
public DefaultSubscriptionRegistry.SessionInfo
getSession(String sessionId)

public DefaultSubscriptionRegistry.SessionInfo
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

sessionsback to summary
private final ConcurrentMap<String, DefaultSubscriptionRegistry.SessionInfo> sessions

Constructor Detail

SessionRegistryback to summary
private SessionRegistry()

Method Detail

addSubscriptionback to summary
public void addSubscription(String sessionId, DefaultSubscriptionRegistry.Subscription subscription)
forEachSubscriptionback to summary
public void forEachSubscription(BiConsumer<String, DefaultSubscriptionRegistry.Subscription> consumer)
getSessionback to summary
public DefaultSubscriptionRegistry.SessionInfo getSession(String sessionId)
Annotations
@Nullable
removeSubscriptionsback to summary
public DefaultSubscriptionRegistry.SessionInfo removeSubscriptions(String sessionId)
Annotations
@Nullable
org.springframework.messaging.simp.broker back to summary

private Class DefaultSubscriptionRegistry.SimpMessageHeaderPropertyAccessor

extends Object
implements PropertyAccessor
Class Inheritance
  • java.lang.Object
  • org.springframework.messaging.simp.broker.DefaultSubscriptionRegistry.SimpMessageHeaderPropertyAccessor
All Implemented Interfaces
org.springframework.expression.PropertyAccessor

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public boolean
canRead(EvaluationContext
the evaluation context in which the access is being attempted
context
,
Object
the target object upon which the property is being accessed
target
,
String
the name of the property being accessed
name
)

Implements org.springframework.expression.PropertyAccessor.canRead.

Called to determine if a resolver instance is able to access a specified property on a specified target object.

public boolean
canWrite(EvaluationContext
the evaluation context in which the access is being attempted
context
,
Object
the target object upon which the property is being accessed
target
,
String
the name of the property being accessed
name
)

Implements org.springframework.expression.PropertyAccessor.canWrite.

Called to determine if a resolver instance is able to write to a specified property on a specified target object.

public Class<?>[]
getSpecificTargetClasses()

Implements org.springframework.expression.PropertyAccessor.getSpecificTargetClasses.

Return an array of classes for which this resolver should be called.

public TypedValue
read(EvaluationContext
the evaluation context in which the access is being attempted
context
,
Object
the target object upon which the property is being accessed
target
,
String
the name of the property being accessed
name
)

Implements org.springframework.expression.PropertyAccessor.read.

Called to read a property from a specified target object.

public void
write(EvaluationContext
the evaluation context in which the access is being attempted
context
,
Object
the target object upon which the property is being accessed
target
,
String
the name of the property being accessed
name
,
Object
the new value for the property
value
)

Implements org.springframework.expression.PropertyAccessor.write.

Called to write to a property on a specified target object.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

SimpMessageHeaderPropertyAccessorback to summary
private SimpMessageHeaderPropertyAccessor()

Method Detail

canReadback to summary
public boolean canRead(EvaluationContext context, Object target, String name)

Implements org.springframework.expression.PropertyAccessor.canRead.

Doc from org.springframework.expression.PropertyAccessor.canRead.

Called to determine if a resolver instance is able to access a specified property on a specified target object.

Parameters
context:EvaluationContext

the evaluation context in which the access is being attempted

target:Object

the target object upon which the property is being accessed

name:String

the name of the property being accessed

Returns:boolean

true if this resolver is able to read the property

Annotations
@Override
canWriteback to summary
public boolean canWrite(EvaluationContext context, Object target, String name)

Implements org.springframework.expression.PropertyAccessor.canWrite.

Doc from org.springframework.expression.PropertyAccessor.canWrite.

Called to determine if a resolver instance is able to write to a specified property on a specified target object.

Parameters
context:EvaluationContext

the evaluation context in which the access is being attempted

target:Object

the target object upon which the property is being accessed

name:String

the name of the property being accessed

Returns:boolean

true if this resolver is able to write to the property

Annotations
@Override
getSpecificTargetClassesback to summary
public Class<?>[] getSpecificTargetClasses()

Implements org.springframework.expression.PropertyAccessor.getSpecificTargetClasses.

Doc from org.springframework.expression.PropertyAccessor.getSpecificTargetClasses.

Return an array of classes for which this resolver should be called.

Returning null indicates this is a general resolver that can be called in an attempt to resolve a property on any type.

Returns:Class<?>[]

an array of classes that this resolver is suitable for (or null if a general resolver)

Annotations
@Override
readback to summary
public TypedValue read(EvaluationContext context, Object target, String name)

Implements org.springframework.expression.PropertyAccessor.read.

Doc from org.springframework.expression.PropertyAccessor.read.

Called to read a property from a specified target object. Should only succeed if canRead also returns true.

Parameters
context:EvaluationContext

the evaluation context in which the access is being attempted

target:Object

the target object upon which the property is being accessed

name:String

the name of the property being accessed

Returns:TypedValue

a TypedValue object wrapping the property value read and a type descriptor for it

Annotations
@Override
@SuppressWarnings:rawtypes
writeback to summary
public void write(EvaluationContext context, Object target, String name, Object value)

Implements org.springframework.expression.PropertyAccessor.write.

Doc from org.springframework.expression.PropertyAccessor.write.

Called to write to a property on a specified target object. Should only succeed if canWrite also returns true.

Parameters
context:EvaluationContext

the evaluation context in which the access is being attempted

target:Object

the target object upon which the property is being accessed

name:String

the name of the property being accessed

value:Object

the new value for the property

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

private final Class DefaultSubscriptionRegistry.Subscription

extends Object
Class Inheritance
  • java.lang.Object
  • org.springframework.messaging.simp.broker.DefaultSubscriptionRegistry.Subscription

Represents a subscription.

Field Summary

Modifier and TypeField and Description
private final String
private final String
private final boolean
private final Expression

Constructor Summary

AccessConstructor and Description
public
Subscription(String id, String destination, boolean isPattern, Expression selector)

Method Summary

Modifier and TypeMethod and Description
public boolean
equals(Object
the reference object with which to compare.
other
)

Overrides java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

public String
public String
public Expression
public int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for this object.

public boolean
public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait

Field Detail

destinationback to summary
private final String destination
idback to summary
private final String id
isPatternback to summary
private final boolean isPattern
selectorback to summary
private final Expression selector
Annotations
@Nullable

Constructor Detail

Subscriptionback to summary
public Subscription(String id, String destination, boolean isPattern, Expression selector)

Method Detail

equalsback to summary
public boolean equals(Object other)

Overrides java.lang.Object.equals.

Doc from java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

The equals method implements an equivalence relation on non-null object references:

  • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
  • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  • For any non-null reference value x, x.equals(null) should return false.

An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes.

Parameters
other:Object

the reference object with which to compare.

Returns:boolean

true if this object is the same as the obj argument; false otherwise.

Annotations
@Override
getDestinationback to summary
public String getDestination()
getIdback to summary
public String getId()
getSelectorback to summary
public Expression getSelector()
Annotations
@Nullable
hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Doc from java.lang.Object.hashCode.

Returns a hash code value for this object. This method is supported for the benefit of hash tables such as those provided by java.util.HashMap.

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
Returns:int

a hash code value for this object

Annotations
@Override
isPatternback to summary
public boolean isPattern()
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