org.springframework.web.context.request.async

Interface DeferredResultProcessingInterceptor

  • All Known Implementing Classes:
    DeferredResultProcessingInterceptorAdapter, TimeoutDeferredResultProcessingInterceptor


    public interface DeferredResultProcessingInterceptor
    Intercepts concurrent request handling, where the concurrent result is obtained by waiting for a DeferredResult to be set from a thread chosen by the application (e.g. in response to some external event).

    A DeferredResultProcessingInterceptor is invoked before the start of async processing, after the DeferredResult is set as well as on timeout/error, or after completing for any reason including a timeout or network error.

    As a general rule exceptions raised by interceptor methods will cause async processing to resume by dispatching back to the container and using the Exception instance as the concurrent result. Such exceptions will then be processed through the HandlerExceptionResolver mechanism.

    The handleTimeout method can set the DeferredResult in order to resume processing.

    Since:
    3.2
    Author:
    Rossen Stoyanchev, Rob Winch
    • Method Detail

      • beforeConcurrentHandling

        default <T> void beforeConcurrentHandling(NativeWebRequest request,
                                                  DeferredResult<T> deferredResult)
                                           throws java.lang.Exception
        Invoked immediately before the start of concurrent handling, in the same thread that started it. This method may be used to capture state just prior to the start of concurrent processing with the given DeferredResult.
        Parameters:
        request - the current request
        deferredResult - the DeferredResult for the current request
        Throws:
        java.lang.Exception - in case of errors
      • preProcess

        default <T> void preProcess(NativeWebRequest request,
                                    DeferredResult<T> deferredResult)
                             throws java.lang.Exception
        Invoked immediately after the start of concurrent handling, in the same thread that started it. This method may be used to detect the start of concurrent processing with the given DeferredResult.

        The DeferredResult may have already been set, for example at the time of its creation or by another thread.

        Parameters:
        request - the current request
        deferredResult - the DeferredResult for the current request
        Throws:
        java.lang.Exception - in case of errors
      • postProcess

        default <T> void postProcess(NativeWebRequest request,
                                     DeferredResult<T> deferredResult,
                                     java.lang.Object concurrentResult)
                              throws java.lang.Exception
        Invoked after a DeferredResult has been set, via DeferredResult.setResult(Object) or DeferredResult.setErrorResult(Object), and is also ready to handle the concurrent result.

        This method may also be invoked after a timeout when the DeferredResult was created with a constructor accepting a default timeout result.

        Parameters:
        request - the current request
        deferredResult - the DeferredResult for the current request
        concurrentResult - the result to which the DeferredResult
        Throws:
        java.lang.Exception - in case of errors
      • handleTimeout

        default <T> boolean handleTimeout(NativeWebRequest request,
                                          DeferredResult<T> deferredResult)
                                   throws java.lang.Exception
        Invoked from a container thread when an async request times out before the DeferredResult has been set. Implementations may invoke setResult or setErrorResult to resume processing.
        Parameters:
        request - the current request
        deferredResult - the DeferredResult for the current request; if the DeferredResult is set, then concurrent processing is resumed and subsequent interceptors are not invoked
        Returns:
        true if processing should continue, or false if other interceptors should not be invoked
        Throws:
        java.lang.Exception - in case of errors
      • handleError

        default <T> boolean handleError(NativeWebRequest request,
                                        DeferredResult<T> deferredResult,
                                        java.lang.Throwable t)
                                 throws java.lang.Exception
        Invoked from a container thread when an error occurred while processing an async request before the DeferredResult has been set. Implementations may invoke setResult or setErrorResult to resume processing.
        Parameters:
        request - the current request
        deferredResult - the DeferredResult for the current request; if the DeferredResult is set, then concurrent processing is resumed and subsequent interceptors are not invoked
        t - the error that occurred while request processing
        Returns:
        true if error handling should continue, or false if other interceptors should by bypassed and not be invoked
        Throws:
        java.lang.Exception - in case of errors
      • afterCompletion

        default <T> void afterCompletion(NativeWebRequest request,
                                         DeferredResult<T> deferredResult)
                                  throws java.lang.Exception
        Invoked from a container thread when an async request completed for any reason including timeout and network error. This method is useful for detecting that a DeferredResult instance is no longer usable.
        Parameters:
        request - the current request
        deferredResult - the DeferredResult for the current request
        Throws:
        java.lang.Exception - in case of errors