Top Description Inners Fields Constructors Methods
org.springframework.messaging.handler.invocation.reactive

public abstract Class AbstractMethodMessageHandler<T>

extends Object
implements ReactiveMessageHandler, ApplicationContextAware, InitializingBean, BeanNameAware
Class Inheritance
All Implemented Interfaces
org.springframework.beans.factory.BeanNameAware, org.springframework.beans.factory.Aware, org.springframework.beans.factory.InitializingBean, org.springframework.context.ApplicationContextAware, org.springframework.messaging.ReactiveMessageHandler
Known Direct Subclasses
org.springframework.messaging.handler.annotation.reactive.MessageMappingMessageHandler
Type Parameters
<T>
the type of the Object that contains information mapping information
Imports
java.lang.reflect.Method, java.util.ArrayList, .Arrays, .Collection, .Collections, .Comparator, .List, .Map, .Set, java.util.concurrent.ConcurrentHashMap, .CopyOnWriteArrayList, java.util.function.Function, .Predicate, java.util.stream.Collectors, org.apache.commons.logging.Log, .LogFactory, reactor.core.publisher.Mono, org.springframework.beans.factory.BeanNameAware, .InitializingBean, org.springframework.context.ApplicationContext, .ApplicationContextAware, org.springframework.core.MethodIntrospector, .ReactiveAdapterRegistry, org.springframework.lang.Nullable, org.springframework.messaging.Message, .MessagingException, .ReactiveMessageHandler, org.springframework.messaging.handler.HandlerMethod, .MessagingAdviceBean, org.springframework.messaging.handler.invocation.AbstractExceptionHandlerMethodResolver, org.springframework.util.Assert, .ClassUtils, .CollectionUtils, .MultiValueMap, .ObjectUtils, .RouteMatcher, .StringUtils

Abstract base class for reactive HandlerMethod-based message handling. Provides most of the logic required to discover handler methods at startup, find a matching handler method at runtime for a given message and invoke it.

Also supports discovering and invoking exception handling methods to process exceptions raised during message handling.

Author
Rossen Stoyanchev
Since
5.2

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
AbstractMethodMessageHandler.Match<T>

Container for matched mapping and HandlerMethod.

private class

Field Summary

Modifier and TypeField and Description
private ApplicationContext
private ArgumentResolverConfigurer
private String
private final Map<String, List<T>>
private final Map<T, HandlerMethod>
private Predicate<Class<?>>
pack-priv List<Object>
private final InvocableHelper
protected final Log
private ReturnValueHandlerConfigurer
private static final String
SCOPED_TARGET_NAME_PREFIX

Bean name prefix for target beans behind scoped proxies.

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
private void
addMatchesToCollection(Collection<T> mappingsToCheck, Message<?> message, List<AbstractMethodMessageHandler.Match<T>> matches)

public void
afterPropertiesSet()

Implements org.springframework.beans.factory.InitializingBean.afterPropertiesSet.

Invoked by the containing BeanFactory after it has set all bean properties and satisfied BeanFactoryAware, ApplicationContextAware etc.

This method allows the bean instance to perform validation of its overall configuration and final initialization when all bean properties have been set.

protected abstract AbstractExceptionHandlerMethodResolver

Returns:

the resolver to use
createExceptionMethodResolverFor
(Class<?>
the class in which an exception occurred during handling
beanType
)

Create a concrete instance of AbstractExceptionHandlerMethodResolver that finds exception handling methods based on some criteria, e.g. based on the presence of @MessageExceptionHandler.

private HandlerMethod
createHandlerMethod(Object handler, Method method)

Create a HandlerMethod instance from an Object handler that is either a handler instance or a String-based bean name.

protected final void
detectHandlerMethods(Object
the handler to check, either an instance of a Spring bean name
handler
)

Detect if the given handler has any methods that can handle messages and if so register it with the extracted mapping information.

protected T

Returns:

