public class AsyncExecutionInterceptor extends AsyncExecutionAspectSupport implements MethodInterceptor, Ordered
MethodInterceptor
that processes method invocations
asynchronously, using a given AsyncTaskExecutor
.
Typically used with the 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 AsyncResult
or EJB 3.1's javax.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.
As of Spring 3.1.2 the AnnotationAsyncExecutionInterceptor
subclass is
preferred for use due to its support for executor qualification in conjunction with
Spring's @Async
annotation.
Async
,
AsyncAnnotationAdvisor
,
AnnotationAsyncExecutionInterceptor
HIGHEST_PRECEDENCE, LOWEST_PRECEDENCE
Constructor and Description |
---|
AsyncExecutionInterceptor(Executor defaultExecutor)
Create a new instance with a default
AsyncUncaughtExceptionHandler . |
AsyncExecutionInterceptor(Executor defaultExecutor,
AsyncUncaughtExceptionHandler exceptionHandler)
Create a new
AsyncExecutionInterceptor . |
Modifier and Type | Method and Description |
---|---|
protected String |
getExecutorQualifier(Method method)
This implementation is a no-op for compatibility in Spring 3.1.2.
|
int |
getOrder()
Return the order value of this object, with a
higher value meaning greater in terms of sorting.
|
protected void |
handleError(Throwable ex,
Method method,
Object... params)
Handles a fatal error thrown while asynchronously invoking the specified
Method . |
Object |
invoke(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.
|
void |
setExceptionHandler(AsyncUncaughtExceptionHandler exceptionHandler)
Supply the
AsyncUncaughtExceptionHandler to use to handle exceptions
thrown by invoking asynchronous methods with a void return type. |
determineAsyncExecutor, setBeanFactory, setExecutor
public AsyncExecutionInterceptor(Executor defaultExecutor, AsyncUncaughtExceptionHandler exceptionHandler)
AsyncExecutionInterceptor
.defaultExecutor
- the Executor
(typically a Spring AsyncTaskExecutor
or ExecutorService
) to delegate toexceptionHandler
- the AsyncUncaughtExceptionHandler
to usepublic AsyncExecutionInterceptor(Executor defaultExecutor)
AsyncUncaughtExceptionHandler
.public void setExceptionHandler(AsyncUncaughtExceptionHandler exceptionHandler)
AsyncUncaughtExceptionHandler
to use to handle exceptions
thrown by invoking asynchronous methods with a void
return type.public Object invoke(MethodInvocation invocation) throws Throwable
invoke
in interface MethodInterceptor
invocation
- the method to intercept and make asynchronousFuture
if the original method returns Future
; null
otherwise.Throwable
protected void handleError(Throwable ex, Method method, Object... params) throws Exception
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.
ex
- the exception to handlemethod
- the method that was invokedparams
- the parameters used to invoke the methodException
protected String getExecutorQualifier(Method method)
getExecutorQualifier
in class AsyncExecutionAspectSupport
method
- the method to inspect for executor qualifier metadatanull
AsyncExecutionAspectSupport.determineAsyncExecutor(Method)
public int getOrder()
Ordered
Normally starting with 0, with Integer.MAX_VALUE
indicating the greatest value. Same order values will result
in arbitrary positions for the affected objects.
Higher values can be interpreted as lower priority. As a consequence, the object with the lowest value has highest priority (somewhat analogous to Servlet "load-on-startup" values).