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

public final Class WebAsyncManager

extends Object
Class Inheritance
Imports
java.util.ArrayList, .LinkedHashMap, .List, .Map, java.util.concurrent.Callable, .Future, jakarta.servlet.http.HttpServletRequest, org.apache.commons.logging.Log, .LogFactory, org.springframework.core.task.AsyncTaskExecutor, .SimpleAsyncTaskExecutor, org.springframework.lang.Nullable, org.springframework.util.Assert, org.springframework.web.context.request.RequestAttributes, org.springframework.web.context.request.async.DeferredResult.DeferredResultHandler, org.springframework.web.util.DisconnectedClientHelper

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().

Authors
Rossen Stoyanchev, Juergen Hoeller, Sam Brannen
Since
3.2
See Also
org.springframework.web.context.request.AsyncWebRequestInterceptor, org.springframework.web.servlet.AsyncHandlerInterceptor, org.springframework.web.filter.OncePerRequestFilter#shouldNotFilterAsyncDispatch, org.springframework.web.filter.OncePerRequestFilter#isAsyncDispatch

Field Summary

Modifier and TypeField and Description
private AsyncWebRequest
private final Map<Object, CallableProcessingInterceptor>
private volatile Object
private volatile Object[]
private static final AsyncTaskExecutor
private final Map<Object, DeferredResultProcessingInterceptor>
private static final String
DISCONNECTED_CLIENT_LOG_CATEGORY

Log category to use for network failure after a client has gone away.

private static final DisconnectedClientHelper
private volatile boolean
private static final Log
private static final Object
private AsyncTaskExecutor
private static final CallableProcessingInterceptor
private static final DeferredResultProcessingInterceptor

Constructor Summary

AccessConstructor and Description
pack-priv
WebAsyncManager()

Package-private constructor.

Method Summary

Modifier and TypeMethod and Description
public void
private String
public CallableProcessingInterceptor

Returns:

the interceptor registered under that key, or null if none
getCallableInterceptor
(Object
the key
key
)

Get the CallableProcessingInterceptor registered under the given key.

public Object

Returns:

an Object, possibly an Exception or Throwable if concurrent handling raised one.
getConcurrentResult
()

Provides access to the result from concurrent handling.

public Object[]
getConcurrentResultContext()

Provides access to additional processing context saved at the start of concurrent handling.

public DeferredResultProcessingInterceptor

Returns:

the interceptor registered under that key, or null if none
getDeferredResultInterceptor
(Object
the key
key
)

Get the DeferredResultProcessingInterceptor registered under the given key.

public boolean
hasConcurrentResult()

Whether a result value exists as a result of concurrent handling.

public boolean
isConcurrentHandlingStarted()

Whether the selected handler for the current request chose to handle the request asynchronously.

public void
registerCallableInterceptor(Object
the key
key
,
CallableProcessingInterceptor
the interceptor to register
interceptor
)

Register a CallableProcessingInterceptor under the given key.

public void
registerCallableInterceptors(CallableProcessingInterceptor...
one or more interceptors to register
interceptors
)

Register a CallableProcessingInterceptor without a key.

public void
registerDeferredResultInterceptor(Object
the key
key
,
DeferredResultProcessingInterceptor
the interceptor to register
interceptor
)

Register a DeferredResultProcessingInterceptor under the given key.

public void
registerDeferredResultInterceptors(DeferredResultProcessingInterceptor...
one or more interceptors to register
interceptors
)

Register one or more DeferredResultProcessingInterceptors without a specified key.

public void
setAsyncWebRequest(AsyncWebRequest
the web request to use
asyncWebRequest
)

Configure the AsyncWebRequest to use.

private void
public void
setTaskExecutor(AsyncTaskExecutor taskExecutor)

Configure an AsyncTaskExecutor for use with concurrent processing via startCallableProcessing(Callable, Object...).

private void
startAsyncProcessing(Object[] processingContext)

public void
startCallableProcessing(Callable<?>
a unit of work to be executed asynchronously
callable
,
Object...
additional context to save that can be accessed via getConcurrentResultContext()
processingContext
)

Start concurrent request processing and execute the given task with an AsyncTaskExecutor.

public void
startCallableProcessing(final WebAsyncTask<?>
a WebAsyncTask containing the target Callable
webAsyncTask
,
Object...
additional context to save that can be accessed via getConcurrentResultContext()
processingContext
)

Use the given WebAsyncTask to configure the task executor as well as the timeout value of the AsyncWebRequest before delegating to startCallableProcessing(Callable, Object...).

public void
startDeferredResultProcessing(final DeferredResult<?>
the DeferredResult instance to initialize
deferredResult
,
Object...
additional context to save that can be accessed via getConcurrentResultContext()
processingContext
)

Start concurrent request processing and initialize the given DeferredResult with a DeferredResultHandler that saves the result and dispatches the request to resume processing of that result.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

asyncWebRequestback to summary
private AsyncWebRequest asyncWebRequest
Annotations
@Nullable
callableInterceptorsback to summary
private final Map<Object, CallableProcessingInterceptor> callableInterceptors
concurrentResultback to summary
private volatile Object concurrentResult
Annotations
@Nullable
concurrentResultContextback to summary
private volatile Object[] concurrentResultContext
Annotations
@Nullable
DEFAULT_TASK_EXECUTORback to summary
private static final AsyncTaskExecutor DEFAULT_TASK_EXECUTOR
deferredResultInterceptorsback to summary
private final Map<Object, DeferredResultProcessingInterceptor> deferredResultInterceptors
DISCONNECTED_CLIENT_LOG_CATEGORYback to summary
private static final String DISCONNECTED_CLIENT_LOG_CATEGORY

