Top Description Fields Constructors Methods
org.springframework.aop.interceptor

public Class AsyncExecutionInterceptor

extends AsyncExecutionAspectSupport
implements MethodInterceptor, Ordered
Class Inheritance
All Implemented Interfaces
org.springframework.core.Ordered, org.aopalliance.intercept.MethodInterceptor, org.aopalliance.intercept.Interceptor, org.aopalliance.aop.Advice
Imports
java.lang.reflect.Method, java.util.concurrent.Callable, .ExecutionException, .Executor, .Future, org.aopalliance.intercept.MethodInterceptor, .MethodInvocation, org.springframework.aop.support.AopUtils, org.springframework.beans.factory.BeanFactory, org.springframework.core.BridgeMethodResolver, .Ordered, org.springframework.core.task.AsyncTaskExecutor, .SimpleAsyncTaskExecutor, org.springframework.lang.Nullable

AOP Alliance MethodInterceptor that processes method invocations asynchronously, using a given org.springframework.core.task.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.

Authors
Juergen Hoeller, Chris Beams, Stephane Nicoll
Since
3.0
See Also
org.springframework.scheduling.annotation.Async, org.springframework.scheduling.annotation.AsyncAnnotationAdvisor, org.springframework.scheduling.annotation.AnnotationAsyncExecutionInterceptor

Field Summary

Inherited from org.springframework.aop.interceptor.AsyncExecutionAspectSupport:
DEFAULT_TASK_EXECUTOR_BEAN_NAMElogger

Constructor Summary

AccessConstructor and Description
public
AsyncExecutionInterceptor(Executor
the Executor (typically a Spring AsyncTaskExecutor or java.util.concurrent.ExecutorService) to delegate to; a local executor for this interceptor will be built otherwise
defaultExecutor
)

Create a new instance with a default AsyncUncaughtExceptionHandler.

public
AsyncExecutionInterceptor(Executor
the Executor (typically a Spring AsyncTaskExecutor or java.util.concurrent.ExecutorService) to delegate to; a local executor for this interceptor will be built otherwise
defaultExecutor
,
AsyncUncaughtExceptionHandler exceptionHandler)

Create a new AsyncExecutionInterceptor.

Method Summary

Modifier and TypeMethod and Description
protected Executor
getDefaultExecutor(BeanFactory
the BeanFactory to use for a default executor lookup
beanFactory
)

Overrides org.springframework.aop.interceptor.AsyncExecutionAspectSupport.getDefaultExecutor.

This implementation searches for a unique org.springframework.core.task.TaskExecutor bean in the context, or for an Executor bean named "taskExecutor" otherwise.
protected String

Returns:

always null
getExecutorQualifier
(Method
the method to inspect for executor qualifier metadata
method
)

Implements abstract org.springframework.aop.interceptor.AsyncExecutionAspectSupport.getExecutorQualifier.

Get the qualifier for a specific executor to use when executing the given method.
public int
getOrder()

Implements org.springframework.core.Ordered.getOrder.

Get the order value of this object.
public Object

Returns:

Future if the original method returns Future; null otherwise.
invoke
(final MethodInvocation
the method to intercept and make asynchronous
invocation
)

Implements org.aopalliance.intercept.MethodInterceptor.invoke.

Intercept the given method invocation, submit the actual calling of the method to the correct task executor and return immediately to the caller.
Inherited from org.springframework.aop.interceptor.AsyncExecutionAspectSupport:
configuredetermineAsyncExecutordoSubmitfindQualifiedExecutorhandleErrorsetBeanFactorysetExceptionHandlersetExecutor

Constructor Detail

AsyncExecutionInterceptorback to summary
public AsyncExecutionInterceptor(Executor defaultExecutor)

Create a new instance with a default AsyncUncaughtExceptionHandler.

Parameters
defaultExecutor:Executor

the Executor (typically a Spring AsyncTaskExecutor or java.util.concurrent.ExecutorService) to delegate to; a local executor for this interceptor will be built otherwise

AsyncExecutionInterceptorback to summary
public AsyncExecutionInterceptor(Executor defaultExecutor, AsyncUncaughtExceptionHandler exceptionHandler)

Create a new AsyncExecutionInterceptor.

Parameters
defaultExecutor:Executor

the Executor (typically a Spring AsyncTaskExecutor or java.util.concurrent.ExecutorService) to delegate to; a local executor for this interceptor will be built otherwise

exceptionHandler:AsyncUncaughtExceptionHandler

the AsyncUncaughtExceptionHandler to use

Method Detail

getDefaultExecutorback to summary
protected Executor getDefaultExecutor(BeanFactory beanFactory)

Overrides org.springframework.aop.interceptor.AsyncExecutionAspectSupport.getDefaultExecutor.

This implementation searches for a unique org.springframework.core.task.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.

Parameters
beanFactory:BeanFactory

Doc from org.springframework.aop.interceptor.AsyncExecutionAspectSupport.getDefaultExecutor.

the BeanFactory to use for a default executor lookup

Returns:Executor

Doc from org.springframework.aop.interceptor.AsyncExecutionAspectSupport.getDefaultExecutor.

the default executor, or null if none available

Annotations
@Override
@Nullable
See Also
DEFAULT_TASK_EXECUTOR_BEAN_NAME
getExecutorQualifierback to summary
protected String getExecutorQualifier(Method method)

Implements abstract org.springframework.aop.interceptor.AsyncExecutionAspectSupport.getExecutorQualifier.

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.

Parameters
method:Method

Doc from org.springframework.aop.interceptor.AsyncExecutionAspectSupport.getExecutorQualifier.

the method to inspect for executor qualifier metadata

Returns:String

always null

Annotations
@Override
@Nullable
Since
3.1.2
See Also
determineAsyncExecutor(Method)
getOrderback to summary
public int getOrder()

Implements org.springframework.core.Ordered.getOrder.

Doc from org.springframework.core.Ordered.getOrder.

Get the order value of this object.

Higher values are interpreted as lower priority. As a consequence, the object with the lowest value has the highest priority (somewhat analogous to Servlet load-on-startup values).

Same order values will result in arbitrary sort positions for the affected objects.

Returns:int

the order value

Annotations
@Override
invokeback to summary
public Object invoke(final MethodInvocation invocation) throws Throwable

Implements org.aopalliance.intercept.MethodInterceptor.invoke.

Intercept the given method invocation, submit the actual calling of the method to the correct task executor and return immediately to the caller.

Parameters
invocation:MethodInvocation

the method to intercept and make asynchronous

Returns:Object

Future if the original method returns Future; null otherwise.

Annotations
@Override
@Nullable
Exceptions
Throwable:

Doc from org.aopalliance.intercept.MethodInterceptor.invoke.

if the interceptors or the target object throws an exception