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

public Class DeferredResult<T>

extends Object
Class Inheritance
Type Parameters
<T>
the result type
Imports
java.util.PriorityQueue, java.util.concurrent.Callable, java.util.function.Consumer, .Supplier, org.apache.commons.logging.Log, .LogFactory, org.springframework.lang.Nullable, org.springframework.util.Assert, org.springframework.web.context.request.NativeWebRequest

DeferredResult provides an alternative to using a Callable for asynchronous request processing. While a Callable is executed concurrently on behalf of the application, with a DeferredResult the application can produce the result from a thread of its choice.

Subclasses can extend this class to easily associate additional data or behavior with the DeferredResult. For example, one might want to associate the user used to create the DeferredResult by extending the class and adding an additional property for the user. In this way, the user could easily be accessed later without the need to use a data structure to do the mapping.

An example of associating additional behavior to this class might be realized by extending the class to implement an additional interface. For example, one might want to implement Comparable so that when the DeferredResult is added to a PriorityQueue it is handled in the correct order.

Authors
Rossen Stoyanchev, Juergen Hoeller, Rob Winch, Sam Brannen
Since
3.2

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static interface
DeferredResult.DeferredResultHandler

Handles a DeferredResult value when set.

Field Summary

Modifier and TypeField and Description
private Runnable
private Consumer<Throwable>
private volatile boolean
private static final Log
private volatile Object
private static final Object
private DeferredResult.DeferredResultHandler
private Runnable
private final Supplier<?>
private final Long

Constructor Summary

AccessConstructor and Description
public
DeferredResult()

Create a DeferredResult.

public
DeferredResult(Long
timeout value in milliseconds
timeoutValue
)

Create a DeferredResult with a custom timeout value.

public
DeferredResult(Long
timeout value in milliseconds (ignored if null)
timeoutValue
,
Object
the result to use
timeoutResult
)

Create a DeferredResult with a timeout value and a default result to use in case of timeout.

public
DeferredResult(Long
timeout value in milliseconds (ignored if null)
timeoutValue
,
Supplier<?>
the result supplier to use
timeoutResult
)

Variant of DeferredResult(Long, Object) that accepts a dynamic fallback value based on a Supplier.

Method Summary

Modifier and TypeMethod and Description
pack-priv final DeferredResultProcessingInterceptor
public Object
getResult()

Return the result, or null if the result wasn't set.

pack-priv final Long
getTimeoutValue()

Return the configured timeout value in milliseconds.

public boolean
hasResult()

Return true if the DeferredResult has been set.

public final boolean
isSetOrExpired()

Return true if this DeferredResult is no longer usable either because it was previously set or because the underlying request expired.

public void
onCompletion(Runnable callback)

Register code to invoke when the async request completes.

public void
onError(Consumer<Throwable> callback)

Register code to invoke when an error occurred during the async request.

public void
onTimeout(Runnable callback)

Register code to invoke when the async request times out.

public boolean

Returns:

true if the result was set to the error value and passed on for handling; false if the result was already set or the async request expired
setErrorResult
(Object
the error result value
result
)

Set an error value for the DeferredResult and handle it.

public boolean

Returns:

true if the result was set and passed on for handling; false if the result was already set or the async request expired
setResult
(T
the value to set
result
)

Set the value for the DeferredResult and handle it.

public final void
setResultHandler(DeferredResult.DeferredResultHandler
the handler
resultHandler
)

Provide a handler to use to handle the result value.

private boolean
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

completionCallbackback to summary
private Runnable completionCallback
Annotations
@Nullable
errorCallbackback to summary
private Consumer<Throwable> errorCallback
Annotations
@Nullable
expiredback to summary
private volatile boolean expired
loggerback to summary
private static final Log logger
resultback to summary
private volatile Object result
Annotations
@Nullable
RESULT_NONEback to summary
private static final Object RESULT_NONE
resultHandlerback to summary
private DeferredResult.DeferredResultHandler resultHandler
Annotations
@Nullable
timeoutCallbackback to summary
private Runnable timeoutCallback
Annotations
@Nullable
timeoutResultback to summary
private final Supplier<?> timeoutResult
timeoutValueback to summary
private final Long timeoutValue
Annotations
@Nullable

