类 WebAsyncManager
- java.lang.Object
- org.springframework.web.context.request.async.WebAsyncManager
public final class WebAsyncManager extends Object
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
startCallableProcessingorstartDeferredResultProcessing, 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 viagetConcurrentResult()or its presence detected viahasConcurrentResult().- 从以下版本开始:
- 3.2
- 作者:
- Rossen Stoyanchev, Juergen Hoeller
- 另请参阅:
AsyncWebRequestInterceptor,AsyncHandlerInterceptor,OncePerRequestFilter.shouldNotFilterAsyncDispatch(),OncePerRequestFilter.isAsyncDispatch(javax.servlet.http.HttpServletRequest)
方法概要
所有方法 实例方法 具体方法 修饰符和类型 方法 说明 voidclearConcurrentResult()Clear concurrentResult and concurrentResultContext.CallableProcessingInterceptorgetCallableInterceptor(Object key)Get theCallableProcessingInterceptorregistered under the given key.ObjectgetConcurrentResult()Provides access to the result from concurrent handling.Object[]getConcurrentResultContext()Provides access to additional processing context saved at the start of concurrent handling.DeferredResultProcessingInterceptorgetDeferredResultInterceptor(Object key)Get theDeferredResultProcessingInterceptorregistered under the given key.booleanhasConcurrentResult()Whether a result value exists as a result of concurrent handling.booleanisConcurrentHandlingStarted()Whether the selected handler for the current request chose to handle the request asynchronously.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 moreDeferredResultProcessingInterceptors without a specified key.voidsetAsyncWebRequest(AsyncWebRequest asyncWebRequest)Configure theAsyncWebRequestto use.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.
方法详细资料
setAsyncWebRequest
public void setAsyncWebRequest(AsyncWebRequest asyncWebRequest)
Configure theAsyncWebRequestto 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.- 参数:
asyncWebRequest- the web request to use
setTaskExecutor
public void setTaskExecutor(AsyncTaskExecutor taskExecutor)
Configure an AsyncTaskExecutor for use with concurrent processing viastartCallableProcessing(Callable, Object...).By default a
SimpleAsyncTaskExecutorinstance 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
public Object getConcurrentResult()
Provides access to the result from concurrent handling.- 返回:
- an Object, possibly an
ExceptionorThrowableif concurrent handling raised one. - 另请参阅:
clearConcurrentResult()
getConcurrentResultContext
public Object[] getConcurrentResultContext()
Provides access to additional processing context saved at the start of concurrent handling.
getCallableInterceptor
public CallableProcessingInterceptor getCallableInterceptor(Object key)
Get theCallableProcessingInterceptorregistered under the given key.- 参数:
key- the key- 返回:
- the interceptor registered under that key, or
nullif none
getDeferredResultInterceptor
public DeferredResultProcessingInterceptor getDeferredResultInterceptor(Object key)
Get theDeferredResultProcessingInterceptorregistered under the given key.- 参数:
key- the key- 返回:
- the interceptor registered under that key, or
nullif none
registerCallableInterceptor
public void registerCallableInterceptor(Object key, CallableProcessingInterceptor interceptor)
Register aCallableProcessingInterceptorunder the given key.- 参数:
key- the keyinterceptor- the interceptor to register
registerCallableInterceptors
public void registerCallableInterceptors(CallableProcessingInterceptor... interceptors)
Register aCallableProcessingInterceptorwithout a key. The key is derived from the class name and hashcode.- 参数:
interceptors- one or more interceptors to register
registerDeferredResultInterceptor
public void registerDeferredResultInterceptor(Object key, DeferredResultProcessingInterceptor interceptor)
Register aDeferredResultProcessingInterceptorunder the given key.- 参数:
key- the keyinterceptor- the interceptor to register
registerDeferredResultInterceptors
public void registerDeferredResultInterceptors(DeferredResultProcessingInterceptor... interceptors)
Register one or moreDeferredResultProcessingInterceptors without a specified key. The default key is derived from the interceptor class name and hash code.- 参数:
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.- 参数:
callable- a unit of work to be executed asynchronouslyprocessingContext- additional context to save that can be accessed viagetConcurrentResultContext()- 抛出:
Exception- if concurrent processing failed to start- 另请参阅:
getConcurrentResult(),getConcurrentResultContext()
startCallableProcessing
public 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...).- 参数:
webAsyncTask- a WebAsyncTask containing the targetCallableprocessingContext- additional context to save that can be accessed viagetConcurrentResultContext()- 抛出:
Exception- if concurrent processing failed to start
startDeferredResultProcessing
public 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.- 参数:
deferredResult- the DeferredResult instance to initializeprocessingContext- additional context to save that can be accessed viagetConcurrentResultContext()- 抛出:
Exception- if concurrent processing failed to start- 另请参阅:
getConcurrentResult(),getConcurrentResultContext()