T
- the type of the Object that contains information mapping informationpublic abstract class AbstractMethodMessageHandler<T> extends Object implements ReactiveMessageHandler, ApplicationContextAware, InitializingBean, BeanNameAware
Also supports discovering and invoking exception handling methods to process exceptions raised during message handling.
Constructor and Description |
---|
AbstractMethodMessageHandler() |
Modifier and Type | Method and Description |
---|---|
void |
afterPropertiesSet()
Invoked by the containing
BeanFactory after it has set all bean properties
and satisfied BeanFactoryAware , ApplicationContextAware etc. |
protected abstract AbstractExceptionHandlerMethodResolver |
createExceptionMethodResolverFor(Class<?> beanType)
Create a concrete instance of
AbstractExceptionHandlerMethodResolver
that finds exception handling methods based on some criteria, e.g. |
protected 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.
|
ApplicationContext |
getApplicationContext() |
ArgumentResolverConfigurer |
getArgumentResolverConfigurer()
Return the configured custom resolvers for handler method arguments.
|
String |
getBeanName() |
protected abstract RouteMatcher.Route |
getDestination(Message<?> message)
Extract the destination from the given message.
|
MultiValueMap<String,T> |
getDestinationLookup()
Return a read-only multi-value map with a direct lookup of mappings,
(e.g.
|
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.
|
Map<T,HandlerMethod> |
getHandlerMethods()
Return a read-only map with all handler methods and their mappings.
|
Predicate<Class<?>> |
getHandlerPredicate()
Return the
configured handler predicate. |
protected abstract Comparator<T> |
getMappingComparator(Message<?> message)
Return a comparator for sorting matching mappings.
|
protected abstract T |
getMappingForMethod(Method method,
Class<?> handlerType)
Obtain the mapping for the given method, if any.
|
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.
|
ReactiveAdapterRegistry |
getReactiveAdapterRegistry()
Return the configured registry for adapting reactive types.
|
ReturnValueHandlerConfigurer |
getReturnValueHandlerConfigurer()
Return the configured return value handlers.
|
protected reactor.core.publisher.Mono<Void> |
handleMatch(T mapping,
HandlerMethod handlerMethod,
Message<?> message) |
reactor.core.publisher.Mono<Void> |
handleMessage(Message<?> message)
Handle the given message.
|
protected void |
handleNoMatch(RouteMatcher.Route destination,
Message<?> message)
Invoked when no matching handler is found.
|
protected abstract List<? extends HandlerMethodArgumentResolver> |
initArgumentResolvers()
Return the list of argument resolvers to use.
|
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.
|
protected void |
registerHandlerMethod(Object handler,
Method method,
T mapping)
Register a handler method and its unique mapping.
|
void |
setApplicationContext(ApplicationContext applicationContext)
Set the ApplicationContext that this object runs in.
|
void |
setArgumentResolverConfigurer(ArgumentResolverConfigurer configurer)
Configure custom resolvers for handler method arguments.
|
void |
setBeanName(String name)
Set the name of the bean in the bean factory that created this bean.
|
void |
setHandlerPredicate(Predicate<Class<?>> handlerPredicate)
Configure a predicate for selecting which Spring beans to check for the
presence of message handler methods.
|
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 . |
void |
setReactiveAdapterRegistry(ReactiveAdapterRegistry registry)
Configure the registry for adapting various reactive types.
|
void |
setReturnValueHandlerConfigurer(ReturnValueHandlerConfigurer configurer)
Configure custom return value handlers for handler metohds.
|
protected final Log logger
public void setHandlerPredicate(@Nullable Predicate<Class<?>> handlerPredicate)
This is not set by default. However sub-classes may initialize it to
some default strategy (e.g. @Controller
classes).
setHandlers(List)
@Nullable public Predicate<Class<?>> getHandlerPredicate()
configured
handler predicate.public void setHandlers(List<Object> handlers)
handlerPredicate
. If you do not
want to disable auto-detection, then call this method first, and then set
the handler predicate.handlers
- the handlers to checkpublic void setArgumentResolverConfigurer(ArgumentResolverConfigurer configurer)
public ArgumentResolverConfigurer getArgumentResolverConfigurer()
public void setReturnValueHandlerConfigurer(ReturnValueHandlerConfigurer configurer)
public ReturnValueHandlerConfigurer getReturnValueHandlerConfigurer()
public void setReactiveAdapterRegistry(ReactiveAdapterRegistry registry)
By default this is an instance of ReactiveAdapterRegistry
with
default settings.
public ReactiveAdapterRegistry getReactiveAdapterRegistry()
public void setApplicationContext(@Nullable ApplicationContext applicationContext)
ApplicationContextAware
Invoked after population of normal bean properties but before an init callback such
as InitializingBean.afterPropertiesSet()
or a custom init-method. Invoked after ResourceLoaderAware.setResourceLoader(org.springframework.core.io.ResourceLoader)
,
ApplicationEventPublisherAware.setApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher)
and
MessageSourceAware
, if applicable.
setApplicationContext
in interface ApplicationContextAware
applicationContext
- the ApplicationContext object to be used by this objectBeanInitializationException
@Nullable public ApplicationContext getApplicationContext()
public void setBeanName(String name)
BeanNameAware
Invoked after population of normal bean properties but before an
init callback such as InitializingBean.afterPropertiesSet()
or a custom init-method.
setBeanName
in interface BeanNameAware
name
- 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.public String getBeanName()
protected void registerExceptionHandlerAdvice(MessagingAdviceBean bean, AbstractExceptionHandlerMethodResolver resolver)
@MessageExceptionHandler
).public Map<T,HandlerMethod> getHandlerMethods()
public MultiValueMap<String,T> getDestinationLookup()
public void afterPropertiesSet()
InitializingBean
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.
afterPropertiesSet
in interface InitializingBean
protected abstract List<? extends HandlerMethodArgumentResolver> initArgumentResolvers()
Subclasses should also take into account custom argument types configured via
setArgumentResolverConfigurer(org.springframework.messaging.handler.invocation.reactive.ArgumentResolverConfigurer)
.
protected abstract List<? extends HandlerMethodReturnValueHandler> initReturnValueHandlers()
Subclasses should also take into account custom return value types configured
via setReturnValueHandlerConfigurer(org.springframework.messaging.handler.invocation.reactive.ReturnValueHandlerConfigurer)
.
protected final void detectHandlerMethods(Object handler)
Note: This method is protected and can be invoked by
subclasses, but this should be done on startup only as documented in
registerHandlerMethod(java.lang.Object, java.lang.reflect.Method, T)
.
handler
- the handler to check, either an instance of a Spring bean name@Nullable protected abstract T getMappingForMethod(Method method, Class<?> handlerType)
method
- the method to checkhandlerType
- the handler type, possibly a sub-type of the method's declaring classnull
if the method is not mappedprotected final void registerHandlerMethod(Object handler, Method method, T mapping)
Note: This method is protected and can be invoked by subclasses. Keep in mind however that the registration is not protected for concurrent use, and is expected to be done on startup.
handler
- the bean name of the handler or the handler instancemethod
- the method to registermapping
- the mapping conditions associated with the handler methodIllegalStateException
- if another method was already registered
under the same mappingprotected abstract Set<String> getDirectLookupMappings(T mapping)
Note: This is completely optional. The mapping metadata for a subclass may support neither direct lookups, nor String based destinations.
public reactor.core.publisher.Mono<Void> handleMessage(Message<?> message) throws MessagingException
ReactiveMessageHandler
handleMessage
in interface ReactiveMessageHandler
message
- the message to be handledMono
for the result of the message handlingMessagingException
protected reactor.core.publisher.Mono<Void> handleMatch(T mapping, HandlerMethod handlerMethod, Message<?> message)
@Nullable protected abstract RouteMatcher.Route getDestination(Message<?> message)
getDirectLookupMappings(Object)
@Nullable protected abstract T getMatchingMapping(T mapping, Message<?> message)
mapping
- the mapping to get a match formessage
- the message being handlednull
if there is no matchprotected abstract Comparator<T> getMappingComparator(Message<?> message)
message
- the current Messagenull
protected void handleNoMatch(@Nullable RouteMatcher.Route destination, Message<?> message)
destination
- the destinationmessage
- the messageprotected abstract AbstractExceptionHandlerMethodResolver createExceptionMethodResolverFor(Class<?> beanType)
AbstractExceptionHandlerMethodResolver
that finds exception handling methods based on some criteria, e.g. based
on the presence of @MessageExceptionHandler
.beanType
- the class in which an exception occurred during handling