Class AsyncExecutionInterceptor
- java.lang.Object
- org.springframework.aop.interceptor.AsyncExecutionAspectSupport
- org.springframework.aop.interceptor.AsyncExecutionInterceptor
- All Implemented Interfaces:
Advice,Interceptor,MethodInterceptor,Aware,BeanFactoryAware,Ordered
- Direct Known Subclasses:
AnnotationAsyncExecutionInterceptor
public class AsyncExecutionInterceptor extends AsyncExecutionAspectSupport implements MethodInterceptor, Ordered
AOP AllianceMethodInterceptorthat processes method invocations asynchronously, using a givenAsyncTaskExecutor. Typically used with theAsyncannotation.In terms of target method signatures, any parameter types are supported. However, the return type is constrained to either
voidorjava.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'sAsyncResultor EJB 3.1'sjavax.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. Withvoidreturn type however, such exceptions cannot be transmitted back. In that case anAsyncUncaughtExceptionHandlercan be registered to process such exceptions.As of Spring 3.1.2 the
AnnotationAsyncExecutionInterceptorsubclass is preferred for use due to its support for executor qualification in conjunction with Spring's@Asyncannotation.- Since:
- 3.0
- Author:
- Juergen Hoeller, Chris Beams, Stephane Nicoll
- See Also:
Async,AsyncAnnotationAdvisor,AnnotationAsyncExecutionInterceptor
Field Summary
Fields inherited from class org.springframework.aop.interceptor.AsyncExecutionAspectSupport
DEFAULT_TASK_EXECUTOR_BEAN_NAME, logger
Fields inherited from interface org.springframework.core.Ordered
HIGHEST_PRECEDENCE, LOWEST_PRECEDENCE
Constructor Summary
Constructors Constructor Description AsyncExecutionInterceptor(Executor defaultExecutor)Create a new instance with a defaultAsyncUncaughtExceptionHandler.AsyncExecutionInterceptor(Executor defaultExecutor, AsyncUncaughtExceptionHandler exceptionHandler)Create a newAsyncExecutionInterceptor.
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected ExecutorgetDefaultExecutor(BeanFactory beanFactory)This implementation searches for a uniqueTaskExecutorbean in the context, or for anExecutorbean named "taskExecutor" otherwise.protected StringgetExecutorQualifier(Method method)This implementation is a no-op for compatibility in Spring 3.1.2.intgetOrder()Get the order value of this object.Objectinvoke(MethodInvocation invocation)Intercept the given method invocation, submit the actual calling of the method to the correct task executor and return immediately to the caller.Methods inherited from class org.springframework.aop.interceptor.AsyncExecutionAspectSupport
determineAsyncExecutor, doSubmit, findQualifiedExecutor, handleError, setBeanFactory, setExceptionHandler, setExecutor
Constructor Detail
AsyncExecutionInterceptor
public AsyncExecutionInterceptor(Executor defaultExecutor)
Create a new instance with a defaultAsyncUncaughtExceptionHandler.- Parameters:
defaultExecutor- theExecutor(typically a SpringAsyncTaskExecutororExecutorService) to delegate to; as of 4.2.6, a local executor for this interceptor will be built otherwise
AsyncExecutionInterceptor
public AsyncExecutionInterceptor(Executor defaultExecutor, AsyncUncaughtExceptionHandler exceptionHandler)
Create a newAsyncExecutionInterceptor.- Parameters:
defaultExecutor- theExecutor(typically a SpringAsyncTaskExecutororExecutorService) to delegate to; as of 4.2.6, a local executor for this interceptor will be built otherwiseexceptionHandler- theAsyncUncaughtExceptionHandlerto use
Method Detail
invoke
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:
invokein interfaceMethodInterceptor- Parameters:
invocation- the method to intercept and make asynchronous- Returns:
Futureif the original method returnsFuture;nullotherwise.- Throws:
Throwable- if the interceptors or the target object throws an exception
getExecutorQualifier
protected String getExecutorQualifier(Method method)
This implementation is a no-op for compatibility in Spring 3.1.2. Subclasses may override to provide support for extracting qualifier information, e.g. via an annotation on the given method.- Specified by:
getExecutorQualifierin classAsyncExecutionAspectSupport- Parameters:
method- the method to inspect for executor qualifier metadata- Returns:
- always
null - Since:
- 3.1.2
- See Also:
AsyncExecutionAspectSupport.determineAsyncExecutor(Method)
getDefaultExecutor
protected Executor getDefaultExecutor(BeanFactory beanFactory)
This implementation searches for a uniqueTaskExecutorbean in the context, or for anExecutorbean named "taskExecutor" otherwise. If neither of the two is resolvable (e.g. if noBeanFactorywas configured at all), this implementation falls back to a newly createdSimpleAsyncTaskExecutorinstance for local use if no default could be found.- Overrides:
getDefaultExecutorin classAsyncExecutionAspectSupport- Parameters:
beanFactory- the BeanFactory to use for a default executor lookup- Returns:
- the default executor, or
nullif none available - See Also:
AsyncExecutionAspectSupport.DEFAULT_TASK_EXECUTOR_BEAN_NAME
getOrder
public int getOrder()
Description copied from interface:OrderedGet 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-startupvalues).Same order values will result in arbitrary sort positions for the affected objects.
- Specified by:
getOrderin interfaceOrdered- Returns:
- the order value
- See Also:
Ordered.HIGHEST_PRECEDENCE,Ordered.LOWEST_PRECEDENCE