T
- the type of the Object that contains information mapping a
HandlerMethod
to incoming messagespublic abstract class AbstractMethodMessageHandler<T> extends Object implements MessageHandler, ApplicationContextAware, InitializingBean
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 a BeanFactory after it has set all bean properties supplied
(and satisfied BeanFactoryAware and ApplicationContextAware).
|
protected abstract AbstractExceptionHandlerMethodResolver |
createExceptionHandlerMethodResolverFor(Class<?> beanType) |
protected 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 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() |
List<HandlerMethodArgumentResolver> |
getArgumentResolvers() |
List<HandlerMethodArgumentResolver> |
getCustomArgumentResolvers()
Return the configured custom argument resolvers, if any.
|
List<HandlerMethodReturnValueHandler> |
getCustomReturnValueHandlers()
Return the configured custom return value handlers, if any.
|
protected abstract String |
getDestination(Message<?> message) |
Collection<String> |
getDestinationPrefixes()
Return the configured destination prefixes.
|
protected abstract Set<String> |
getDirectLookupDestinations(T mapping)
Return destinations contained in the mapping that are not patterns and are
therefore suitable for direct lookups.
|
protected InvocableHandlerMethod |
getExceptionHandlerMethod(HandlerMethod handlerMethod,
Exception exception)
Find an
@MessageExceptionHandler method for the given exception. |
Map<T,HandlerMethod> |
getHandlerMethods()
Return a map with all handler methods and their mappings.
|
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 abstract Comparator<T> |
getMappingComparator(Message<?> message)
Return a comparator for sorting matching mappings.
|
protected abstract T |
getMappingForMethod(Method method,
Class<?> handlerType)
Provide the mapping for a handler method.
|
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.
|
List<HandlerMethodReturnValueHandler> |
getReturnValueHandlers() |
protected void |
handleMatch(T mapping,
HandlerMethod handlerMethod,
String lookupDestination,
Message<?> message) |
void |
handleMessage(Message<?> message)
Handle the given message.
|
protected void |
handleMessageInternal(Message<?> message,
String lookupDestination) |
protected void |
handleNoMatch(Set<T> ts,
String lookupDestination,
Message<?> message) |
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 abstract boolean |
isHandler(Class<?> beanType)
Whether the given bean type should be introspected for messaging handling methods.
|
protected void |
processHandlerMethodException(HandlerMethod handlerMethod,
Exception ex,
Message<?> message) |
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 |
setArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers)
Configure the complete list of supported argument types effectively overriding
the ones configured by default.
|
void |
setCustomArgumentResolvers(List<HandlerMethodArgumentResolver> customArgumentResolvers)
Sets the list of custom
HandlerMethodArgumentResolver s that will be used
after resolvers for supported argument type. |
void |
setCustomReturnValueHandlers(List<HandlerMethodReturnValueHandler> customReturnValueHandlers)
Set the list of custom
HandlerMethodReturnValueHandler s that will be used
after return value handlers for known types. |
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 |
setReturnValueHandlers(List<HandlerMethodReturnValueHandler> returnValueHandlers)
Configure the complete list of supported return value types effectively overriding
the ones configured by default.
|
String |
toString() |
protected final Log logger
public void setDestinationPrefixes(Collection<String> prefixes)
By default, no prefixes are configured in which case all messages are eligible for handling.
public Collection<String> getDestinationPrefixes()
public void setCustomArgumentResolvers(List<HandlerMethodArgumentResolver> customArgumentResolvers)
HandlerMethodArgumentResolver
s that will be used
after resolvers for supported argument type.customArgumentResolvers
- the list of resolvers; never null
.public List<HandlerMethodArgumentResolver> getCustomArgumentResolvers()
public void setCustomReturnValueHandlers(List<HandlerMethodReturnValueHandler> customReturnValueHandlers)
HandlerMethodReturnValueHandler
s that will be used
after return value handlers for known types.customReturnValueHandlers
- the list of custom return value handlers, never null
.public List<HandlerMethodReturnValueHandler> getCustomReturnValueHandlers()
public void setArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers)
setCustomArgumentResolvers(java.util.List<org.springframework.messaging.handler.invocation.HandlerMethodArgumentResolver>)
.public List<HandlerMethodArgumentResolver> getArgumentResolvers()
public void setReturnValueHandlers(List<HandlerMethodReturnValueHandler> returnValueHandlers)
setCustomReturnValueHandlers(java.util.List<org.springframework.messaging.handler.invocation.HandlerMethodReturnValueHandler>)
.public List<HandlerMethodReturnValueHandler> getReturnValueHandlers()
public Map<T,HandlerMethod> getHandlerMethods()
public void setApplicationContext(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
public ApplicationContext getApplicationContext()
public void afterPropertiesSet()
InitializingBean
This method allows the bean instance to perform initialization only possible when all bean properties have been set and to throw an exception in the event of misconfiguration.
afterPropertiesSet
in interface InitializingBean
protected abstract List<? extends HandlerMethodArgumentResolver> initArgumentResolvers()
setArgumentResolvers(java.util.List<org.springframework.messaging.handler.invocation.HandlerMethodArgumentResolver>)
.
Subclasses should also take into account custom argument types configured via
setCustomArgumentResolvers(java.util.List<org.springframework.messaging.handler.invocation.HandlerMethodArgumentResolver>)
.
protected abstract List<? extends HandlerMethodReturnValueHandler> initReturnValueHandlers()
setReturnValueHandlers(java.util.List<org.springframework.messaging.handler.invocation.HandlerMethodReturnValueHandler>)
.
Subclasses should also take into account custom return value types configured
via setCustomReturnValueHandlers(java.util.List<org.springframework.messaging.handler.invocation.HandlerMethodReturnValueHandler>)
.
protected abstract boolean isHandler(Class<?> beanType)
protected final void detectHandlerMethods(Object handler)
handler
- the handler to check, either an instance of a Spring bean nameprotected abstract T getMappingForMethod(Method method, Class<?> handlerType)
method
- the method to provide a mapping forhandlerType
- the handler type, possibly a sub-type of the method's declaring classnull
if the method is not mappedprotected void registerHandlerMethod(Object handler, Method method, T mapping)
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 HandlerMethod createHandlerMethod(Object handler, Method method)
protected abstract Set<String> getDirectLookupDestinations(T mapping)
protected void registerExceptionHandlerAdvice(MessagingAdviceBean bean, AbstractExceptionHandlerMethodResolver resolver)
@MessageExceptionHandler
).public void handleMessage(Message<?> message) throws MessagingException
MessageHandler
handleMessage
in interface MessageHandler
message
- the message to be handledMessagingException
protected String getLookupDestination(String destination)
If there are no matching prefixes, return null
.
If there are no destination prefixes, return the destination as is.
protected void handleMessageInternal(Message<?> message, String lookupDestination)
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 handleMatch(T mapping, HandlerMethod handlerMethod, String lookupDestination, Message<?> message)
protected void processHandlerMethodException(HandlerMethod handlerMethod, Exception ex, Message<?> message)
protected InvocableHandlerMethod getExceptionHandlerMethod(HandlerMethod handlerMethod, Exception exception)
@MessageExceptionHandler
method for the given exception.
The default implementation searches methods in the class hierarchy of the
HandlerMethod first and if not found, it continues searching for additional
@MessageExceptionHandler
methods among the configured
MessagingAdviceBean, if any.handlerMethod
- the method where the exception was raisedexception
- the raised exceptionnull
protected abstract AbstractExceptionHandlerMethodResolver createExceptionHandlerMethodResolverFor(Class<?> beanType)