Class SimpAnnotationMethodMessageHandler
- All Implemented Interfaces:
org.springframework.beans.factory.Aware
,org.springframework.beans.factory.InitializingBean
,org.springframework.context.ApplicationContextAware
,org.springframework.context.EmbeddedValueResolverAware
,org.springframework.context.Lifecycle
,org.springframework.context.Phased
,org.springframework.context.SmartLifecycle
,MessageHandler
@MessageMapping
and @SubscribeMapping
annotated methods.
Supports Ant-style path patterns with template variables.
- Since:
- 4.0
- Author:
- Rossen Stoyanchev, Brian Clozel, Juergen Hoeller
-
Field Summary
Fields inherited from class org.springframework.messaging.handler.invocation.AbstractMethodMessageHandler
logger
Fields inherited from interface org.springframework.context.SmartLifecycle
DEFAULT_PHASE
-
Constructor Summary
ConstructorDescriptionSimpAnnotationMethodMessageHandler
(SubscribableChannel clientInboundChannel, MessageChannel clientOutboundChannel, SimpMessageSendingOperations brokerTemplate) Create an instance of SimpAnnotationMethodMessageHandler with the given message channels and broker messaging template. -
Method Summary
Modifier and TypeMethodDescriptionprotected AbstractExceptionHandlerMethodResolver
createExceptionHandlerMethodResolverFor
(Class<?> beanType) org.springframework.core.convert.ConversionService
Return the configuredConversionService
.protected String
getDestination
(Message<?> message) Return destinations contained in the mapping that are not patterns and are therefore suitable for direct lookups.protected Log
Return a logger to set onInvocableHandlerMethod
.Return the configured header initializer.protected String
getLookupDestination
(String destination) Check whether the given destination (of an incoming message) matches to one of the configured destination prefixes and if so return the remaining portion of the destination after the matched prefix.protected Comparator<SimpMessageMappingInfo>
getMappingComparator
(Message<?> message) Return a comparator for sorting matching mappings.protected SimpMessageMappingInfo
getMappingForMethod
(Method method, Class<?> handlerType) Provide the mapping for a handler method.protected SimpMessageMappingInfo
getMatchingMapping
(SimpMessageMappingInfo mapping, Message<?> message) Check if a mapping matches the current message and return a possibly new mapping with conditions relevant to the current request.Return the configuredMessageConverter
.org.springframework.util.PathMatcher
Return the PathMatcher implementation to use for matching destinations.protected Log
Return a logger to set onHandlerMethodReturnValueHandlerComposite
.org.springframework.validation.Validator
Return the configured Validator instance.protected void
handleMatch
(SimpMessageMappingInfo mapping, HandlerMethod handlerMethod, String lookupDestination, Message<?> message) protected List<HandlerMethodArgumentResolver>
Return the list of argument resolvers to use.protected List<? extends HandlerMethodReturnValueHandler>
Return the list of return value handlers to use.protected boolean
Whether the given bean type should be introspected for messaging handling methods.final boolean
protected String[]
resolveEmbeddedValuesInDestinations
(String[] destinations) Resolve placeholder values in the given array of destinations.void
setConversionService
(org.springframework.core.convert.ConversionService conversionService) Configure aConversionService
to use when resolving method arguments, for example message header values.void
setDestinationPrefixes
(Collection<String> prefixes) When this property is configured only messages to destinations matching one of the configured prefixes are eligible for handling.void
setEmbeddedValueResolver
(org.springframework.util.StringValueResolver resolver) void
setHeaderInitializer
(MessageHeaderInitializer headerInitializer) Configure aMessageHeaderInitializer
to pass on toHandlerMethodReturnValueHandlers
that send messages from controller return values.void
setMessageConverter
(MessageConverter converter) Configure aMessageConverter
to use to convert the payload of a message from its serialized form with a specific MIME type to an Object matching the target method parameter.void
setPathMatcher
(org.springframework.util.PathMatcher pathMatcher) Set the PathMatcher implementation to use for matching destinations against configured destination patterns.void
setValidator
(org.springframework.validation.Validator validator) Set the Validator instance used for validating@Payload
arguments.final void
start()
final void
stop()
final void
Methods inherited from class org.springframework.messaging.handler.invocation.AbstractMethodMessageHandler
afterPropertiesSet, createHandlerMethod, detectHandlerMethods, getApplicationContext, getArgumentResolvers, getCustomArgumentResolvers, getCustomReturnValueHandlers, getDestinationPrefixes, getExceptionHandlerMethod, getHandlerMethods, getReturnValueHandlers, handleMessage, handleMessageInternal, handleNoMatch, processHandlerMethodException, registerExceptionHandlerAdvice, registerHandlerMethod, setApplicationContext, setArgumentResolvers, setCustomArgumentResolvers, setCustomReturnValueHandlers, setReturnValueHandlers, toString
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface org.springframework.context.SmartLifecycle
getPhase, isAutoStartup
-
Constructor Details
-
SimpAnnotationMethodMessageHandler
public SimpAnnotationMethodMessageHandler(SubscribableChannel clientInboundChannel, MessageChannel clientOutboundChannel, SimpMessageSendingOperations brokerTemplate) Create an instance of SimpAnnotationMethodMessageHandler with the given message channels and broker messaging template.- Parameters:
clientInboundChannel
- the channel for receiving messages from clients (e.g. WebSocket clients)clientOutboundChannel
- the channel for messages to clients (e.g. WebSocket clients)brokerTemplate
- a messaging template to send application messages to the broker
-
-
Method Details
-
setDestinationPrefixes
When this property is configured only messages to destinations matching one of the configured prefixes are eligible for handling. When there is a match the prefix is removed and only the remaining part of the destination is used for method-mapping purposes.By default, no prefixes are configured in which case all messages are eligible for handling.
Destination prefixes are expected to be slash-separated Strings and therefore a slash is automatically appended where missing to ensure a proper prefix-based match (i.e. matching complete segments).
Note however that the remaining portion of a destination after the prefix may use a different separator (e.g. commonly "." in messaging) depending on the configured
PathMatcher
.- Overrides:
setDestinationPrefixes
in classAbstractMethodMessageHandler<SimpMessageMappingInfo>
-
setMessageConverter
Configure aMessageConverter
to use to convert the payload of a message from its serialized form with a specific MIME type to an Object matching the target method parameter. The converter is also used when sending a message to the message broker.- See Also:
-
getMessageConverter
Return the configuredMessageConverter
. -
setConversionService
public void setConversionService(org.springframework.core.convert.ConversionService conversionService) Configure aConversionService
to use when resolving method arguments, for example message header values.By default,
DefaultFormattingConversionService
is used. -
getConversionService
public org.springframework.core.convert.ConversionService getConversionService()Return the configuredConversionService
. -
setPathMatcher
public void setPathMatcher(org.springframework.util.PathMatcher pathMatcher) Set the PathMatcher implementation to use for matching destinations against configured destination patterns.By default,
AntPathMatcher
is used. -
getPathMatcher
public org.springframework.util.PathMatcher getPathMatcher()Return the PathMatcher implementation to use for matching destinations. -
getValidator
@Nullable public org.springframework.validation.Validator getValidator()Return the configured Validator instance. -
setValidator
public void setValidator(@Nullable org.springframework.validation.Validator validator) Set the Validator instance used for validating@Payload
arguments.- See Also:
-
Validated
PayloadMethodArgumentResolver
-
setEmbeddedValueResolver
public void setEmbeddedValueResolver(org.springframework.util.StringValueResolver resolver) - Specified by:
setEmbeddedValueResolver
in interfaceorg.springframework.context.EmbeddedValueResolverAware
-
setHeaderInitializer
Configure aMessageHeaderInitializer
to pass on toHandlerMethodReturnValueHandlers
that send messages from controller return values.By default, this property is not set.
-
getHeaderInitializer
Return the configured header initializer. -
start
public final void start()- Specified by:
start
in interfaceorg.springframework.context.Lifecycle
-
stop
public final void stop()- Specified by:
stop
in interfaceorg.springframework.context.Lifecycle
-
stop
- Specified by:
stop
in interfaceorg.springframework.context.SmartLifecycle
-
isRunning
public final boolean isRunning()- Specified by:
isRunning
in interfaceorg.springframework.context.Lifecycle
-
initArgumentResolvers
Description copied from class:AbstractMethodMessageHandler
Return the list of argument resolvers to use. Invoked only if the resolvers have not already been set viaAbstractMethodMessageHandler.setArgumentResolvers(java.util.List<org.springframework.messaging.handler.invocation.HandlerMethodArgumentResolver>)
.Subclasses should also take into account custom argument types configured via
AbstractMethodMessageHandler.setCustomArgumentResolvers(java.util.List<org.springframework.messaging.handler.invocation.HandlerMethodArgumentResolver>)
.- Specified by:
initArgumentResolvers
in classAbstractMethodMessageHandler<SimpMessageMappingInfo>
-
initReturnValueHandlers
Description copied from class:AbstractMethodMessageHandler
Return the list of return value handlers to use. Invoked only if the return value handlers have not already been set viaAbstractMethodMessageHandler.setReturnValueHandlers(java.util.List<org.springframework.messaging.handler.invocation.HandlerMethodReturnValueHandler>)
.Subclasses should also take into account custom return value types configured via
AbstractMethodMessageHandler.setCustomReturnValueHandlers(java.util.List<org.springframework.messaging.handler.invocation.HandlerMethodReturnValueHandler>)
.- Specified by:
initReturnValueHandlers
in classAbstractMethodMessageHandler<SimpMessageMappingInfo>
-
getReturnValueHandlerLogger
Description copied from class:AbstractMethodMessageHandler
Return a logger to set onHandlerMethodReturnValueHandlerComposite
.- Overrides:
getReturnValueHandlerLogger
in classAbstractMethodMessageHandler<SimpMessageMappingInfo>
-
getHandlerMethodLogger
Description copied from class:AbstractMethodMessageHandler
Return a logger to set onInvocableHandlerMethod
.- Overrides:
getHandlerMethodLogger
in classAbstractMethodMessageHandler<SimpMessageMappingInfo>
-
isHandler
Description copied from class:AbstractMethodMessageHandler
Whether the given bean type should be introspected for messaging handling methods.- Specified by:
isHandler
in classAbstractMethodMessageHandler<SimpMessageMappingInfo>
-
getMappingForMethod
Description copied from class:AbstractMethodMessageHandler
Provide the mapping for a handler method.- Specified by:
getMappingForMethod
in classAbstractMethodMessageHandler<SimpMessageMappingInfo>
- Parameters:
method
- the method to provide a mapping forhandlerType
- the handler type, possibly a subtype of the method's declaring class- Returns:
- the mapping, or
null
if the method is not mapped
-
resolveEmbeddedValuesInDestinations
Resolve placeholder values in the given array of destinations.- Returns:
- a new array with updated destinations
- Since:
- 4.2
-
getDirectLookupDestinations
Description copied from class:AbstractMethodMessageHandler
Return destinations contained in the mapping that are not patterns and are therefore suitable for direct lookups.- Specified by:
getDirectLookupDestinations
in classAbstractMethodMessageHandler<SimpMessageMappingInfo>
-
getDestination
- Specified by:
getDestination
in classAbstractMethodMessageHandler<SimpMessageMappingInfo>
-
getLookupDestination
Description copied from class:AbstractMethodMessageHandler
Check whether the given destination (of an incoming message) matches to one of the configured destination prefixes and if so return the remaining portion of the destination after the matched prefix.If there are no matching prefixes, return
null
.If there are no destination prefixes, return the destination as is.
- Overrides:
getLookupDestination
in classAbstractMethodMessageHandler<SimpMessageMappingInfo>
-
getMatchingMapping
@Nullable protected SimpMessageMappingInfo getMatchingMapping(SimpMessageMappingInfo mapping, Message<?> message) Description copied from class:AbstractMethodMessageHandler
Check if a mapping matches the current message and return a possibly new mapping with conditions relevant to the current request.- Specified by:
getMatchingMapping
in classAbstractMethodMessageHandler<SimpMessageMappingInfo>
- Parameters:
mapping
- the mapping to get a match formessage
- the message being handled- Returns:
- the match or
null
if there is no match
-
getMappingComparator
Description copied from class:AbstractMethodMessageHandler
Return a comparator for sorting matching mappings. The returned comparator should sort 'better' matches higher.- Specified by:
getMappingComparator
in classAbstractMethodMessageHandler<SimpMessageMappingInfo>
- Parameters:
message
- the current Message- Returns:
- the comparator, never
null
-
handleMatch
protected void handleMatch(SimpMessageMappingInfo mapping, HandlerMethod handlerMethod, String lookupDestination, Message<?> message) - Overrides:
handleMatch
in classAbstractMethodMessageHandler<SimpMessageMappingInfo>
-
createExceptionHandlerMethodResolverFor
protected AbstractExceptionHandlerMethodResolver createExceptionHandlerMethodResolverFor(Class<?> beanType) - Specified by:
createExceptionHandlerMethodResolverFor
in classAbstractMethodMessageHandler<SimpMessageMappingInfo>
-