org.springframework.aop.interceptor

Class AsyncExecutionAspectSupport

  • java.lang.Object
    • org.springframework.aop.interceptor.AsyncExecutionAspectSupport
  • All Implemented Interfaces:
    Aware, BeanFactoryAware
    Direct Known Subclasses:
    AsyncExecutionInterceptor


    public abstract class AsyncExecutionAspectSupport
    extends java.lang.Object
    implements BeanFactoryAware
    Base class for asynchronous method execution aspects, such as org.springframework.scheduling.annotation.AnnotationAsyncExecutionInterceptor or org.springframework.scheduling.aspectj.AnnotationAsyncExecutionAspect.

    Provides support for executor qualification on a method-by-method basis. AsyncExecutionAspectSupport objects must be constructed with a default Executor, but each individual method may further qualify a specific Executor bean to be used when executing it, e.g. through an annotation attribute.

    Since:
    3.1.2
    Author:
    Chris Beams, Juergen Hoeller, Stephane Nicoll
    • Method Summary

      All Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method and Description
      void configure(java.util.function.Supplier<java.util.concurrent.Executor> defaultExecutor, java.util.function.Supplier<AsyncUncaughtExceptionHandler> exceptionHandler)
      Configure this aspect with the given executor and exception handler suppliers, applying the corresponding default if a supplier is not resolvable.
      protected AsyncTaskExecutor determineAsyncExecutor(java.lang.reflect.Method method)
      Determine the specific executor to use when executing the given method.
      protected java.lang.Object doSubmit(java.util.concurrent.Callable<java.lang.Object> task, AsyncTaskExecutor executor, java.lang.Class<?> returnType)
      Delegate for actually executing the given task with the chosen executor.
      protected java.util.concurrent.Executor findQualifiedExecutor(BeanFactory beanFactory, java.lang.String qualifier)
      Retrieve a target executor for the given qualifier.
      protected java.util.concurrent.Executor getDefaultExecutor(BeanFactory beanFactory)
      Retrieve or build a default executor for this advice instance.
      protected abstract java.lang.String getExecutorQualifier(java.lang.reflect.Method method)
      Return the qualifier or bean name of the executor to be used when executing the given async method, typically specified in the form of an annotation attribute.
      protected void handleError(java.lang.Throwable ex, java.lang.reflect.Method method, java.lang.Object... params)
      Handles a fatal error thrown while asynchronously invoking the specified Method.
      void setBeanFactory(BeanFactory beanFactory)
      Set the BeanFactory to be used when looking up executors by qualifier or when relying on the default executor lookup algorithm.
      void setExceptionHandler(AsyncUncaughtExceptionHandler exceptionHandler)
      Supply the AsyncUncaughtExceptionHandler to use to handle exceptions thrown by invoking asynchronous methods with a void return type.
      void setExecutor(java.util.concurrent.Executor defaultExecutor)
      Supply the executor to be used when executing async methods.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • DEFAULT_TASK_EXECUTOR_BEAN_NAME

        public static final java.lang.String DEFAULT_TASK_EXECUTOR_BEAN_NAME
        The default name of the TaskExecutor bean to pick up: "taskExecutor".

        Note that the initial lookup happens by type; this is just the fallback in case of multiple executor beans found in the context.

        Since:
        4.2.6
        See Also:
        Constant Field Values
      • logger

        protected final Log logger
    • Constructor Detail

      • AsyncExecutionAspectSupport

        public AsyncExecutionAspectSupport(@Nullable
                                           java.util.concurrent.Executor defaultExecutor)
        Create a new instance with a default AsyncUncaughtExceptionHandler.
        Parameters:
        defaultExecutor - the Executor (typically a Spring AsyncTaskExecutor or ExecutorService) to delegate to, unless a more specific executor has been requested via a qualifier on the async method, in which case the executor will be looked up at invocation time against the enclosing bean factory
      • AsyncExecutionAspectSupport

        public AsyncExecutionAspectSupport(@Nullable
                                           java.util.concurrent.Executor defaultExecutor,
                                           AsyncUncaughtExceptionHandler exceptionHandler)
        Create a new AsyncExecutionAspectSupport with the given exception handler.
        Parameters:
        defaultExecutor - the Executor (typically a Spring AsyncTaskExecutor or ExecutorService) to delegate to, unless a more specific executor has been requested via a qualifier on the async method, in which case the executor will be looked up at invocation time against the enclosing bean factory
        exceptionHandler - the AsyncUncaughtExceptionHandler to use
    • Method Detail

      • configure

        public void configure(@Nullable
                              java.util.function.Supplier<java.util.concurrent.Executor> defaultExecutor,
                              @Nullable
                              java.util.function.Supplier<AsyncUncaughtExceptionHandler> exceptionHandler)
        Configure this aspect with the given executor and exception handler suppliers, applying the corresponding default if a supplier is not resolvable.
        Since:
        5.1
      • setExecutor

        public void setExecutor(java.util.concurrent.Executor defaultExecutor)
        Supply the executor to be used when executing async methods.
        Parameters:
        defaultExecutor - the Executor (typically a Spring AsyncTaskExecutor or ExecutorService) to delegate to, unless a more specific executor has been requested via a qualifier on the async method, in which case the executor will be looked up at invocation time against the enclosing bean factory
        See Also:
        getExecutorQualifier(Method), setBeanFactory(BeanFactory), getDefaultExecutor(BeanFactory)
      • determineAsyncExecutor

        @Nullable
        protected AsyncTaskExecutor determineAsyncExecutor(java.lang.reflect.Method method)
        Determine the specific executor to use when executing the given method. Should preferably return an AsyncListenableTaskExecutor implementation.
        Returns:
        the executor to use (or null, but just if no default executor is available)
      • getExecutorQualifier

        @Nullable
        protected abstract java.lang.String getExecutorQualifier(java.lang.reflect.Method method)
        Return the qualifier or bean name of the executor to be used when executing the given async method, typically specified in the form of an annotation attribute. Returning an empty string or null indicates that no specific executor has been specified and that the default executor should be used.
        Parameters:
        method - the method to inspect for executor qualifier metadata
        Returns:
        the qualifier if specified, otherwise empty String or null
        See Also:
        determineAsyncExecutor(Method), findQualifiedExecutor(BeanFactory, String)
      • findQualifiedExecutor

        @Nullable
        protected java.util.concurrent.Executor findQualifiedExecutor(@Nullable
                                                                                BeanFactory beanFactory,
                                                                                java.lang.String qualifier)
        Retrieve a target executor for the given qualifier.
        Parameters:
        qualifier - the qualifier to resolve
        Returns:
        the target executor, or null if none available
        Since:
        4.2.6
        See Also:
        getExecutorQualifier(Method)
      • getDefaultExecutor

        @Nullable
        protected java.util.concurrent.Executor getDefaultExecutor(@Nullable
                                                                             BeanFactory beanFactory)
        Retrieve or build a default executor for this advice instance. An executor returned from here will be cached for further use.

        The default implementation searches for a unique TaskExecutor bean in the context, or for an Executor bean named "taskExecutor" otherwise. If neither of the two is resolvable, this implementation will return null.

        Parameters:
        beanFactory - the BeanFactory to use for a default executor lookup
        Returns:
        the default executor, or null if none available
        Since:
        4.2.6
        See Also:
        findQualifiedExecutor(BeanFactory, String), DEFAULT_TASK_EXECUTOR_BEAN_NAME
      • doSubmit

        @Nullable
        protected java.lang.Object doSubmit(java.util.concurrent.Callable<java.lang.Object> task,
                                                      AsyncTaskExecutor executor,
                                                      java.lang.Class<?> returnType)
        Delegate for actually executing the given task with the chosen executor.
        Parameters:
        task - the task to execute
        executor - the chosen executor
        returnType - the declared return type (potentially a Future variant)
        Returns:
        the execution result (potentially a corresponding Future handle)
      • handleError

        protected void handleError(java.lang.Throwable ex,
                                   java.lang.reflect.Method method,
                                   java.lang.Object... params)
                            throws java.lang.Exception
        Handles a fatal error thrown while asynchronously invoking the specified Method.

        If the return type of the method is a Future object, the original exception can be propagated by just throwing it at the higher level. However, for all other cases, the exception will not be transmitted back to the client. In that later case, the current AsyncUncaughtExceptionHandler will be used to manage such exception.

        Parameters:
        ex - the exception to handle
        method - the method that was invoked
        params - the parameters used to invoke the method
        Throws:
        java.lang.Exception