T
- the type of the Object that contains information mapping a
HandlerMethod
to incoming messagespublic abstract class AbstractMethodMessageHandler<T> extends java.lang.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(java.lang.Class<?> beanType) |
protected HandlerMethod |
createHandlerMethod(java.lang.Object handler,
java.lang.reflect.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(java.lang.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() |
java.util.List<HandlerMethodArgumentResolver> |
getArgumentResolvers()
Return the complete list of argument resolvers.
|
java.util.List<HandlerMethodArgumentResolver> |
getCustomArgumentResolvers()
Return the configured custom argument resolvers, if any.
|
java.util.List<HandlerMethodReturnValueHandler> |
getCustomReturnValueHandlers()
Return the configured custom return value handlers, if any.
|
protected abstract java.lang.String |
getDestination(Message<?> message) |
java.util.Collection<java.lang.String> |
getDestinationPrefixes()
Return the configured destination prefixes, if any.
|
protected abstract java.util.Set<java.lang.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,
java.lang.Exception exception)
Find an
@MessageExceptionHandler method for the given exception. |
java.util.Map<T,HandlerMethod> |
getHandlerMethods()
Return a map with all handler methods and their mappings.
|
protected java.lang.String |
getLookupDestination(java.lang.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 java.util.Comparator<T> |
getMappingComparator(Message<?> message)
Return a comparator for sorting matching mappings.
|
protected abstract T |
getMappingForMethod(java.lang.reflect.Method method,
java.lang.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.
|
java.util.List<HandlerMethodReturnValueHandler> |
getReturnValueHandlers()
Return the complete list of return value handlers.
|
protected void |
handleMatch(T mapping,
HandlerMethod handlerMethod,
java.lang.String lookupDestination,
Message<?> message) |
void |
handleMessage(Message<?> message)
Handle the given message.
|
protected void |
handleMessageInternal(Message<?> message,
java.lang.String lookupDestination) |
protected void |
handleNoMatch(java.util.Set<T> ts,
java.lang.String lookupDestination,
Message<?> message) |
protected abstract java.util.List<? extends HandlerMethodArgumentResolver> |
initArgumentResolvers()
Return the list of argument resolvers to use.
|
protected abstract java.util.List<? extends HandlerMethodReturnValueHandler> |
initReturnValueHandlers()
Return the list of return value handlers to use.
|
protected abstract boolean |
isHandler(java.lang.Class<?> beanType)
Whether the given bean type should be introspected for messaging handling methods.
|
protected void |
processHandlerMethodException(HandlerMethod handlerMethod,
java.lang.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(java.lang.Object handler,
java.lang.reflect.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(java.util.List<HandlerMethodArgumentResolver> argumentResolvers)
Configure the complete list of supported argument types, effectively overriding
the ones configured by default.
|
void |
setCustomArgumentResolvers(java.util.List<HandlerMethodArgumentResolver> customArgumentResolvers)
Sets the list of custom
HandlerMethodArgumentResolver s that will be used
after resolvers for supported argument type. |
void |
setCustomReturnValueHandlers(java.util.List<HandlerMethodReturnValueHandler> customReturnValueHandlers)
Set the list of custom
HandlerMethodReturnValueHandler s that will be used
after return value handlers for known types. |
void |
setDestinationPrefixes(java.util.Collection<java.lang.String> prefixes)
When this property is configured only messages to destinations matching
one of the configured prefixes are eligible for handling.
|
void |
setReturnValueHandlers(java.util.List<HandlerMethodReturnValueHandler> returnValueHandlers)
Configure the complete list of supported return value types, effectively overriding
the ones configured by default.
|
java.lang.String |
toString() |
protected final Log logger
public void setDestinationPrefixes(java.util.Collection<java.lang.String> prefixes)
By default, no prefixes are configured in which case all messages are eligible for handling.
public java.util.Collection<java.lang.String> getDestinationPrefixes()
public void setCustomArgumentResolvers(java.util.List<HandlerMethodArgumentResolver> customArgumentResolvers)
HandlerMethodArgumentResolver
s that will be used
after resolvers for supported argument type.public java.util.List<HandlerMethodArgumentResolver> getCustomArgumentResolvers()
public void setCustomReturnValueHandlers(java.util.List<HandlerMethodReturnValueHandler> customReturnValueHandlers)
HandlerMethodReturnValueHandler
s that will be used
after return value handlers for known types.public java.util.List<HandlerMethodReturnValueHandler> getCustomReturnValueHandlers()
public void setArgumentResolvers(java.util.List<HandlerMethodArgumentResolver> argumentResolvers)
setCustomArgumentResolvers(java.util.List<org.springframework.messaging.handler.invocation.HandlerMethodArgumentResolver>)
.public java.util.List<HandlerMethodArgumentResolver> getArgumentResolvers()
public void setReturnValueHandlers(java.util.List<HandlerMethodReturnValueHandler> returnValueHandlers)
setCustomReturnValueHandlers(java.util.List<org.springframework.messaging.handler.invocation.HandlerMethodReturnValueHandler>)
.public java.util.List<HandlerMethodReturnValueHandler> getReturnValueHandlers()
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 java.util.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 java.util.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(java.lang.Class<?> beanType)
protected final void detectHandlerMethods(java.lang.Object handler)
handler
- the handler to check, either an instance of a Spring bean nameprotected abstract T getMappingForMethod(java.lang.reflect.Method method, java.lang.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(java.lang.Object handler, java.lang.reflect.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 methodjava.lang.IllegalStateException
- if another method was already registered
under the same mappingprotected HandlerMethod createHandlerMethod(java.lang.Object handler, java.lang.reflect.Method method)
protected abstract java.util.Set<java.lang.String> getDirectLookupDestinations(T mapping)
protected void registerExceptionHandlerAdvice(MessagingAdviceBean bean, AbstractExceptionHandlerMethodResolver resolver)
@MessageExceptionHandler
).public java.util.Map<T,HandlerMethod> getHandlerMethods()
public void handleMessage(Message<?> message) throws MessagingException
MessageHandler
handleMessage
in interface MessageHandler
message
- the message to be handledMessagingException
protected abstract java.lang.String getDestination(Message<?> message)
protected java.lang.String getLookupDestination(java.lang.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, java.lang.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 void handleNoMatch(java.util.Set<T> ts, java.lang.String lookupDestination, Message<?> message)
protected abstract java.util.Comparator<T> getMappingComparator(Message<?> message)
message
- the current Messagenull
protected void handleMatch(T mapping, HandlerMethod handlerMethod, java.lang.String lookupDestination, Message<?> message)
protected void processHandlerMethodException(HandlerMethod handlerMethod, java.lang.Exception ex, Message<?> message)
protected InvocableHandlerMethod getExceptionHandlerMethod(HandlerMethod handlerMethod, java.lang.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(java.lang.Class<?> beanType)
public java.lang.String toString()
toString
in class java.lang.Object