Log category to use for network failure after a client has gone away.

See Also
DisconnectedClientHelper
disconnectedClientHelperback to summary
private static final DisconnectedClientHelper disconnectedClientHelper
errorHandlingInProgressback to summary
private volatile boolean errorHandlingInProgress
loggerback to summary
private static final Log logger
RESULT_NONEback to summary
private static final Object RESULT_NONE
taskExecutorback to summary
private AsyncTaskExecutor taskExecutor
timeoutCallableInterceptorback to summary
private static final CallableProcessingInterceptor timeoutCallableInterceptor
timeoutDeferredResultInterceptorback to summary
private static final DeferredResultProcessingInterceptor timeoutDeferredResultInterceptor

Constructor Detail

WebAsyncManagerback to summary
pack-priv WebAsyncManager()

Package-private constructor.

See Also
WebAsyncUtils#getAsyncManager(jakarta.servlet.ServletRequest), WebAsyncUtils#getAsyncManager(org.springframework.web.context.request.WebRequest)

Method Detail

clearConcurrentResultback to summary
public void clearConcurrentResult()

Clear concurrentResult and concurrentResultContext.

formatRequestUriback to summary
private String formatRequestUri()
getCallableInterceptorback to summary
public CallableProcessingInterceptor getCallableInterceptor(Object key)

Get the CallableProcessingInterceptor registered under the given key.

Parameters
key:Object

the key

Returns:CallableProcessingInterceptor

the interceptor registered under that key, or null if none

Annotations
@Nullable
getConcurrentResultback to summary
public Object getConcurrentResult()

Provides access to the result from concurrent handling.

Returns:Object

an Object, possibly an Exception or Throwable if concurrent handling raised one.

Annotations
@Nullable
See Also
clearConcurrentResult()
getConcurrentResultContextback to summary
public Object[] getConcurrentResultContext()

Provides access to additional processing context saved at the start of concurrent handling.

Annotations
@Nullable
See Also
clearConcurrentResult()
getDeferredResultInterceptorback to summary
public DeferredResultProcessingInterceptor getDeferredResultInterceptor(Object key)

Get the DeferredResultProcessingInterceptor registered under the given key.

Parameters
key:Object

the key

Returns:DeferredResultProcessingInterceptor

the interceptor registered under that key, or null if none

Annotations
@Nullable
hasConcurrentResultback to summary
public boolean hasConcurrentResult()

Whether a result value exists as a result of concurrent handling.

isConcurrentHandlingStartedback to summary
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.

registerCallableInterceptorback to summary
public void registerCallableInterceptor(Object key, CallableProcessingInterceptor interceptor)

Register a CallableProcessingInterceptor under the given key.

Parameters
key:Object

the key

interceptor:CallableProcessingInterceptor

the interceptor to register

registerCallableInterceptorsback to summary
public void registerCallableInterceptors(CallableProcessingInterceptor... interceptors)

Register a CallableProcessingInterceptor without a key. The key is derived from the class name and hashcode.

Parameters
interceptors:CallableProcessingInterceptor[]

one or more interceptors to register

registerDeferredResultInterceptorback to summary
public void registerDeferredResultInterceptor(Object key, DeferredResultProcessingInterceptor interceptor)

Register a DeferredResultProcessingInterceptor under the given key.

Parameters
key:Object

the key

interceptor:DeferredResultProcessingInterceptor

the interceptor to register

registerDeferredResultInterceptorsback to summary
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.

Parameters
interceptors:DeferredResultProcessingInterceptor[]

one or more interceptors to register

setAsyncWebRequestback to summary
public void setAsyncWebRequest(AsyncWebRequest asyncWebRequest)

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 isConcurrentHandlingStarted() is true.

Parameters
asyncWebRequest:AsyncWebRequest

the web request to use

setConcurrentResultAndDispatchback to summary
private void setConcurrentResultAndDispatch(Object result)
setTaskExecutorback to summary
public void setTaskExecutor(AsyncTaskExecutor taskExecutor)

Configure an AsyncTaskExecutor for use with concurrent processing via startCallableProcessing(Callable, Object...).

By default a SimpleAsyncTaskExecutor instance is used.

startAsyncProcessingback to summary
private void startAsyncProcessing(Object[] processingContext)
startCallableProcessingback to summary
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.

Parameters
callable:Callable<?>

a unit of work to be executed asynchronously

processingContext:Object[]

additional context to save that can be accessed via getConcurrentResultContext()

Annotations
@SuppressWarnings:rawtypes, unchecked
Exceptions
Exception:
if concurrent processing failed to start
See Also
getConcurrentResult(), getConcurrentResultContext()
startCallableProcessingback to summary
public void startCallableProcessing(final 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...).

Parameters
webAsyncTask:WebAsyncTask<?>

a WebAsyncTask containing the target Callable

processingContext:Object[]

additional context to save that can be accessed via getConcurrentResultContext()

Exceptions
Exception:
if concurrent processing failed to start
startDeferredResultProcessingback to summary
public void startDeferredResultProcessing(final DeferredResult<?> deferredResult, Object... processingContext) throws Exception

Start concurrent request processing and initialize the given DeferredResult with a 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.

Parameters
deferredResult:DeferredResult<?>

the DeferredResult instance to initialize

processingContext:Object[]

additional context to save that can be accessed via getConcurrentResultContext()

Exceptions
Exception:
if concurrent processing failed to start
See Also
getConcurrentResult(), getConcurrentResultContext()