Example usage for org.springframework.web.context.request.async DeferredResult getTimeoutValue

List of usage examples for org.springframework.web.context.request.async DeferredResult getTimeoutValue

Introduction

In this page you can find the example usage for org.springframework.web.context.request.async DeferredResult getTimeoutValue.

Prototype

@Nullable
final Long getTimeoutValue() 

Source Link

Document

Return the configured timeout value in milliseconds.

Usage

From source file:org.springframework.web.context.request.async.WebAsyncManager.java

/**
 * Start concurrent request processing and initialize the given
 * {@link DeferredResult} with a {@link DeferredResultHandler} that saves
 * the result and dispatches the request to resume processing of that
 * result. The {@code AsyncWebRequest} is also updated with a completion
 * handler that expires the {@code DeferredResult} and a timeout handler
 * assuming the {@code DeferredResult} has a default timeout result.
 * @param deferredResult the DeferredResult instance to initialize
 * @param processingContext additional context to save that can be accessed
 * via {@link #getConcurrentResultContext()}
 * @throws Exception if concurrent processing failed to start
 * @see #getConcurrentResult()/*  w  w w .  ja v  a 2s.c o  m*/
 * @see #getConcurrentResultContext()
 */
public void startDeferredResultProcessing(final DeferredResult<?> deferredResult, Object... processingContext)
        throws Exception {

    Assert.notNull(deferredResult, "DeferredResult must not be null");
    Assert.state(this.asyncWebRequest != null, "AsyncWebRequest must not be null");

    Long timeout = deferredResult.getTimeoutValue();
    if (timeout != null) {
        this.asyncWebRequest.setTimeout(timeout);
    }

    List<DeferredResultProcessingInterceptor> interceptors = new ArrayList<>();
    interceptors.add(deferredResult.getInterceptor());
    interceptors.addAll(this.deferredResultInterceptors.values());
    interceptors.add(timeoutDeferredResultInterceptor);

    final DeferredResultInterceptorChain interceptorChain = new DeferredResultInterceptorChain(interceptors);

    this.asyncWebRequest.addTimeoutHandler(() -> {
        try {
            interceptorChain.triggerAfterTimeout(this.asyncWebRequest, deferredResult);
        } catch (Throwable ex) {
            setConcurrentResultAndDispatch(ex);
        }
    });

    this.asyncWebRequest.addErrorHandler(ex -> {
        try {
            if (!interceptorChain.triggerAfterError(this.asyncWebRequest, deferredResult, ex)) {
                return;
            }
            deferredResult.setErrorResult(ex);
        } catch (Throwable interceptorEx) {
            setConcurrentResultAndDispatch(interceptorEx);
        }
    });

    this.asyncWebRequest.addCompletionHandler(
            () -> interceptorChain.triggerAfterCompletion(this.asyncWebRequest, deferredResult));

    interceptorChain.applyBeforeConcurrentHandling(this.asyncWebRequest, deferredResult);
    startAsyncProcessing(processingContext);

    try {
        interceptorChain.applyPreProcess(this.asyncWebRequest, deferredResult);
        deferredResult.setResultHandler(result -> {
            result = interceptorChain.applyPostProcess(this.asyncWebRequest, deferredResult, result);
            setConcurrentResultAndDispatch(result);
        });
    } catch (Throwable ex) {
        setConcurrentResultAndDispatch(ex);
    }
}