org.springframework.messaging.handler.invocation

Class AbstractMethodMessageHandler<T>

  • java.lang.Object
    • org.springframework.messaging.handler.invocation.AbstractMethodMessageHandler<T>
  • Type Parameters:
    T - the type of the Object that contains information mapping a HandlerMethod to incoming messages
    All Implemented Interfaces:
    Aware, InitializingBean, ApplicationContextAware, MessageHandler
    Direct Known Subclasses:
    SimpAnnotationMethodMessageHandler


    public abstract class AbstractMethodMessageHandler<T>
    extends java.lang.Object
    implements MessageHandler, ApplicationContextAware, InitializingBean
    Abstract base class for 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.

    Since:
    4.0
    Author:
    Rossen Stoyanchev, Juergen Hoeller
    • Field Detail

      • logger

        protected final Log logger
    • Constructor Detail

      • AbstractMethodMessageHandler

        public AbstractMethodMessageHandler()
    • Method Detail

      • setDestinationPrefixes

        public void setDestinationPrefixes(@Nullable
                                           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. When there is a match the prefix is removed and only the remaining part of the destination is used for method-mapping purposes.

        By default, no prefixes are configured in which case all messages are eligible for handling.

      • getDestinationPrefixes

        public java.util.Collection<java.lang.String> getDestinationPrefixes()
        Return the configured destination prefixes, if any.
      • setCustomArgumentResolvers

        public void setCustomArgumentResolvers(@Nullable
                                               java.util.List<HandlerMethodArgumentResolver> customArgumentResolvers)
        Sets the list of custom HandlerMethodArgumentResolvers that will be used after resolvers for supported argument type.
      • getCustomArgumentResolvers

        public java.util.List<HandlerMethodArgumentResolver> getCustomArgumentResolvers()
        Return the configured custom argument resolvers, if any.
      • setCustomReturnValueHandlers

        public void setCustomReturnValueHandlers(@Nullable
                                                 java.util.List<HandlerMethodReturnValueHandler> customReturnValueHandlers)
        Set the list of custom HandlerMethodReturnValueHandlers that will be used after return value handlers for known types.
      • getCustomReturnValueHandlers

        public java.util.List<HandlerMethodReturnValueHandler> getCustomReturnValueHandlers()
        Return the configured custom return value handlers, if any.
      • getArgumentResolvers

        public java.util.List<HandlerMethodArgumentResolver> getArgumentResolvers()
        Return the complete list of argument resolvers.
      • getReturnValueHandlers

        public java.util.List<HandlerMethodReturnValueHandler> getReturnValueHandlers()
        Return the complete list of return value handlers.
      • afterPropertiesSet

        public void afterPropertiesSet()
        Description copied from interface: InitializingBean
        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.

        Specified by:
        afterPropertiesSet in interface InitializingBean
      • isHandler

        protected abstract boolean isHandler(java.lang.Class<?> beanType)
        Whether the given bean type should be introspected for messaging handling methods.
      • detectHandlerMethods

        protected final 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.
        Parameters:
        handler - the handler to check, either an instance of a Spring bean name
      • getMappingForMethod

        @Nullable
        protected abstract T getMappingForMethod(java.lang.reflect.Method method,
                                                           java.lang.Class<?> handlerType)
        Provide the mapping for a handler method.
        Parameters:
        method - the method to provide a mapping for
        handlerType - the handler type, possibly a sub-type of the method's declaring class
        Returns:
        the mapping, or null if the method is not mapped
      • registerHandlerMethod

        protected void registerHandlerMethod(java.lang.Object handler,
                                             java.lang.reflect.Method method,
                                             T mapping)
        Register a handler method and its unique mapping.
        Parameters:
        handler - the bean name of the handler or the handler instance
        method - the method to register
        mapping - the mapping conditions associated with the handler method
        Throws:
        java.lang.IllegalStateException - if another method was already registered under the same mapping
      • createHandlerMethod

        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.
      • getDirectLookupDestinations

        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.
      • registerExceptionHandlerAdvice

        protected void registerExceptionHandlerAdvice(MessagingAdviceBean bean,
                                                      AbstractExceptionHandlerMethodResolver resolver)
        Subclasses can invoke this method to populate the MessagingAdviceBean cache (e.g. to support "global" @MessageExceptionHandler).
        Since:
        4.2
      • getHandlerMethods

        public java.util.Map<T,HandlerMethod> getHandlerMethods()
        Return a map with all handler methods and their mappings.
      • getDestination

        @Nullable
        protected abstract java.lang.String getDestination(Message<?> message)
      • getLookupDestination

        @Nullable
        protected java.lang.String getLookupDestination(@Nullable
                                                                  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.

        If there are no matching prefixes, return null.

        If there are no destination prefixes, return the destination as is.

      • handleMessageInternal

        protected void handleMessageInternal(Message<?> message,
                                             java.lang.String lookupDestination)
      • getMatchingMapping

        @Nullable
        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 - the mapping to get a match for
        message - the message being handled
        Returns:
        the match or null if there is no match
      • handleNoMatch

        protected void handleNoMatch(java.util.Set<T> ts,
                                     java.lang.String lookupDestination,
                                     Message<?> message)
      • getMappingComparator

        protected abstract java.util.Comparator<T> getMappingComparator(Message<?> message)
        Return a comparator for sorting matching mappings. The returned comparator should sort 'better' matches higher.
        Parameters:
        message - the current Message
        Returns:
        the comparator, never null
      • handleMatch

        protected void handleMatch(T mapping,
                                   HandlerMethod handlerMethod,
                                   java.lang.String lookupDestination,
                                   Message<?> message)
      • processHandlerMethodException

        protected void processHandlerMethodException(HandlerMethod handlerMethod,
                                                     java.lang.Exception exception,
                                                     Message<?> message)
      • getExceptionHandlerMethod

        @Nullable
        protected InvocableHandlerMethod getExceptionHandlerMethod(HandlerMethod handlerMethod,
                                                                             java.lang.Exception exception)
        Find an @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.
        Parameters:
        handlerMethod - the method where the exception was raised
        exception - the raised exception
        Returns:
        a method to handle the exception, or null
        Since:
        4.2
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object