public class MessageMappingMessageHandler extends AbstractMethodMessageHandler<CompositeMessageCondition> implements EmbeddedValueResolverAware
AbstractMethodMessageHandler
for reactive, non-blocking
handling of messages via @MessageMapping
methods.
By default such methods are detected in @Controller
Spring beans but
that can be changed via setHandlerPredicate(Predicate)
.
Payloads for incoming messages are decoded through the configured
setDecoders(List)
decoders, with the help of
PayloadMethodArgumentResolver
.
There is no default handling for return values but
AbstractMethodMessageHandler.setReturnValueHandlerConfigurer(org.springframework.messaging.handler.invocation.reactive.ReturnValueHandlerConfigurer)
can be used to configure custom
return value handlers. Sub-classes may also override
initReturnValueHandlers()
to set up default return value handlers.
AbstractEncoderMethodReturnValueHandler
Modifier and Type | Field and Description |
---|---|
private ConversionService |
conversionService |
private java.util.List<Decoder<?>> |
decoders |
private java.util.function.Predicate<java.lang.Class<?>> |
handlerPredicate |
private PathMatcher |
pathMatcher |
private Validator |
validator |
private StringValueResolver |
valueResolver |
logger
Constructor and Description |
---|
MessageMappingMessageHandler() |
Modifier and Type | Method and Description |
---|---|
protected AbstractExceptionHandlerMethodResolver |
createExceptionMethodResolverFor(java.lang.Class<?> beanType)
Create a concrete instance of
AbstractExceptionHandlerMethodResolver
that finds exception handling methods based on some criteria, e.g. |
private CompositeMessageCondition |
getCondition(java.lang.reflect.AnnotatedElement element) |
ConversionService |
getConversionService()
Return the configured ConversionService.
|
java.util.List<? extends Decoder<?>> |
getDecoders()
Return the configured decoders.
|
protected java.lang.String |
getDestination(Message<?> message)
Extract a String-based destination, if any, that can be used to perform
a direct look up into the registered mappings.
|
protected java.util.Set<java.lang.String> |
getDirectLookupMappings(CompositeMessageCondition mapping)
Return String-based destinations for the given mapping, if any, that can
be used to find matches with a direct lookup (i.e.
|
java.util.function.Predicate<java.lang.Class<?>> |
getHandlerPredicate()
Return the
configured handler predicate. |
protected java.util.Comparator<CompositeMessageCondition> |
getMappingComparator(Message<?> message)
Return a comparator for sorting matching mappings.
|
protected CompositeMessageCondition |
getMappingForMethod(java.lang.reflect.Method method,
java.lang.Class<?> handlerType)
Obtain the mapping for the given method, if any.
|
protected CompositeMessageCondition |
getMatchingMapping(CompositeMessageCondition mapping,
Message<?> message)
Check if a mapping matches the current message and return a possibly
new mapping with conditions relevant to the current request.
|
PathMatcher |
getPathMatcher()
Return the PathMatcher implementation to use for matching destinations.
|
Validator |
getValidator()
Return the configured Validator instance.
|
protected <any> |
handleMatch(CompositeMessageCondition mapping,
HandlerMethod handlerMethod,
Message<?> message) |
protected java.util.List<? extends HandlerMethodArgumentResolver> |
initArgumentResolvers()
Return the list of argument resolvers to use.
|
protected java.util.function.Predicate<java.lang.Class<?>> |
initHandlerPredicate()
Return the predicate to use to check whether a given Spring bean should
be introspected for message handling methods.
|
protected java.util.List<? extends HandlerMethodReturnValueHandler> |
initReturnValueHandlers()
Return the list of return value handlers to use.
|
void |
setAutoDetectDisabled()
Disable auto-detection of
@MessageMapping methods, e.g. |
void |
setConversionService(ConversionService conversionService)
Configure a
ConversionService to use for type conversion of
String based values, e.g. |
void |
setDecoders(java.util.List<? extends Decoder<?>> decoders)
Configure the decoders to use for incoming payloads.
|
void |
setEmbeddedValueResolver(StringValueResolver resolver)
Set the StringValueResolver to use for resolving embedded definition values.
|
void |
setHandlerPredicate(java.util.function.Predicate<java.lang.Class<?>> handlerPredicate)
Configure the predicate to use for selecting which Spring beans to check
for
@MessageMapping methods. |
void |
setHandlers(java.util.List<java.lang.Object> handlers)
Manually configure handlers to check for
@MessageMapping methods. |
void |
setPathMatcher(PathMatcher pathMatcher)
Set the PathMatcher implementation to use for matching destinations
against configured destination patterns.
|
void |
setValidator(Validator validator)
Set the Validator instance used for validating
@Payload arguments. |
afterPropertiesSet, detectHandlerMethods, getApplicationContext, getArgumentResolverConfigurer, getBeanName, getDestinationLookup, getHandlerMethods, getReactiveAdapterRegistry, getReturnValueHandlerConfigurer, handleMessage, handleNoMatch, registerExceptionHandlerAdvice, registerHandlerMethod, setApplicationContext, setArgumentResolverConfigurer, setBeanName, setReactiveAdapterRegistry, setReturnValueHandlerConfigurer
@Nullable private java.util.function.Predicate<java.lang.Class<?>> handlerPredicate
private final java.util.List<Decoder<?>> decoders
private PathMatcher pathMatcher
private ConversionService conversionService
@Nullable private StringValueResolver valueResolver
public void setHandlers(java.util.List<java.lang.Object> handlers)
@MessageMapping
methods.
Note: the given handlers are not required to be
annotated with @Controller
. Consider also using
setAutoDetectDisabled()
if the intent is to use these handlers
instead of, and not in addition to @Controller
classes. Or
alternatively use setHandlerPredicate(Predicate)
to select a
different set of beans based on a different criteria.
handlers
- the handlers to registersetAutoDetectDisabled()
,
setHandlerPredicate(Predicate)
public void setHandlerPredicate(@Nullable java.util.function.Predicate<java.lang.Class<?>> handlerPredicate)
@MessageMapping
methods. When set to null
,
auto-detection is turned off which is what
setAutoDetectDisabled()
does internally.
The predicate used by default selects @Controller
classes.
setHandlers(List)
,
setAutoDetectDisabled()
@Nullable public java.util.function.Predicate<java.lang.Class<?>> getHandlerPredicate()
configured
handler predicate.public void setAutoDetectDisabled()
@MessageMapping
methods, e.g. in
@Controller
s, by setting setHandlerPredicate(null)
.public void setDecoders(java.util.List<? extends Decoder<?>> decoders)
public java.util.List<? extends Decoder<?>> getDecoders()
public void setValidator(@Nullable Validator validator)
@Payload
arguments.Validated
,
PayloadMethodArgumentResolver
public void setPathMatcher(PathMatcher pathMatcher)
By default, AntPathMatcher
is used with separator set to ".".
public PathMatcher getPathMatcher()
public void setConversionService(ConversionService conversionService)
ConversionService
to use for type conversion of
String based values, e.g. in destination variables or headers.
By default DefaultFormattingConversionService
is used.
conversionService
- the conversion service to usepublic ConversionService getConversionService()
public void setEmbeddedValueResolver(StringValueResolver resolver)
EmbeddedValueResolverAware
setEmbeddedValueResolver
in interface EmbeddedValueResolverAware
protected java.util.List<? extends HandlerMethodArgumentResolver> initArgumentResolvers()
AbstractMethodMessageHandler
Subclasses should also take into account custom argument types configured via
AbstractMethodMessageHandler.setArgumentResolverConfigurer(org.springframework.messaging.handler.invocation.reactive.ArgumentResolverConfigurer)
.
initArgumentResolvers
in class AbstractMethodMessageHandler<CompositeMessageCondition>
protected java.util.List<? extends HandlerMethodReturnValueHandler> initReturnValueHandlers()
AbstractMethodMessageHandler
Subclasses should also take into account custom return value types configured
via AbstractMethodMessageHandler.setReturnValueHandlerConfigurer(org.springframework.messaging.handler.invocation.reactive.ReturnValueHandlerConfigurer)
.
initReturnValueHandlers
in class AbstractMethodMessageHandler<CompositeMessageCondition>
protected java.util.function.Predicate<java.lang.Class<?>> initHandlerPredicate()
AbstractMethodMessageHandler
null
is
returned, auto-detection is effectively disabled.initHandlerPredicate
in class AbstractMethodMessageHandler<CompositeMessageCondition>
protected CompositeMessageCondition getMappingForMethod(java.lang.reflect.Method method, java.lang.Class<?> handlerType)
AbstractMethodMessageHandler
getMappingForMethod
in class AbstractMethodMessageHandler<CompositeMessageCondition>
method
- the method to checkhandlerType
- the handler type, possibly a sub-type of the method's declaring classnull
if the method is not mapped@Nullable private CompositeMessageCondition getCondition(java.lang.reflect.AnnotatedElement element)
protected java.util.Set<java.lang.String> getDirectLookupMappings(CompositeMessageCondition mapping)
AbstractMethodMessageHandler
Note: This is completely optional. The mapping metadata for a sub-class may support neither direct lookups, nor String based destinations.
getDirectLookupMappings
in class AbstractMethodMessageHandler<CompositeMessageCondition>
protected java.lang.String getDestination(Message<?> message)
AbstractMethodMessageHandler
Note: This is completely optional. The mapping metadata for a sub-class may support neither direct lookups, nor String based destinations.
getDestination
in class AbstractMethodMessageHandler<CompositeMessageCondition>
AbstractMethodMessageHandler.getDirectLookupMappings(Object)
protected CompositeMessageCondition getMatchingMapping(CompositeMessageCondition mapping, Message<?> message)
AbstractMethodMessageHandler
getMatchingMapping
in class AbstractMethodMessageHandler<CompositeMessageCondition>
mapping
- the mapping to get a match formessage
- the message being handlednull
if there is no matchprotected java.util.Comparator<CompositeMessageCondition> getMappingComparator(Message<?> message)
AbstractMethodMessageHandler
getMappingComparator
in class AbstractMethodMessageHandler<CompositeMessageCondition>
message
- the current Messagenull
protected AbstractExceptionHandlerMethodResolver createExceptionMethodResolverFor(java.lang.Class<?> beanType)
AbstractMethodMessageHandler
AbstractExceptionHandlerMethodResolver
that finds exception handling methods based on some criteria, e.g. based
on the presence of @MessageExceptionHandler
.createExceptionMethodResolverFor
in class AbstractMethodMessageHandler<CompositeMessageCondition>
beanType
- the class in which an exception occurred during handlingprotected <any> handleMatch(CompositeMessageCondition mapping, HandlerMethod handlerMethod, Message<?> message)
handleMatch
in class AbstractMethodMessageHandler<CompositeMessageCondition>