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

public abstract Class AsyncExecutionAspectSupport

extends Object
implements BeanFactoryAware
Class Inheritance
All Implemented Interfaces
org.springframework.beans.factory.BeanFactoryAware, org.springframework.beans.factory.Aware
Known Direct Subclasses
org.springframework.aop.interceptor.AsyncExecutionInterceptor
Imports
java.lang.reflect.Method, java.util.Map, java.util.concurrent.Callable, .CompletableFuture, .ConcurrentHashMap, .Executor, .Future, java.util.function.Supplier, org.apache.commons.logging.Log, .LogFactory, org.springframework.beans.factory.BeanFactory, .BeanFactoryAware, .NoSuchBeanDefinitionException, .NoUniqueBeanDefinitionException, org.springframework.beans.factory.annotation.BeanFactoryAnnotationUtils, org.springframework.beans.factory.config.ConfigurableBeanFactory, .EmbeddedValueResolver, org.springframework.core.task.AsyncTaskExecutor, .TaskExecutor, org.springframework.core.task.support.TaskExecutorAdapter, org.springframework.lang.Nullable, org.springframework.util.ReflectionUtils, .StringUtils, .StringValueResolver, org.springframework.util.function.SingletonSupplier

Base class for asynchronous method execution aspects, such as 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.

Authors
Chris Beams, Juergen Hoeller, Stephane Nicoll, He Bo, Sebastien Deleuze
Since
3.1.2

Field Summary

Modifier and TypeField and Description
private BeanFactory
public static final String
DEFAULT_TASK_EXECUTOR_BEAN_NAME

The default name of the TaskExecutor bean to pick up: "taskExecutor".

private SingletonSupplier<Executor>
private StringValueResolver
private SingletonSupplier<AsyncUncaughtExceptionHandler>
private final Map<Method, AsyncTaskExecutor>
protected final Log

Constructor Summary

AccessConstructor and Description
public
AsyncExecutionAspectSupport(Executor
the Executor (typically a Spring AsyncTaskExecutor or java.util.concurrent.ExecutorService) 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
defaultExecutor
)

Create a new instance with a default AsyncUncaughtExceptionHandler.

public
AsyncExecutionAspectSupport(Executor
the Executor (typically a Spring AsyncTaskExecutor or java.util.concurrent.ExecutorService) 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
defaultExecutor
,
AsyncUncaughtExceptionHandler exceptionHandler)

Create a new AsyncExecutionAspectSupport with the given exception handler.

Method Summary

Modifier and TypeMethod and Description
public void
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

Returns:

the executor to use (or null, but just if no default executor is available)
determineAsyncExecutor
(Method method)

Determine the specific executor to use when executing the given method.

protected Object

Returns:

the execution result (potentially a corresponding Future handle)
doSubmit
(Callable<Object>
the task to execute
task
,
AsyncTaskExecutor
the chosen executor
executor
,
Class<?>
the declared return type (potentially a Future variant)
returnType
)

Delegate for actually executing the given task with the chosen executor.

protected Executor

Returns:

the target executor, or null if none available
findQualifiedExecutor
(BeanFactory beanFactory, String
the qualifier to resolve
qualifier
)

Retrieve a target executor for the given qualifier.

protected Executor

Returns:

the default executor, or null if none available
getDefaultExecutor
(BeanFactory
the BeanFactory to use for a default executor lookup
beanFactory
)

Retrieve or build a default executor for this advice instance.

protected abstract String

Returns:

the qualifier if specified, otherwise empty String or null
getExecutorQualifier
(Method
the method to inspect for executor qualifier metadata
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
the exception to handle
ex
,
Method
the method that was invoked
method
,
Object...
the parameters used to invoke the method
params
)

Handles a fatal error thrown while asynchronously invoking the specified Method.

public void
setBeanFactory(BeanFactory
owning BeanFactory (never null). The bean can immediately call methods on the factory.
beanFactory
)

Implements org.springframework.beans.factory.BeanFactoryAware.setBeanFactory.

Set the BeanFactory to be used when looking up executors by qualifier or when relying on the default executor lookup algorithm.
public void
setExceptionHandler(AsyncUncaughtExceptionHandler exceptionHandler)

Supply the AsyncUncaughtExceptionHandler to use to handle exceptions thrown by invoking asynchronous methods with a void return type.

public void
setExecutor(Executor
the Executor (typically a Spring AsyncTaskExecutor or java.util.concurrent.ExecutorService) 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
defaultExecutor
)

Supply the executor to be used when executing async methods.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

beanFactoryback to summary
private BeanFactory beanFactory
Annotations
@Nullable
DEFAULT_TASK_EXECUTOR_BEAN_NAMEback to summary
public static final String DEFAULT_TASK_EXECUTOR_BEAN_NAME