Constructor Detail

DeferredResultback to summary
public DeferredResult()

Create a DeferredResult.

DeferredResultback to summary
public DeferredResult(Long timeoutValue)

Create a DeferredResult with a custom timeout value.

By default not set in which case the default configured in the MVC Java Config or the MVC namespace is used, or if that's not set, then the timeout depends on the default of the underlying server.

Parameters
timeoutValue:Long

timeout value in milliseconds

DeferredResultback to summary
public DeferredResult(Long timeoutValue, Object timeoutResult)

Create a DeferredResult with a timeout value and a default result to use in case of timeout.

Parameters
timeoutValue:Long

timeout value in milliseconds (ignored if null)

timeoutResult:Object

the result to use

DeferredResultback to summary
public DeferredResult(Long timeoutValue, Supplier<?> timeoutResult)

Variant of DeferredResult(Long, Object) that accepts a dynamic fallback value based on a Supplier.

Parameters
timeoutValue:Long

timeout value in milliseconds (ignored if null)

timeoutResult:Supplier<?>

the result supplier to use

Since
5.1.1

Method Detail

getInterceptorback to summary
pack-priv final DeferredResultProcessingInterceptor getInterceptor()
getResultback to summary
public Object getResult()

Return the result, or null if the result wasn't set. Since the result can also be null, it is recommended to use hasResult() first to check if there is a result prior to calling this method.

Annotations
@Nullable
Since
4.0
getTimeoutValueback to summary
pack-priv final Long getTimeoutValue()

Return the configured timeout value in milliseconds.

Annotations
@Nullable
hasResultback to summary
public boolean hasResult()

Return true if the DeferredResult has been set.

Since
4.0
isSetOrExpiredback to summary
public final boolean isSetOrExpired()

Return true if this DeferredResult is no longer usable either because it was previously set or because the underlying request expired.

The result may have been set with a call to setResult(Object), or setErrorResult(Object), or as a result of a timeout, if a timeout result was provided to the constructor. The request may also expire due to a timeout or network error.

onCompletionback to summary
public void onCompletion(Runnable callback)

Register code to invoke when the async request completes.

This method is called from a container thread when an async request completed for any reason including timeout and network error. This is useful for detecting that a DeferredResult instance is no longer usable.

onErrorback to summary
public void onError(Consumer<Throwable> callback)

Register code to invoke when an error occurred during the async request.

This method is called from a container thread when an error occurs while processing an async request before the DeferredResult has been populated. It may invoke setResult or setErrorResult to resume processing.

Since
5.0
onTimeoutback to summary
public void onTimeout(Runnable callback)

Register code to invoke when the async request times out.

This method is called from a container thread when an async request times out before the DeferredResult has been populated. It may invoke setResult or setErrorResult to resume processing.

setErrorResultback to summary
public boolean setErrorResult(Object result)

Set an error value for the DeferredResult and handle it. The value may be an Exception or Throwable in which case it will be processed as if a handler raised the exception.

Parameters
result:Object

the error result value

Returns:boolean

true if the result was set to the error value and passed on for handling; false if the result was already set or the async request expired

See Also
isSetOrExpired()
setResultback to summary
public boolean setResult(T result)

Set the value for the DeferredResult and handle it.

Parameters
result:T

the value to set

Returns:boolean

true if the result was set and passed on for handling; false if the result was already set or the async request expired

See Also
isSetOrExpired()
setResultHandlerback to summary
public final void setResultHandler(DeferredResult.DeferredResultHandler resultHandler)

Provide a handler to use to handle the result value.

Parameters
resultHandler:DeferredResult.DeferredResultHandler

the handler

See Also
DeferredResultProcessingInterceptor
setResultInternalback to summary
private boolean setResultInternal(Object result)
org.springframework.web.context.request.async back to summary

public Interface DeferredResult.DeferredResultHandler

Annotations
@FunctionalInterface

Handles a DeferredResult value when set.

Method Summary

Modifier and TypeMethod and Description
public void

Method Detail

handleResultback to summary
public void handleResult(Object result)