Class AsyncExecutionInterceptor

java.lang.Object
org.springframework.aop.interceptor.AsyncExecutionAspectSupport
org.springframework.aop.interceptor.AsyncExecutionInterceptor
All Implemented Interfaces:
Advice, Interceptor, MethodInterceptor, org.springframework.beans.factory.Aware, org.springframework.beans.factory.BeanFactoryAware, org.springframework.core.Ordered

public class AsyncExecutionInterceptor extends AsyncExecutionAspectSupport implements MethodInterceptor, org.springframework.core.Ordered
AOP Alliance MethodInterceptor that processes method invocations asynchronously, using a given AsyncTaskExecutor. Typically used with the org.springframework.scheduling.annotation.Async annotation.

In terms of target method signatures, any parameter types are supported. However, the return type is constrained to either void or java.util.concurrent.Future. In the latter case, the Future handle returned from the proxy will be an actual asynchronous Future that can be used to track the result of the asynchronous method execution. However, since the target method needs to implement the same signature, it will have to return a temporary Future handle that just passes the return value through (like Spring's org.springframework.scheduling.annotation.AsyncResult or EJB's jakarta.ejb.AsyncResult).

When the return type is java.util.concurrent.Future, any exception thrown during the execution can be accessed and managed by the caller. With void return type however, such exceptions cannot be transmitted back. In that case an AsyncUncaughtExceptionHandler can be registered to process such exceptions.

Note: the AnnotationAsyncExecutionInterceptor subclass is preferred due to its support for executor qualification in conjunction with Spring's @Async annotation.

Since:
3.0
Author:
Juergen Hoeller, Chris Beams, Stephane Nicoll
See Also:
  • org.springframework.scheduling.annotation.Async
  • org.springframework.scheduling.annotation.AsyncAnnotationAdvisor
  • org.springframework.scheduling.annotation.AnnotationAsyncExecutionInterceptor
  • Constructor Details

    • AsyncExecutionInterceptor

      public AsyncExecutionInterceptor(@Nullable Executor defaultExecutor)
      Create a new instance with a default AsyncUncaughtExceptionHandler.
      Parameters:
      defaultExecutor - the Executor (typically a Spring AsyncTaskExecutor or ExecutorService) to delegate to; a local executor for this interceptor will be built otherwise
    • AsyncExecutionInterceptor

      public AsyncExecutionInterceptor(@Nullable Executor defaultExecutor, AsyncUncaughtExceptionHandler exceptionHandler)
      Create a new AsyncExecutionInterceptor.
      Parameters:
      defaultExecutor - the Executor (typically a Spring AsyncTaskExecutor or ExecutorService) to delegate to; a local executor for this interceptor will be built otherwise
      exceptionHandler - the AsyncUncaughtExceptionHandler to use
  • Method Details

    • invoke

      @Nullable public Object invoke(MethodInvocation invocation) throws Throwable
      Intercept the given method invocation, submit the actual calling of the method to the correct task executor and return immediately to the caller.
      Specified by:
      invoke in interface MethodInterceptor
      Parameters:
      invocation - the method to intercept and make asynchronous
      Returns:
      Future if the original method returns Future; null otherwise.
      Throws:
      Throwable - if the interceptors or the target object throws an exception
    • getExecutorQualifier

      @Nullable protected String getExecutorQualifier(Method method)
      Get the qualifier for a specific executor to use when executing the given method.

      The default implementation of this method is effectively a no-op.

      Subclasses may override this method to provide support for extracting qualifier information — for example, via an annotation on the given method.

      Specified by:
      getExecutorQualifier in class AsyncExecutionAspectSupport
      Parameters:
      method - the method to inspect for executor qualifier metadata
      Returns:
      always null
      Since:
      3.1.2
      See Also:
    • getDefaultExecutor

      @Nullable protected Executor getDefaultExecutor(@Nullable org.springframework.beans.factory.BeanFactory beanFactory)
      This 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 (e.g. if no BeanFactory was configured at all), this implementation falls back to a newly created SimpleAsyncTaskExecutor instance for local use if no default could be found.
      Overrides:
      getDefaultExecutor in class AsyncExecutionAspectSupport
      Parameters:
      beanFactory - the BeanFactory to use for a default executor lookup
      Returns:
      the default executor, or null if none available
      See Also:
    • getOrder

      public int getOrder()
      Specified by:
      getOrder in interface org.springframework.core.Ordered