The default name of the TaskExecutor 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
defaultExecutorback to summary
private SingletonSupplier<Executor> defaultExecutor
embeddedValueResolverback to summary
private StringValueResolver embeddedValueResolver
Annotations
@Nullable
exceptionHandlerback to summary
private SingletonSupplier<AsyncUncaughtExceptionHandler> exceptionHandler
executorsback to summary
private final Map<Method, AsyncTaskExecutor> executors
loggerback to summary
protected final Log logger

Constructor Detail

AsyncExecutionAspectSupportback to summary
public AsyncExecutionAspectSupport(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, 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

AsyncExecutionAspectSupportback to summary
public AsyncExecutionAspectSupport(Executor defaultExecutor, AsyncUncaughtExceptionHandler exceptionHandler)

Create a new AsyncExecutionAspectSupport with the given exception handler.

Parameters
defaultExecutor:Executor

the Executor (typically a Spring AsyncTaskExecutor or java.util.concurrent.ExecutorService) 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

exceptionHandler:AsyncUncaughtExceptionHandler

the AsyncUncaughtExceptionHandler to use

Method Detail

configureback to summary
public void 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.

Since
5.1
determineAsyncExecutorback to summary
protected AsyncTaskExecutor determineAsyncExecutor(Method method)

Determine the specific executor to use when executing the given method.

Returns:AsyncTaskExecutor

the executor to use (or null, but just if no default executor is available)

Annotations
@Nullable
doSubmitback to summary
protected Object doSubmit(Callable<Object> task, AsyncTaskExecutor executor, Class<?> returnType)

Delegate for actually executing the given task with the chosen executor.

Parameters
task:Callable<Object>

the task to execute

executor:AsyncTaskExecutor

the chosen executor

returnType:Class<?>

the declared return type (potentially a Future variant)

Returns:Object

the execution result (potentially a corresponding Future handle)

Annotations
@Nullable
@SuppressWarnings:deprecation
findQualifiedExecutorback to summary
protected Executor findQualifiedExecutor(BeanFactory beanFactory, String qualifier)

Retrieve a target executor for the given qualifier.

Parameters
qualifier:String

the qualifier to resolve

Returns:Executor

the target executor, or null if none available

Annotations
@Nullable
Since
4.2.6
See Also
getExecutorQualifier(Method)
getDefaultExecutorback to summary
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 TaskExecutor bean in the context, or for an Executor bean named "taskExecutor" otherwise. If neither of the two is resolvable, this implementation will return null.

Parameters
beanFactory:BeanFactory

the BeanFactory to use for a default executor lookup

Returns:Executor

the default executor, or null if none available

Annotations
@Nullable
Since
4.2.6
See Also
findQualifiedExecutor(BeanFactory, String), DEFAULT_TASK_EXECUTOR_BEAN_NAME
getExecutorQualifierback to summary
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 or null indicates that no specific executor has been specified and that the default executor should be used.

Parameters
method:Method

the method to inspect for executor qualifier metadata

Returns:String

the qualifier if specified, otherwise empty String or null

Annotations
@Nullable
See Also
determineAsyncExecutor(Method), findQualifiedExecutor(BeanFactory, String)
handleErrorback to summary
protected void handleError(Throwable ex, Method method, Object... params) throws Exception

Handles a fatal error thrown while asynchronously invoking the specified Method.

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 current AsyncUncaughtExceptionHandler will be used to manage such exception.

Parameters
ex:Throwable

the exception to handle

method:Method

the method that was invoked

params:Object[]

the parameters used to invoke the method

setBeanFactoryback to summary
public void setBeanFactory(BeanFactory beanFactory)

Implements org.springframework.beans.factory.BeanFactoryAware.setBeanFactory.

Set the BeanFactory to be used when looking up executors by qualifier or when relying on the default executor lookup algorithm.

Parameters
beanFactory:BeanFactory

Doc from org.springframework.beans.factory.BeanFactoryAware.setBeanFactory.

owning BeanFactory (never null). The bean can immediately call methods on the factory.

Annotations
@Override
See Also
findQualifiedExecutor(BeanFactory, String), getDefaultExecutor(BeanFactory)
setExceptionHandlerback to summary
public void setExceptionHandler(AsyncUncaughtExceptionHandler exceptionHandler)

Supply the AsyncUncaughtExceptionHandler to use to handle exceptions thrown by invoking asynchronous methods with a void return type.

setExecutorback to summary
public void setExecutor(Executor defaultExecutor)

Supply the executor to be used when executing async methods.

Parameters
defaultExecutor:Executor

the Executor (typically a Spring AsyncTaskExecutor or java.util.concurrent.ExecutorService) 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)