Spring Framework

org.springframework.web.context.request.async
Class DeferredResult

java.lang.Object
  extended by org.springframework.web.context.request.async.DeferredResult

public final class DeferredResult
extends java.lang.Object

DeferredResult provides an alternative to using a Callable for async request processing. With a Callable the framework manages a thread on behalf of the application through an AsyncTaskExecutor. With a DeferredResult the application sets the result in a thread of its choice.

The following sequence describes the intended use scenario:

  1. thread-1: framework calls application method
  2. thread-1: application method returns a DeferredResult
  3. thread-1: framework initializes DeferredResult
  4. thread-2: application calls set(Object)
  5. thread-2: framework completes async processing with given result

If the application calls set(Object) in thread-2 before the DeferredResult is initialized by the framework in thread-1, then thread-2 will block and wait for the initialization to complete. Therefore an application should never create and set the DeferredResult in the same thread because the initialization will never complete.

Since:
3.2
Author:
Rossen Stoyanchev

Constructor Summary
DeferredResult()
          Create a new instance.
DeferredResult(java.lang.Object timeoutResult)
          Create a new instance and also provide a default result to use if a timeout occurs before set(Object) is called.
 
Method Summary
 void set(java.lang.Object result)
          Complete async processing with the given result.
 boolean trySet(java.lang.Object result)
          A variant of set(Object) that absorbs a potential, resulting StaleAsyncWebRequestException.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DeferredResult

public DeferredResult()
Create a new instance.


DeferredResult

public DeferredResult(java.lang.Object timeoutResult)
Create a new instance and also provide a default result to use if a timeout occurs before set(Object) is called.

Method Detail

set

public void set(java.lang.Object result)
         throws StaleAsyncWebRequestException
Complete async processing with the given result. If the DeferredResult is not yet fully initialized, this method will block and wait for that to occur before proceeding. See the class level javadoc for more details.

Throws:
StaleAsyncWebRequestException - if the underlying async request has already timed out or ended due to a network error.

trySet

public boolean trySet(java.lang.Object result)
               throws StaleAsyncWebRequestException
A variant of set(Object) that absorbs a potential, resulting StaleAsyncWebRequestException.

Returns:
false if the outcome was a StaleAsyncWebRequestException
Throws:
StaleAsyncWebRequestException

Spring Framework