Top Description Fields Methods
org.springframework.web.context.request.async

public Interface CallableProcessingInterceptor

Known Direct Implementers
org.springframework.web.context.request.async.TimeoutCallableProcessingInterceptor
Imports
java.util.concurrent.Callable, org.springframework.core.task.AsyncTaskExecutor, org.springframework.lang.Nullable, org.springframework.web.context.request.NativeWebRequest

Intercepts concurrent request handling, where the concurrent result is obtained by executing a Callable on behalf of the application with an AsyncTaskExecutor.

A CallableProcessingInterceptor is invoked before and after the invocation of the Callable task in the asynchronous thread, as well as on timeout/error from a container thread, or after completing for any reason including a timeout or network error.

As a general rule exceptions raised by interceptor methods will cause async processing to resume by dispatching back to the container and using the Exception instance as the concurrent result. Such exceptions will then be processed through the HandlerExceptionResolver mechanism.

The handleTimeout method can select a value to be used to resume processing.

Authors
Rossen Stoyanchev, Rob Winch
Since
3.2

Field Summary

Modifier and TypeField and Description
public static final Object
RESPONSE_HANDLED

Constant indicating that the response has been handled by this interceptor without a result and that no further interceptors are to be invoked.

public static final Object
RESULT_NONE

Constant indicating that no result has been determined by this interceptor, giving subsequent interceptors a chance.

Method Summary

Modifier and TypeMethod and Description
public default <T> void
afterCompletion(NativeWebRequest
the current request
request
,
Callable<T>
the task for the current async request
task
)

Invoked from a container thread when async processing completes for any reason including timeout or network error.

public default <T> void
beforeConcurrentHandling(NativeWebRequest
the current request
request
,
Callable<T>
the task for the current async request
task
)

Invoked before the start of concurrent handling in the original thread in which the Callable is submitted for concurrent handling.

public default <T> Object

Returns:

a concurrent result value; if the value is anything other than RESULT_NONE or RESPONSE_HANDLED, concurrent processing is resumed and subsequent interceptors are not invoked
handleError
(NativeWebRequest
the current request
request
,
Callable<T>
the task for the current async request
task
,
Throwable
the error that occurred while request processing
t
)

Invoked from a container thread when an error occurred while processing the async request before the Callable task completes.

public default <T> Object

Returns:

a concurrent result value; if the value is anything other than RESULT_NONE or RESPONSE_HANDLED, concurrent processing is resumed and subsequent interceptors are not invoked
handleTimeout
(NativeWebRequest
the current request
request
,
Callable<T>
the task for the current async request
task
)

Invoked from a container thread when the async request times out before the Callable task completes.

public default <T> void
postProcess(NativeWebRequest
the current request
request
,
Callable<T>
the task for the current async request
task
,
Object
the result of concurrent processing, which could be a Throwable if the Callable raised an exception
concurrentResult
)

Invoked after the Callable has produced a result in the async thread in which the Callable is executed.

public default <T> void
preProcess(NativeWebRequest
the current request
request
,
Callable<T>
the task for the current async request
task
)

Invoked after the start of concurrent handling in the async thread in which the Callable is executed and before the actual invocation of the Callable.

Field Detail

RESPONSE_HANDLEDback to summary
public static final Object RESPONSE_HANDLED

Constant indicating that the response has been handled by this interceptor without a result and that no further interceptors are to be invoked.

See Also
handleTimeout, handleError
RESULT_NONEback to summary
public static final Object RESULT_NONE

Constant indicating that no result has been determined by this interceptor, giving subsequent interceptors a chance.

See Also
handleTimeout, handleError

Method Detail

afterCompletionback to summary
public default <T> void afterCompletion(NativeWebRequest request, Callable<T> task) throws Exception

Invoked from a container thread when async processing completes for any reason including timeout or network error.

The default implementation is empty.

Parameters
request:NativeWebRequest

the current request

task:Callable<T>

the task for the current async request

Exceptions
Exception:
in case of errors
beforeConcurrentHandlingback to summary
public default <T> void beforeConcurrentHandling(NativeWebRequest request, Callable<T> task) throws Exception

Invoked before the start of concurrent handling in the original thread in which the Callable is submitted for concurrent handling.

This is useful for capturing the state of the current thread just prior to invoking the Callable. Once the state is captured, it can then be transferred to the new Thread in preProcess(NativeWebRequest, Callable). Capturing the state of Spring Security's SecurityContextHolder and migrating it to the new Thread is a concrete example of where this is useful.

The default implementation is empty.

Parameters
request:NativeWebRequest

the current request

task:Callable<T>

the task for the current async request

Exceptions
Exception:
in case of errors
handleErrorback to summary
public default <T> Object handleError(NativeWebRequest request, Callable<T> task, Throwable t) throws Exception

Invoked from a container thread when an error occurred while processing the async request before the Callable task completes. Implementations may return a value, including an Exception, to use instead of the value the Callable did not return in time.

The default implementation always returns RESULT_NONE.

Parameters
request:NativeWebRequest

the current request

task:Callable<T>

the task for the current async request

t:Throwable

the error that occurred while request processing

Returns:Object

a concurrent result value; if the value is anything other than RESULT_NONE or RESPONSE_HANDLED, concurrent processing is resumed and subsequent interceptors are not invoked

Exceptions
Exception:
in case of errors
Since
5.0
handleTimeoutback to summary
public default <T> Object handleTimeout(NativeWebRequest request, Callable<T> task) throws Exception

Invoked from a container thread when the async request times out before the Callable task completes. Implementations may return a value, including an Exception, to use instead of the value the Callable did not return in time.

The default implementation always returns RESULT_NONE.

Parameters
request:NativeWebRequest

the current request

task:Callable<T>

the task for the current async request

Returns:Object

a concurrent result value; if the value is anything other than RESULT_NONE or RESPONSE_HANDLED, concurrent processing is resumed and subsequent interceptors are not invoked

Exceptions
Exception:
in case of errors
postProcessback to summary
public default <T> void postProcess(NativeWebRequest request, Callable<T> task, Object concurrentResult) throws Exception

Invoked after the Callable has produced a result in the async thread in which the Callable is executed. This method may be invoked later than afterTimeout or afterCompletion depending on when the Callable finishes processing.

The default implementation is empty.

Parameters
request:NativeWebRequest

the current request

task:Callable<T>

the task for the current async request

concurrentResult:Object

the result of concurrent processing, which could be a Throwable if the Callable raised an exception

Exceptions
Exception:
in case of errors
preProcessback to summary
public default <T> void preProcess(NativeWebRequest request, Callable<T> task) throws Exception

Invoked after the start of concurrent handling in the async thread in which the Callable is executed and before the actual invocation of the Callable.

The default implementation is empty.

Parameters
request:NativeWebRequest

the current request

task:Callable<T>

the task for the current async request

Exceptions
Exception:
in case of errors