a new mapping or the same
extendMapping
(T
the mapping to be added
mapping
,
HandlerMethod
the target handler for the mapping
handlerMethod
)

This method is invoked just before mappings are added.

private String
formatMappings(Class<?> userType, Map<Method, T> methods)

public ApplicationContext
public ArgumentResolverConfigurer
getArgumentResolverConfigurer()

Return the configured custom resolvers for handler method arguments.

protected HandlerMethodArgumentResolverComposite
getArgumentResolvers()

Return the argument resolvers initialized during afterPropertiesSet().

public String
protected abstract RouteMatcher.Route
getDestination(Message<?> message)

Extract the destination from the given message.

public MultiValueMap<String, T>
getDestinationLookup()

Return a read-only multi-value map with a direct lookup of mappings, (e.g. for non-pattern destinations).

protected abstract Set<String>
getDirectLookupMappings(T mapping)

Return String-based destinations for the given mapping, if any, that can be used to find matches with a direct lookup (i.e. non-patterns).

private AbstractMethodMessageHandler.Match<T>
public Map<T, HandlerMethod>
getHandlerMethods()

Return a read-only map with all handler methods and their mappings.

public Predicate<Class<?>>
getHandlerPredicate()

Return the configured handler predicate.

protected abstract Comparator<T>

Returns:

the comparator, never null
getMappingComparator
(Message<?>
the current Message
message
)

Return a comparator for sorting matching mappings.

protected abstract T

Returns:

the mapping, or null if the method is not mapped
getMappingForMethod
(Method
the method to check
method
,
Class<?>
the handler type, possibly a subtype of the method's declaring class
handlerType
)

Obtain the mapping for the given method, if any.

protected abstract T

Returns:

the match or null if there is no match
getMatchingMapping
(T
the mapping to get a match for
mapping
,
Message<?>
the message being handled
message
)

Check if a mapping matches the current message and return a possibly new mapping with conditions relevant to the current request.

public ReactiveAdapterRegistry
getReactiveAdapterRegistry()

Return the configured registry for adapting reactive types.

public ReturnValueHandlerConfigurer
getReturnValueHandlerConfigurer()

Return the configured return value handlers.

protected Mono<Void>
handleMatch(T mapping, HandlerMethod handlerMethod, Message<?> message)

public Mono<Void>
handleMessage(Message<?>
the message to be handled
message
)

Implements org.springframework.messaging.ReactiveMessageHandler.handleMessage.

Handle the given message.
protected void
handleNoMatch(RouteMatcher.Route
the destination
destination
,
Message<?>
the message
message
)

Invoked when no matching handler is found.

protected abstract List<? extends HandlerMethodArgumentResolver>
initArgumentResolvers()

Return the list of argument resolvers to use.

private void
protected abstract List<? extends HandlerMethodReturnValueHandler>
initReturnValueHandlers()

Return the list of return value handlers to use.

protected void
registerExceptionHandlerAdvice(MessagingAdviceBean bean, AbstractExceptionHandlerMethodResolver resolver)

Subclasses can invoke this method to populate the MessagingAdviceBean cache (e.g. to support "global" @MessageExceptionHandler).

public final void
registerHandlerMethod(Object
the bean name of the handler or the handler instance
handler
,
Method
the method to register
method
,
T
the mapping conditions associated with the handler method
mapping
)

Register a handler method and its unique mapping.

public void
setApplicationContext(ApplicationContext
the ApplicationContext object to be used by this object
applicationContext
)

Implements org.springframework.context.ApplicationContextAware.setApplicationContext.

Set the ApplicationContext that this object runs in. Normally this call will be used to initialize the object.
public void
setArgumentResolverConfigurer(ArgumentResolverConfigurer configurer)

Configure custom resolvers for handler method arguments.

