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 SummaryModifier and TypeMethodDescriptionvoidClear concurrentResult and concurrentResultContext.Return the currentAsyncWebRequest.Get theCallableProcessingInterceptorregistered under the given key.Get the result from concurrent handling.Get the additional processing context saved at the start of concurrent handling.Get theDeferredResultProcessingInterceptorregistered under the given key.booleanReturn whether a result value exists as a result of concurrent handling.booleanReturn whether the selected handler for the current request chose to handle the request asynchronously.booleanReturntrueif thisWebAsyncManagerwas previously marked as wrapping a multipart async request,falseotherwise.voidregisterCallableInterceptor(Object key, CallableProcessingInterceptor interceptor) Register aCallableProcessingInterceptorunder the given key.voidregisterCallableInterceptors(CallableProcessingInterceptor... interceptors) Register aCallableProcessingInterceptorwithout a key.voidregisterDeferredResultInterceptor(Object key, DeferredResultProcessingInterceptor interceptor) Register aDeferredResultProcessingInterceptorunder the given key.voidregisterDeferredResultInterceptors(DeferredResultProcessingInterceptor... interceptors) Register one or moreDeferredResultProcessingInterceptorswithout a specified key.voidsetAsyncWebRequest(AsyncWebRequest asyncWebRequest) Configure theAsyncWebRequestto use.voidsetMultipartRequestParsed(boolean isMultipart) Mark theWebAsyncManageras wrapping a multipart async request.voidsetTaskExecutor(AsyncTaskExecutor taskExecutor) Configure an AsyncTaskExecutor for use with concurrent processing viastartCallableProcessing(Callable, Object...).voidstartCallableProcessing(Callable<?> callable, Object... processingContext) Start concurrent request processing and execute the given task with anAsyncTaskExecutor.voidstartCallableProcessing(WebAsyncTask<?> webAsyncTask, Object... processingContext) Use the givenWebAsyncTaskto configure the task executor as well as the timeout value of theAsyncWebRequestbefore delegating tostartCallableProcessing(Callable, Object...).voidstartDeferredResultProcessing(DeferredResult<?> deferredResult, Object... processingContext) Start concurrent request processing and initialize the givenDeferredResultwith aDeferredResult.DeferredResultHandlerthat saves the result and dispatches the request to resume processing of that result.
- 
Method Details- 
setAsyncWebRequestConfigure theAsyncWebRequestto use. This property may be set more than once during a single request to accurately reflect the current state of the request (for example, 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
 
- 
getAsyncWebRequestReturn the currentAsyncWebRequest.- Since:
- 5.3.33
 
- 
setTaskExecutorConfigure an AsyncTaskExecutor for use with concurrent processing viastartCallableProcessing(Callable, Object...).By default a SimpleAsyncTaskExecutorinstance is used.
- 
isConcurrentHandlingStartedpublic boolean isConcurrentHandlingStarted()Return 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.
- 
hasConcurrentResultpublic boolean hasConcurrentResult()Return whether a result value exists as a result of concurrent handling.
- 
getConcurrentResult
- 
getConcurrentResultContext
- 
getCallableInterceptorGet theCallableProcessingInterceptorregistered under the given key.- Parameters:
- key- the key
- Returns:
- the interceptor registered under that key, or nullif none
 
- 
getDeferredResultInterceptorGet theDeferredResultProcessingInterceptorregistered under the given key.- Parameters:
- key- the key
- Returns:
- the interceptor registered under that key, or nullif none
 
- 
registerCallableInterceptorRegister aCallableProcessingInterceptorunder the given key.- Parameters:
- key- the key
- interceptor- the interceptor to register
 
- 
registerCallableInterceptorsRegister aCallableProcessingInterceptorwithout a key. The key is derived from the class name and hash code.- Parameters:
- interceptors- one or more interceptors to register
 
- 
registerDeferredResultInterceptorpublic void registerDeferredResultInterceptor(Object key, DeferredResultProcessingInterceptor interceptor) Register aDeferredResultProcessingInterceptorunder the given key.- Parameters:
- key- the key
- interceptor- the interceptor to register
 
- 
registerDeferredResultInterceptorsRegister one or moreDeferredResultProcessingInterceptorswithout a specified key. The default key is derived from the interceptor class name and hash code.- Parameters:
- interceptors- one or more interceptors to register
 
- 
setMultipartRequestParsedpublic void setMultipartRequestParsed(boolean isMultipart) Mark theWebAsyncManageras wrapping a multipart async request.- Since:
- 6.1.12
 
- 
isMultipartRequestParsedpublic boolean isMultipartRequestParsed()Returntrueif thisWebAsyncManagerwas previously marked as wrapping a multipart async request,falseotherwise.- Since:
- 6.1.12
 
- 
clearConcurrentResultpublic void clearConcurrentResult()Clear concurrentResult and concurrentResultContext.
- 
startCallableProcessingpublic 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 asynchronously
- processingContext- additional context to save that can be accessed via- getConcurrentResultContext()
- Throws:
- Exception- if concurrent processing failed to start
- See Also:
 
- 
startCallableProcessingpublic void startCallableProcessing(WebAsyncTask<?> webAsyncTask, Object... processingContext) throws Exception Use the givenWebAsyncTaskto configure the task executor as well as the timeout value of theAsyncWebRequestbefore delegating tostartCallableProcessing(Callable, Object...).- Parameters:
- webAsyncTask- a WebAsyncTask containing the target- Callable
- processingContext- additional context to save that can be accessed via- getConcurrentResultContext()
- Throws:
- Exception- if concurrent processing failed to start
 
- 
startDeferredResultProcessingpublic void startDeferredResultProcessing(DeferredResult<?> deferredResult, Object... processingContext) throws Exception Start concurrent request processing and initialize the givenDeferredResultwith aDeferredResult.DeferredResultHandlerthat saves the result and dispatches the request to resume processing of that result. TheAsyncWebRequestis also updated with a completion handler that expires theDeferredResultand a timeout handler assuming theDeferredResulthas a default timeout result.- Parameters:
- deferredResult- the DeferredResult instance to initialize
- processingContext- additional context to save that can be accessed via- getConcurrentResultContext()
- Throws:
- Exception- if concurrent processing failed to start
- See Also:
 
 
-