Class WebAsyncManager
java.lang.Object
org.springframework.web.context.request.async.WebAsyncManager
The central class for managing asynchronous request processing, mainly intended
as an SPI and not typically used directly by application classes.
An async scenario starts with request processing as usual in a thread (T1).
Concurrent request handling can be initiated by calling
startCallableProcessing
or
startDeferredResultProcessing
,
both of which produce a result in a separate thread (T2). The result is saved
and the request dispatched to the container, to resume processing with the saved
result in a third thread (T3). Within the dispatched thread (T3), the saved
result can be accessed via getConcurrentResult()
or its presence
detected via hasConcurrentResult()
.
- Since:
- 3.2
- Author:
- Rossen Stoyanchev, Juergen Hoeller, Sam Brannen
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionvoid
Clear concurrentResult and concurrentResultContext.Get theCallableProcessingInterceptor
registered under the given key.Provides access to the result from concurrent handling.Object[]
Provides access to additional processing context saved at the start of concurrent handling.Get theDeferredResultProcessingInterceptor
registered under the given key.boolean
Whether a result value exists as a result of concurrent handling.boolean
Whether the selected handler for the current request chose to handle the request asynchronously.void
registerCallableInterceptor
(Object key, CallableProcessingInterceptor interceptor) Register aCallableProcessingInterceptor
under the given key.void
registerCallableInterceptors
(CallableProcessingInterceptor... interceptors) Register aCallableProcessingInterceptor
without a key.void
registerDeferredResultInterceptor
(Object key, DeferredResultProcessingInterceptor interceptor) Register aDeferredResultProcessingInterceptor
under the given key.void
registerDeferredResultInterceptors
(DeferredResultProcessingInterceptor... interceptors) Register one or moreDeferredResultProcessingInterceptors
without a specified key.void
setAsyncWebRequest
(AsyncWebRequest asyncWebRequest) Configure theAsyncWebRequest
to use.void
setTaskExecutor
(AsyncTaskExecutor taskExecutor) Configure an AsyncTaskExecutor for use with concurrent processing viastartCallableProcessing(Callable, Object...)
.void
startCallableProcessing
(Callable<?> callable, Object... processingContext) Start concurrent request processing and execute the given task with anAsyncTaskExecutor
.void
startCallableProcessing
(WebAsyncTask<?> webAsyncTask, Object... processingContext) Use the givenWebAsyncTask
to configure the task executor as well as the timeout value of theAsyncWebRequest
before delegating tostartCallableProcessing(Callable, Object...)
.void
startDeferredResultProcessing
(DeferredResult<?> deferredResult, Object... processingContext) Start concurrent request processing and initialize the givenDeferredResult
with aDeferredResult.DeferredResultHandler
that saves the result and dispatches the request to resume processing of that result.
-
Method Details
-
setAsyncWebRequest
Configure theAsyncWebRequest
to use. This property may be set more than once during a single request to accurately reflect the current state of the request (e.g. following a forward, request/response wrapping, etc). However, it should not be set while concurrent handling is in progress, i.e. whileisConcurrentHandlingStarted()
istrue
.- Parameters:
asyncWebRequest
- the web request to use
-
setTaskExecutor
Configure an AsyncTaskExecutor for use with concurrent processing viastartCallableProcessing(Callable, Object...)
.By default a
SimpleAsyncTaskExecutor
instance is used. -
isConcurrentHandlingStarted
public boolean isConcurrentHandlingStarted()Whether the selected handler for the current request chose to handle the request asynchronously. A return value of "true" indicates concurrent handling is under way and the response will remain open. A return value of "false" means concurrent handling was either not started or possibly that it has completed and the request was dispatched for further processing of the concurrent result. -
hasConcurrentResult
public boolean hasConcurrentResult()Whether a result value exists as a result of concurrent handling. -
getConcurrentResult
Provides access to the result from concurrent handling.- Returns:
- an Object, possibly an
Exception
orThrowable
if concurrent handling raised one. - See Also:
-
getConcurrentResultContext
Provides access to additional processing context saved at the start of concurrent handling.- See Also:
-
getCallableInterceptor
Get theCallableProcessingInterceptor
registered under the given key.- Parameters:
key
- the key- Returns:
- the interceptor registered under that key, or
null
if none
-
getDeferredResultInterceptor
Get theDeferredResultProcessingInterceptor
registered under the given key.- Parameters:
key
- the key- Returns:
- the interceptor registered under that key, or
null
if none
-
registerCallableInterceptor
Register aCallableProcessingInterceptor
under the given key.- Parameters:
key
- the keyinterceptor
- the interceptor to register
-
registerCallableInterceptors
Register aCallableProcessingInterceptor
without a key. The key is derived from the class name and hashcode.- Parameters:
interceptors
- one or more interceptors to register
-
registerDeferredResultInterceptor
public void registerDeferredResultInterceptor(Object key, DeferredResultProcessingInterceptor interceptor) Register aDeferredResultProcessingInterceptor
under the given key.- Parameters:
key
- the keyinterceptor
- the interceptor to register
-
registerDeferredResultInterceptors
Register one or moreDeferredResultProcessingInterceptors
without a specified key. The default key is derived from the interceptor class name and hash code.- Parameters:
interceptors
- one or more interceptors to register
-
clearConcurrentResult
public void clearConcurrentResult()Clear concurrentResult and concurrentResultContext. -
startCallableProcessing
public void startCallableProcessing(Callable<?> callable, Object... processingContext) throws Exception Start concurrent request processing and execute the given task with anAsyncTaskExecutor
. The result from the task execution is saved and the request dispatched in order to resume processing of that result. If the task raises an Exception then the saved result will be the raised Exception.- Parameters:
callable
- a unit of work to be executed asynchronouslyprocessingContext
- additional context to save that can be accessed viagetConcurrentResultContext()
- Throws:
Exception
- if concurrent processing failed to start- See Also:
-
startCallableProcessing
public void startCallableProcessing(WebAsyncTask<?> webAsyncTask, Object... processingContext) throws Exception Use the givenWebAsyncTask
to configure the task executor as well as the timeout value of theAsyncWebRequest
before delegating tostartCallableProcessing(Callable, Object...)
.- Parameters:
webAsyncTask
- a WebAsyncTask containing the targetCallable
processingContext
- additional context to save that can be accessed viagetConcurrentResultContext()
- Throws:
Exception
- if concurrent processing failed to start
-
startDeferredResultProcessing
public void startDeferredResultProcessing(DeferredResult<?> deferredResult, Object... processingContext) throws Exception Start concurrent request processing and initialize the givenDeferredResult
with aDeferredResult.DeferredResultHandler
that saves the result and dispatches the request to resume processing of that result. TheAsyncWebRequest
is also updated with a completion handler that expires theDeferredResult
and a timeout handler assuming theDeferredResult
has a default timeout result.- Parameters:
deferredResult
- the DeferredResult instance to initializeprocessingContext
- additional context to save that can be accessed viagetConcurrentResultContext()
- Throws:
Exception
- if concurrent processing failed to start- See Also:
-