org.springframework.web.context.request.async

Class DeferredResult<T>

  • java.lang.Object
    • org.springframework.web.context.request.async.DeferredResult<T>
  • Type Parameters:
    T - the result type


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

    Since:
    3.2
    Author:
    Rossen Stoyanchev, Juergen Hoeller, Rob Winch
    • Constructor Summary

      Constructors 
      Constructor and Description
      DeferredResult()
      Create a DeferredResult.
      DeferredResult(java.lang.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.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method and Description
      java.lang.Object getResult()
      Return the result, or null if the result wasn't set.
      boolean hasResult()
      Return true if the DeferredResult has been set.
      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 onError(java.util.function.Consumer<java.lang.Throwable> callback)
      Register code to invoke when an error occurred during the async request.
      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.
      • 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 DeferredResult.
      • DeferredResult

        public DeferredResult(java.lang.Long timeout)
        Create a DeferredResult with a 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:
        timeout - timeout value in milliseconds
      • DeferredResult

        public DeferredResult(@Nullable
                              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.
        Parameters:
        timeout - timeout value in milliseconds (ignored if null)
        timeoutResult - the result to use
    • Method Detail

      • isSetOrExpired

        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.

      • hasResult

        public boolean hasResult()
        Return true if the DeferredResult has been set.
        Since:
        4.0
      • getResult

        @Nullable
        public java.lang.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.
        Since:
        4.0
      • onTimeout

        public void onTimeout(java.lang.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.

      • onError

        public void onError(java.util.function.Consumer<java.lang.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
      • onCompletion

        public void onCompletion(java.lang.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.

      • setResult

        public boolean setResult(T result)
        Set the value for the DeferredResult and handle it.
        Parameters:
        result - the value to set
        Returns:
        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()
      • setErrorResult

        public boolean setErrorResult(java.lang.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 - the error result value
        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
        See Also:
        isSetOrExpired()