public void
setBeanName(String
the name of the bean in the factory. Note that this name is the actual bean name used in the factory, which may differ from the originally specified name: in particular for inner bean names, the actual bean name might have been made unique through appending "#..." suffixes. Use the BeanFactoryUtils#originalBeanName(String) method to extract the original bean name (without suffix), if desired.
name
)

Implements org.springframework.beans.factory.BeanNameAware.setBeanName.

Set the name of the bean in the bean factory that created this bean.
public void
setHandlerPredicate(Predicate<Class<?>> handlerPredicate)

Configure a predicate for selecting which Spring beans to check for the presence of message handler methods.

public void
setHandlers(List<Object>
the handlers to check
handlers
)

Manually configure the handlers to check for the presence of message handling methods, which also disables auto-detection via a handlerPredicate.

public void
setReactiveAdapterRegistry(ReactiveAdapterRegistry registry)

Configure the registry for adapting various reactive types.

public void
setReturnValueHandlerConfigurer(ReturnValueHandlerConfigurer configurer)

Configure custom return value handlers for handler methods.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

applicationContextback to summary
private ApplicationContext applicationContext
Annotations
@Nullable
argumentResolverConfigurerback to summary
private ArgumentResolverConfigurer argumentResolverConfigurer
beanNameback to summary
private String beanName
Annotations
@Nullable
destinationLookupback to summary
private final Map<String, List<T>> destinationLookup
handlerMethodsback to summary
private final Map<T, HandlerMethod> handlerMethods
handlerPredicateback to summary
private Predicate<Class<?>> handlerPredicate
Annotations
@Nullable
handlersback to summary
pack-priv List<Object> handlers
Annotations
@Nullable
invocableHelperback to summary
private final InvocableHelper invocableHelper
loggerback to summary
protected final Log logger
returnValueHandlerConfigurerback to summary
private ReturnValueHandlerConfigurer returnValueHandlerConfigurer
SCOPED_TARGET_NAME_PREFIXback to summary
private static final String SCOPED_TARGET_NAME_PREFIX

Bean name prefix for target beans behind scoped proxies. Used to exclude those targets from handler method detection, in favor of the corresponding proxies.

We're not checking the autowire-candidate status here, which is how the proxy target filtering problem is being handled at the autowiring level, since autowire-candidate may have been turned to false for other reasons, while still expecting the bean to be eligible for handler methods.

Originally defined in org.springframework.aop.scope.ScopedProxyUtils but duplicated here to avoid a hard dependency on the spring-aop module.

Constructor Detail

AbstractMethodMessageHandlerback to summary
public AbstractMethodMessageHandler()

Method Detail

addMatchesToCollectionback to summary
private void addMatchesToCollection(Collection<T> mappingsToCheck, Message<?> message, List<AbstractMethodMessageHandler.Match<T>> matches)
afterPropertiesSetback to summary
public void afterPropertiesSet()

Implements org.springframework.beans.factory.InitializingBean.afterPropertiesSet.

Doc from org.springframework.beans.factory.InitializingBean.afterPropertiesSet.

Invoked by the containing BeanFactory after it has set all bean properties and satisfied BeanFactoryAware, ApplicationContextAware etc.

This method allows the bean instance to perform validation of its overall configuration and final initialization when all bean properties have been set.

Annotations
@Override
createExceptionMethodResolverForback to summary
protected abstract AbstractExceptionHandlerMethodResolver createExceptionMethodResolverFor(Class<?> beanType)

Create a concrete instance of AbstractExceptionHandlerMethodResolver that finds exception handling methods based on some criteria, e.g. based on the presence of @MessageExceptionHandler.

Parameters
beanType:Class<?>

the class in which an exception occurred during handling

Returns:AbstractExceptionHandlerMethodResolver

the resolver to use

createHandlerMethodback to summary
private HandlerMethod createHandlerMethod(Object handler, Method method)

Create a HandlerMethod instance from an Object handler that is either a handler instance or a String-based bean name.

detectHandlerMethodsback to summary
protected final void detectHandlerMethods(Object handler)

