Class AsyncExecutionAspectSupport
- java.lang.Object
- org.springframework.aop.interceptor.AsyncExecutionAspectSupport
- All Implemented Interfaces:
Aware,BeanFactoryAware
- Direct Known Subclasses:
AsyncExecutionInterceptor
public abstract class AsyncExecutionAspectSupport extends Object implements BeanFactoryAware
Base class for asynchronous method execution aspects, such asorg.springframework.scheduling.annotation.AnnotationAsyncExecutionInterceptorororg.springframework.scheduling.aspectj.AnnotationAsyncExecutionAspect.Provides support for executor qualification on a method-by-method basis.
AsyncExecutionAspectSupportobjects must be constructed with a defaultExecutor, but each individual method may further qualify a specificExecutorbean to be used when executing it, e.g. through an annotation attribute.- Since:
- 3.1.2
- Author:
- Chris Beams, Juergen Hoeller, Stephane Nicoll
Field Summary
Fields Modifier and Type Field Description static StringDEFAULT_TASK_EXECUTOR_BEAN_NAMEThe default name of theTaskExecutorbean to pick up: "taskExecutor".protected Loglogger
Constructor Summary
Constructors Constructor Description AsyncExecutionAspectSupport(Executor defaultExecutor)Create a new instance with a defaultAsyncUncaughtExceptionHandler.AsyncExecutionAspectSupport(Executor defaultExecutor, AsyncUncaughtExceptionHandler exceptionHandler)Create a newAsyncExecutionAspectSupportwith the given exception handler.
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected AsyncTaskExecutordetermineAsyncExecutor(Method method)Determine the specific executor to use when executing the given method.protected ObjectdoSubmit(Callable<Object> task, AsyncTaskExecutor executor, Class<?> returnType)Delegate for actually executing the given task with the chosen executor.protected ExecutorfindQualifiedExecutor(BeanFactory beanFactory, String qualifier)Retrieve a target executor for the given qualifier.protected ExecutorgetDefaultExecutor(BeanFactory beanFactory)Retrieve or build a default executor for this advice instance.protected abstract StringgetExecutorQualifier(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 voidhandleError(Throwable ex, Method method, Object... params)Handles a fatal error thrown while asynchronously invoking the specifiedMethod.voidsetBeanFactory(BeanFactory beanFactory)Set theBeanFactoryto be used when looking up executors by qualifier or when relying on the default executor lookup algorithm.voidsetExceptionHandler(AsyncUncaughtExceptionHandler exceptionHandler)Supply theAsyncUncaughtExceptionHandlerto use to handle exceptions thrown by invoking asynchronous methods with avoidreturn type.voidsetExecutor(Executor defaultExecutor)Supply the executor to be used when executing async methods.
Field Detail
DEFAULT_TASK_EXECUTOR_BEAN_NAME
public static final String DEFAULT_TASK_EXECUTOR_BEAN_NAME
The default name of theTaskExecutorbean 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
Constructor Detail
AsyncExecutionAspectSupport
public AsyncExecutionAspectSupport(Executor defaultExecutor)
Create a new instance with a defaultAsyncUncaughtExceptionHandler.- Parameters:
defaultExecutor- theExecutor(typically a SpringAsyncTaskExecutororExecutorService) 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(Executor defaultExecutor, AsyncUncaughtExceptionHandler exceptionHandler)
Create a newAsyncExecutionAspectSupportwith the given exception handler.- Parameters:
defaultExecutor- theExecutor(typically a SpringAsyncTaskExecutororExecutorService) 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 factoryexceptionHandler- theAsyncUncaughtExceptionHandlerto use
Method Detail
setExecutor
public void setExecutor(Executor defaultExecutor)
Supply the executor to be used when executing async methods.- Parameters:
defaultExecutor- theExecutor(typically a SpringAsyncTaskExecutororExecutorService) 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)
setExceptionHandler
public void setExceptionHandler(AsyncUncaughtExceptionHandler exceptionHandler)
Supply theAsyncUncaughtExceptionHandlerto use to handle exceptions thrown by invoking asynchronous methods with avoidreturn type.
setBeanFactory
public void setBeanFactory(BeanFactory beanFactory)
Set theBeanFactoryto be used when looking up executors by qualifier or when relying on the default executor lookup algorithm.- Specified by:
setBeanFactoryin interfaceBeanFactoryAware- Parameters:
beanFactory- owning BeanFactory (nevernull). The bean can immediately call methods on the factory.- See Also:
findQualifiedExecutor(BeanFactory, String),getDefaultExecutor(BeanFactory)
determineAsyncExecutor
protected AsyncTaskExecutor determineAsyncExecutor(Method method)
Determine the specific executor to use when executing the given method. Should preferably return anAsyncListenableTaskExecutorimplementation.- Returns:
- the executor to use (or
null, but just if no default executor is available)
getExecutorQualifier
protected abstract String getExecutorQualifier(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 ornullindicates 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
protected Executor findQualifiedExecutor(BeanFactory beanFactory, String qualifier)
Retrieve a target executor for the given qualifier.- Parameters:
qualifier- the qualifier to resolve- Returns:
- the target executor, or
nullif none available - Since:
- 4.2.6
- See Also:
getExecutorQualifier(Method)
getDefaultExecutor
protected Executor getDefaultExecutor(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
TaskExecutorbean in the context, or for anExecutorbean named "taskExecutor" otherwise. If neither of the two is resolvable, this implementation will returnnull.- Parameters:
beanFactory- the BeanFactory to use for a default executor lookup- Returns:
- the default executor, or
nullif none available - Since:
- 4.2.6
- See Also:
findQualifiedExecutor(BeanFactory, String),DEFAULT_TASK_EXECUTOR_BEAN_NAME
doSubmit
protected Object doSubmit(Callable<Object> task, AsyncTaskExecutor executor, Class<?> returnType)
Delegate for actually executing the given task with the chosen executor.
handleError
protected void handleError(Throwable ex, Method method, Object... params) throws Exception
Handles a fatal error thrown while asynchronously invoking the specifiedMethod.If the return type of the method is a
Futureobject, 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 currentAsyncUncaughtExceptionHandlerwill be used to manage such exception.- Parameters:
ex- the exception to handlemethod- the method that was invokedparams- the parameters used to invoke the method- Throws:
Exception