类 WebAsyncManager

java.lang.Object
cn.taketoday.web.context.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 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().

从以下版本开始:
4.0
作者:
Rossen Stoyanchev, Juergen Hoeller, Harry Yang
另请参阅:
  • 字段详细资料

    • WEB_ASYNC_REQUEST_ATTRIBUTE

      public static final String WEB_ASYNC_REQUEST_ATTRIBUTE
      The name attribute of the RequestContext.
    • WEB_ASYNC_RESULT_ATTRIBUTE

      public static final String WEB_ASYNC_RESULT_ATTRIBUTE
      The name attribute containing the result.
    • RESULT_NONE

      private static final Object RESULT_NONE
    • DEFAULT_TASK_EXECUTOR

      private static final cn.taketoday.core.task.AsyncTaskExecutor DEFAULT_TASK_EXECUTOR
    • logger

      private static final cn.taketoday.logging.Logger logger
    • timeoutInterceptor

      private static final TimeoutAsyncProcessingInterceptor timeoutInterceptor
    • asyncRequest

      private AsyncWebRequest asyncRequest
    • taskExecutor

      private cn.taketoday.core.task.AsyncTaskExecutor taskExecutor
    • concurrentResult

      private volatile Object concurrentResult
    • concurrentResultContext

      private volatile Object[] concurrentResultContext
    • errorHandlingInProgress

      private volatile boolean errorHandlingInProgress
    • callableInterceptors

      private final Map<Object,CallableProcessingInterceptor> callableInterceptors
    • deferredResultInterceptors

      private final Map<Object,DeferredResultProcessingInterceptor> deferredResultInterceptors
    • requestContext

      private final RequestContext requestContext
  • 构造器详细资料

    • WebAsyncManager

      public WebAsyncManager(RequestContext requestContext)
  • 方法详细资料

    • setAsyncRequest

      public void setAsyncRequest(AsyncWebRequest asyncRequest)
      Configure the AsyncWebRequest 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. while RequestContext.isConcurrentHandlingStarted() is true.
      参数:
      asyncRequest - the web request to use
    • setTaskExecutor

      public void setTaskExecutor(cn.taketoday.core.task.AsyncTaskExecutor taskExecutor)
      Configure an AsyncTaskExecutor for use with concurrent processing via startCallableProcessing(Callable, Object...).

      By default a SimpleAsyncTaskExecutor instance is used.

    • 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 Exception or Throwable if concurrent handling raised one.
      另请参阅:
    • getConcurrentResultContext

      public Object[] getConcurrentResultContext()
      Provides access to additional processing context saved at the start of concurrent handling.
      另请参阅:
    • getCallableInterceptor

      @Nullable public CallableProcessingInterceptor getCallableInterceptor(Object key)
      Get the CallableProcessingInterceptor registered under the given key.
      参数:
      key - the key
      返回:
      the interceptor registered under that key, or null if none
    • getDeferredResultInterceptor

      @Nullable public DeferredResultProcessingInterceptor getDeferredResultInterceptor(Object key)
      Get the DeferredResultProcessingInterceptor registered under the given key.
      参数:
      key - the key
      返回:
      the interceptor registered under that key, or null if none
    • registerCallableInterceptor

      public void registerCallableInterceptor(Object key, CallableProcessingInterceptor interceptor)
      Register a CallableProcessingInterceptor under the given key.
      参数:
      key - the key
      interceptor - the interceptor to register
    • registerCallableInterceptors

      public void registerCallableInterceptors(CallableProcessingInterceptor... interceptors)
      Register a CallableProcessingInterceptor without 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 a DeferredResultProcessingInterceptor under the given key.
      参数:
      key - the key
      interceptor - the interceptor to register
    • registerDeferredResultInterceptors

      public void registerDeferredResultInterceptors(DeferredResultProcessingInterceptor... interceptors)
      Register one or more DeferredResultProcessingInterceptors 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()
    • startCallableProcessing

      public void startCallableProcessing(Callable<?> callable, Object... processingContext) throws Exception
      Start concurrent request processing and execute the given task with an AsyncTaskExecutor. 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 asynchronously
      processingContext - additional context to save that can be accessed via getConcurrentResultContext()
      抛出:
      Exception - if concurrent processing failed to start
      另请参阅:
    • startCallableProcessing

      public void startCallableProcessing(WebAsyncTask<?> webAsyncTask, Object... processingContext) throws Exception
      Use the given WebAsyncTask to configure the task executor as well as the timeout value of the AsyncWebRequest before delegating to startCallableProcessing(Callable, Object...).
      参数:
      webAsyncTask - a WebAsyncTask containing the target Callable
      processingContext - additional context to save that can be accessed via getConcurrentResultContext()
      抛出:
      Exception - if concurrent processing failed to start
    • formatRequestUri

      private String formatRequestUri()
    • setConcurrentResultAndDispatch

      private void setConcurrentResultAndDispatch(Object result)
    • startDeferredResultProcessing

      public void startDeferredResultProcessing(DeferredResult<?> deferredResult, Object... processingContext) throws Exception
      Start concurrent request processing and initialize the given DeferredResult with a DeferredResult.DeferredResultHandler that saves the result and dispatches the request to resume processing of that result. The AsyncWebRequest is also updated with a completion handler that expires the DeferredResult and a timeout handler assuming the DeferredResult has a default timeout result.
      参数:
      deferredResult - the DeferredResult instance to initialize
      processingContext - additional context to save that can be accessed via getConcurrentResultContext()
      抛出:
      Exception - if concurrent processing failed to start
      另请参阅:
    • startAsyncProcessing

      private void startAsyncProcessing(Object[] processingContext)
    • findHttpRequestHandler

      @Nullable public static Object findHttpRequestHandler(RequestContext request)