Detect if the given handler has any methods that can handle messages and if so register it with the extracted mapping information.

Note

This method is protected and can be invoked by subclasses, but this should be done on startup only as documented in registerHandlerMethod.

Parameters
handler:Object

the handler to check, either an instance of a Spring bean name

extendMappingback to summary
protected T extendMapping(T mapping, HandlerMethod handlerMethod)

This method is invoked just before mappings are added. It allows subclasses to update the mapping with the HandlerMethod in mind. This can be useful when the method signature is used to refine the mapping, e.g. based on the cardinality of input and output.

By default this method returns the mapping that is passed in.

Parameters
mapping:T

the mapping to be added

handlerMethod:HandlerMethod

the target handler for the mapping

Returns:T

a new mapping or the same

Since
5.2.2
formatMappingsback to summary
private String formatMappings(Class<?> userType, Map<Method, T> methods)
getApplicationContextback to summary
public ApplicationContext getApplicationContext()
Annotations
@Nullable
getArgumentResolverConfigurerback to summary
public ArgumentResolverConfigurer getArgumentResolverConfigurer()

Return the configured custom resolvers for handler method arguments.

getArgumentResolversback to summary
protected HandlerMethodArgumentResolverComposite getArgumentResolvers()

Return the argument resolvers initialized during afterPropertiesSet(). Primarily for internal use in subclasses.

Since
5.2.2
getBeanNameback to summary
public String getBeanName()
getDestinationback to summary
protected abstract RouteMatcher.Route getDestination(Message<?> message)

Extract the destination from the given message.

Annotations
@Nullable
See Also
getDirectLookupMappings(Object)
getDestinationLookupback to summary
public MultiValueMap<String, T> getDestinationLookup()

Return a read-only multi-value map with a direct lookup of mappings, (e.g. for non-pattern destinations).

getDirectLookupMappingsback to summary
protected abstract Set<String> getDirectLookupMappings(T mapping)

Return String-based destinations for the given mapping, if any, that can be used to find matches with a direct lookup (i.e. non-patterns).

Note

This is completely optional. The mapping metadata for a subclass may support neither direct lookups, nor String based destinations.

getHandlerMethodback to summary
private AbstractMethodMessageHandler.Match<T> getHandlerMethod(Message<?> message)
Annotations
@Nullable
getHandlerMethodsback to summary
public Map<T, HandlerMethod> getHandlerMethods()

Return a read-only map with all handler methods and their mappings.

getHandlerPredicateback to summary
public Predicate<Class<?>> getHandlerPredicate()

Return the configured handler predicate.

Annotations
@Nullable
getMappingComparatorback to summary
protected abstract Comparator<T> getMappingComparator(Message<?> message)

Return a comparator for sorting matching mappings. The returned comparator should sort 'better' matches higher.

Parameters
message:Message<?>

the current Message

Returns:Comparator<T>

the comparator, never null

getMappingForMethodback to summary
protected abstract T getMappingForMethod(Method method, Class<?> handlerType)

Obtain the mapping for the given method, if any.

Parameters
method:Method

the method to check

handlerType:Class<?>

the handler type, possibly a subtype of the method's declaring class

Returns:T

the mapping, or null if the method is not mapped

Annotations
@Nullable
getMatchingMappingback to summary
protected abstract T getMatchingMapping(T mapping, Message<?> message)

Check if a mapping matches the current message and return a possibly new mapping with conditions relevant to the current request.

Parameters
mapping:T

the mapping to get a match for

message:Message<?>

the message being handled

Returns:T

the match or null if there is no match

Annotations
@Nullable
getReactiveAdapterRegistryback to summary
public ReactiveAdapterRegistry getReactiveAdapterRegistry()

Return the configured registry for adapting reactive types.

getReturnValueHandlerConfigurerback to summary
public ReturnValueHandlerConfigurer getReturnValueHandlerConfigurer()

