Class AsyncExecutionAspectSupport
- All Implemented Interfaces:
Aware
,BeanFactoryAware
- Direct Known Subclasses:
AsyncExecutionInterceptor
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
-
Field Summary
Modifier and TypeFieldDescriptionstatic final String
The default name of theTaskExecutor
bean to pick up: "taskExecutor".protected final Log
-
Constructor Summary
ConstructorDescriptionAsyncExecutionAspectSupport
(Executor defaultExecutor) Create a new instance with a defaultAsyncUncaughtExceptionHandler
.AsyncExecutionAspectSupport
(Executor defaultExecutor, AsyncUncaughtExceptionHandler exceptionHandler) Create a newAsyncExecutionAspectSupport
with the given exception handler. -
Method Summary
Modifier and TypeMethodDescriptionvoid
configure
(Supplier<Executor> defaultExecutor, 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
(Method method) Determine the specific executor to use when executing the given method.protected Object
doSubmit
(Callable<Object> task, AsyncTaskExecutor executor, Class<?> returnType) Delegate for actually executing the given task with the chosen executor.protected Executor
findQualifiedExecutor
(BeanFactory beanFactory, String qualifier) Retrieve a target executor for the given qualifier.protected Executor
getDefaultExecutor
(BeanFactory beanFactory) Retrieve or build a default executor for this advice instance.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.protected void
handleError
(Throwable ex, Method method, Object... params) Handles a fatal error thrown while asynchronously invoking the specifiedMethod
.void
setBeanFactory
(BeanFactory beanFactory) Set theBeanFactory
to be used when looking up executors by qualifier or when relying on the default executor lookup algorithm.void
setExceptionHandler
(AsyncUncaughtExceptionHandler exceptionHandler) Supply theAsyncUncaughtExceptionHandler
to use to handle exceptions thrown by invoking asynchronous methods with avoid
return type.void
setExecutor
(Executor defaultExecutor) Supply the executor to be used when executing async methods.
-
Field Details
-
DEFAULT_TASK_EXECUTOR_BEAN_NAME
The default name of theTaskExecutor
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:
-
logger
-
-
Constructor Details
-
AsyncExecutionAspectSupport
Create a new instance with a defaultAsyncUncaughtExceptionHandler
.- Parameters:
defaultExecutor
- theExecutor
(typically a SpringAsyncTaskExecutor
orExecutorService
) 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 Executor defaultExecutor, AsyncUncaughtExceptionHandler exceptionHandler) Create a newAsyncExecutionAspectSupport
with the given exception handler.- Parameters:
defaultExecutor
- theExecutor
(typically a SpringAsyncTaskExecutor
orExecutorService
) 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
- theAsyncUncaughtExceptionHandler
to use
-
-
Method Details
-
configure
public void configure(@Nullable Supplier<Executor> defaultExecutor, @Nullable 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
Supply the executor to be used when executing async methods.- Parameters:
defaultExecutor
- theExecutor
(typically a SpringAsyncTaskExecutor
orExecutorService
) 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:
-
setExceptionHandler
Supply theAsyncUncaughtExceptionHandler
to use to handle exceptions thrown by invoking asynchronous methods with avoid
return type. -
setBeanFactory
Set theBeanFactory
to be used when looking up executors by qualifier or when relying on the default executor lookup algorithm.- Specified by:
setBeanFactory
in interfaceBeanFactoryAware
- Parameters:
beanFactory
- owning BeanFactory (nevernull
). The bean can immediately call methods on the factory.- See Also:
-
determineAsyncExecutor
Determine the specific executor to use when executing the given method. Should preferably return anAsyncListenableTaskExecutor
implementation.- Returns:
- the executor to use (or
null
, but just if no default executor is available)
-
getExecutorQualifier
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 ornull
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:
-
findQualifiedExecutor
@Nullable protected Executor findQualifiedExecutor(@Nullable BeanFactory beanFactory, 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:
-
getDefaultExecutor
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 anExecutor
bean 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
null
if none available - Since:
- 4.2.6
- See Also:
-
doSubmit
@Nullable protected Object doSubmit(Callable<Object> task, AsyncTaskExecutor executor, Class<?> returnType) Delegate for actually executing the given task with the chosen executor. -
handleError
Handles a fatal error thrown while asynchronously invoking the specifiedMethod
.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 currentAsyncUncaughtExceptionHandler
will 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
-