public class DeferredResult<T>
extends java.lang.Object
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 addition 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 a Comparable
so that when the
DeferredResult
is added to a PriorityQueue
it is handled in
the correct order.
Modifier and Type | Class and Description |
---|---|
static interface |
DeferredResult.DeferredResultHandler
Handles a DeferredResult value when set.
|
Modifier and Type | Field and Description |
---|---|
private java.lang.Runnable |
completionCallback |
private boolean |
expired |
private static Log |
logger |
private java.lang.Object |
result |
private static java.lang.Object |
RESULT_NONE |
private DeferredResult.DeferredResultHandler |
resultHandler |
private java.lang.Long |
timeout |
private java.lang.Runnable |
timeoutCallback |
private java.lang.Object |
timeoutResult |
Constructor and Description |
---|
DeferredResult()
Create a DeferredResult.
|
DeferredResult(long timeout)
Create a DeferredResult with a timeout value.
|
DeferredResult(java.lang.Long timeout,
java.lang.Object timeoutResult)
Create a DeferredResult with a timeout value and a default result to use
in case of timeout.
|
Modifier and Type | Method and Description |
---|---|
(package private) DeferredResultProcessingInterceptor |
getInterceptor() |
(package private) java.lang.Long |
getTimeoutValue()
Return the configured timeout value in milliseconds.
|
boolean |
isSetOrExpired()
Return
true if this DeferredResult is no longer usable either
because it was previously set or because the underlying request expired. |
void |
onCompletion(java.lang.Runnable callback)
Register code to invoke when the async request completes.
|
void |
onTimeout(java.lang.Runnable callback)
Register code to invoke when the async request times out.
|
boolean |
setErrorResult(java.lang.Object result)
Set an error value for the
DeferredResult and handle it. |
boolean |
setResult(T result)
Set the value for the DeferredResult and handle it.
|
void |
setResultHandler(DeferredResult.DeferredResultHandler resultHandler)
Provide a handler to use to handle the result value.
|
private boolean |
setResultInternal(java.lang.Object result) |
private static final Log logger
private static final java.lang.Object RESULT_NONE
private final java.lang.Long timeout
private final java.lang.Object timeoutResult
private java.lang.Runnable timeoutCallback
private java.lang.Runnable completionCallback
private DeferredResult.DeferredResultHandler resultHandler
private java.lang.Object result
private boolean expired
public DeferredResult()
public DeferredResult(long timeout)
timeout
- timeout value in millisecondspublic DeferredResult(java.lang.Long timeout, java.lang.Object timeoutResult)
timeout
- timeout value in milliseconds; ignored if null
timeoutResult
- the result to usepublic final boolean isSetOrExpired()
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.
final java.lang.Long getTimeoutValue()
public void onTimeout(java.lang.Runnable callback)
DeferredResult
has been set. It may invoke
setResult
or
setErrorResult
to resume
processing.public void onCompletion(java.lang.Runnable callback)
DeferredResult
instance is no longer usable.public final void setResultHandler(DeferredResult.DeferredResultHandler resultHandler)
resultHandler
- the handlerDeferredResultProcessingInterceptor
public boolean setResult(T result)
result
- the value to setisSetOrExpired()
private boolean setResultInternal(java.lang.Object result)
public boolean setErrorResult(java.lang.Object result)
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.result
- the error result valueisSetOrExpired()
final DeferredResultProcessingInterceptor getInterceptor()