Return the configured return value handlers.

handleMatchback to summary
protected Mono<Void> handleMatch(T mapping, HandlerMethod handlerMethod, Message<?> message)
handleMessageback to summary
public Mono<Void> handleMessage(Message<?> message) throws MessagingException

Implements org.springframework.messaging.ReactiveMessageHandler.handleMessage.

Doc from org.springframework.messaging.ReactiveMessageHandler.handleMessage.

Handle the given message.

Parameters
message:Message<?>

the message to be handled

Returns:Mono<Void>

a completion Mono for the result of the message handling

Annotations
@Override
handleNoMatchback to summary
protected void handleNoMatch(RouteMatcher.Route destination, Message<?> message)

Invoked when no matching handler is found.

Parameters
destination:RouteMatcher.Route

the destination

message:Message<?>

the message

initArgumentResolversback to summary
protected abstract List<? extends HandlerMethodArgumentResolver> initArgumentResolvers()

Return the list of argument resolvers to use.

Subclasses should also take into account custom argument types configured via setArgumentResolverConfigurer.

initHandlerMethodsback to summary
private void initHandlerMethods()
initReturnValueHandlersback to summary
protected abstract List<? extends HandlerMethodReturnValueHandler> initReturnValueHandlers()

Return the list of return value handlers to use.

Subclasses should also take into account custom return value types configured via setReturnValueHandlerConfigurer.

registerExceptionHandlerAdviceback to summary
protected void registerExceptionHandlerAdvice(MessagingAdviceBean bean, AbstractExceptionHandlerMethodResolver resolver)

Subclasses can invoke this method to populate the MessagingAdviceBean cache (e.g. to support "global" @MessageExceptionHandler).

registerHandlerMethodback to summary
public final void registerHandlerMethod(Object handler, Method method, T mapping)

Register a handler method and its unique mapping.

Note

As of 5.3 this method is public (rather than protected) and can be used both at startup and at runtime.

Parameters
handler:Object

the bean name of the handler or the handler instance

method:Method

the method to register

mapping:T

the mapping conditions associated with the handler method

Exceptions
IllegalStateException:
if another method was already registered under the same mapping
setApplicationContextback to summary
public void setApplicationContext(ApplicationContext applicationContext)

Implements org.springframework.context.ApplicationContextAware.setApplicationContext.

Doc from org.springframework.context.ApplicationContextAware.setApplicationContext.

Set the ApplicationContext that this object runs in. Normally this call will be used to initialize the object.

Invoked after population of normal bean properties but before an init callback such as org.springframework.beans.factory.InitializingBean#afterPropertiesSet() or a custom init-method. Invoked after ResourceLoaderAware#setResourceLoader, ApplicationEventPublisherAware#setApplicationEventPublisher and MessageSourceAware, if applicable.

Parameters
applicationContext:ApplicationContext

the ApplicationContext object to be used by this object

Annotations
@Override
setArgumentResolverConfigurerback to summary
public void setArgumentResolverConfigurer(ArgumentResolverConfigurer configurer)

Configure custom resolvers for handler method arguments.

setBeanNameback to summary
public void setBeanName(String name)

Implements org.springframework.beans.factory.BeanNameAware.setBeanName.

Doc from org.springframework.beans.factory.BeanNameAware.setBeanName.

Set the name of the bean in the bean factory that created this bean.

Invoked after population of normal bean properties but before an init callback such as InitializingBean#afterPropertiesSet() or a custom init-method.

Parameters
name:String

the name of the bean in the factory. Note that this name is the actual bean name used in the factory, which may differ from the originally specified name: in particular for inner bean names, the actual bean name might have been made unique through appending "#..." suffixes. Use the BeanFactoryUtils#originalBeanName(String) method to extract the original bean name (without suffix), if desired.

Annotations
@Override
setHandlerPredicateback to summary
public void setHandlerPredicate(Predicate<Class<?>> handlerPredicate)

Configure a predicate for selecting which Spring beans to check for the presence of message handler methods.

This is not set by default. However, subclasses may initialize it to some default strategy (e.g. @Controller classes).

See Also
setHandlers(List)
setHandlersback to summary
public void setHandlers(List<Object> handlers)

Manually configure the handlers to check for the presence of message handling methods, which also disables auto-detection via a handlerPredicate. If you do not want to disable auto-detection, then call this method first, and then set the handler predicate.

Parameters
handlers:List<Object>

the handlers to check

setReactiveAdapterRegistryback to summary
public void setReactiveAdapterRegistry(ReactiveAdapterRegistry registry)

Configure the registry for adapting various reactive types.

By default this is an instance of ReactiveAdapterRegistry with default settings.

setReturnValueHandlerConfigurerback to summary
public void setReturnValueHandlerConfigurer(ReturnValueHandlerConfigurer configurer)

Configure custom return value handlers for handler methods.

org.springframework.messaging.handler.invocation.reactive back to summary

private Class AbstractMethodMessageHandler.Match<T>

extends Object
Class Inheritance
  • java.lang.Object
  • org.springframework.messaging.handler.invocation.reactive.AbstractMethodMessageHandler.Match

Container for matched mapping and HandlerMethod. Used for best match comparison and for access to mapping information.

Field Summary

Modifier and TypeField and Description
private final HandlerMethod
private final T

Constructor Summary

AccessConstructor and Description
pack-priv
Match(T mapping, HandlerMethod handlerMethod)

Method Summary

Modifier and TypeMethod and Description
public String
toString()

Overrides java.lang.Object.toString.

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

Field Detail

handlerMethodback to summary
private final HandlerMethod handlerMethod
mappingback to summary
private final T mapping

Constructor Detail

Matchback to summary
pack-priv Match(T mapping, HandlerMethod handlerMethod)

Method Detail

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.handler.invocation.reactive back to summary

private Class AbstractMethodMessageHandler.MatchComparator

extends Object
implements Comparator<AbstractMethodMessageHandler.Match<T>>
Class Inheritance
  • java.lang.Object
  • org.springframework.messaging.handler.invocation.reactive.AbstractMethodMessageHandler.MatchComparator
All Implemented Interfaces
java.util.Comparator

Field Summary

Modifier and TypeField and Description
private final Comparator<T>

Constructor Summary

AccessConstructor and Description
pack-priv
MatchComparator(Comparator<T> comparator)

Method Summary

Modifier and TypeMethod and Description
public int
compare(AbstractMethodMessageHandler.Match<T>
the first object to be compared.
match1
,
AbstractMethodMessageHandler.Match<T>
the second object to be compared.
match2
)

Implements java.util.Comparator.compare.

Compares its two arguments for order.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

comparatorback to summary
private final Comparator<T> comparator

Constructor Detail

MatchComparatorback to summary
pack-priv MatchComparator(Comparator<T> comparator)

Method Detail

compareback to summary
public int compare(AbstractMethodMessageHandler.Match<T> match1, AbstractMethodMessageHandler.Match<T> match2)

Implements java.util.Comparator.compare.

Doc from java.util.Comparator.compare.

Compares its two arguments for order. Returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.

The implementor must ensure that signum(compare(x, y)) == -signum(compare(y, x)) for all x and y. (This implies that compare(x, y) must throw an exception if and only if compare(y, x) throws an exception.)

The implementor must also ensure that the relation is transitive: ((compare(x, y)>0) && (compare(y, z)>0)) implies compare(x, z)>0.

Finally, the implementor must ensure that compare(x, y)==0 implies that signum(compare(x, z))==signum(compare(y, z)) for all z.

Parameters
match1:AbstractMethodMessageHandler.Match<T>

the first object to be compared.

match2:AbstractMethodMessageHandler.Match<T>

the second object to be compared.

Returns:int

a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.

Annotations
@Override