Example usage for org.springframework.web.context.request.async CallableInterceptorChain applyPreProcess

List of usage examples for org.springframework.web.context.request.async CallableInterceptorChain applyPreProcess

Introduction

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

Prototype

public void applyPreProcess(NativeWebRequest request, Callable<?> task) throws Exception 

Source Link

Usage

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

/**
 * Use the given {@link WebAsyncTask} to configure the task executor as well as
 * the timeout value of the {@code AsyncWebRequest} before delegating to
 * {@link #startCallableProcessing(Callable, Object...)}.
 * @param webAsyncTask a WebAsyncTask containing the target {@code Callable}
 * @param processingContext additional context to save that can be accessed
 * via {@link #getConcurrentResultContext()}
 * @throws Exception if concurrent processing failed to start
 *///  w w  w .jav  a  2 s .  com
public void startCallableProcessing(final WebAsyncTask<?> webAsyncTask, Object... processingContext)
        throws Exception {

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

    Long timeout = webAsyncTask.getTimeout();
    if (timeout != null) {
        this.asyncWebRequest.setTimeout(timeout);
    }

    AsyncTaskExecutor executor = webAsyncTask.getExecutor();
    if (executor != null) {
        this.taskExecutor = executor;
    }

    List<CallableProcessingInterceptor> interceptors = new ArrayList<>();
    interceptors.add(webAsyncTask.getInterceptor());
    interceptors.addAll(this.callableInterceptors.values());
    interceptors.add(timeoutCallableInterceptor);

    final Callable<?> callable = webAsyncTask.getCallable();
    final CallableInterceptorChain interceptorChain = new CallableInterceptorChain(interceptors);

    this.asyncWebRequest.addTimeoutHandler(() -> {
        logger.debug("Processing timeout");
        Object result = interceptorChain.triggerAfterTimeout(this.asyncWebRequest, callable);
        if (result != CallableProcessingInterceptor.RESULT_NONE) {
            setConcurrentResultAndDispatch(result);
        }
    });

    this.asyncWebRequest.addErrorHandler(ex -> {
        logger.debug("Processing error");
        Object result = interceptorChain.triggerAfterError(this.asyncWebRequest, callable, ex);
        result = (result != CallableProcessingInterceptor.RESULT_NONE ? result : ex);
        setConcurrentResultAndDispatch(result);
    });

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

    interceptorChain.applyBeforeConcurrentHandling(this.asyncWebRequest, callable);
    startAsyncProcessing(processingContext);
    try {
        Future<?> future = this.taskExecutor.submit(() -> {
            Object result = null;
            try {
                interceptorChain.applyPreProcess(this.asyncWebRequest, callable);
                result = callable.call();
            } catch (Throwable ex) {
                result = ex;
            } finally {
                result = interceptorChain.applyPostProcess(this.asyncWebRequest, callable, result);
            }
            setConcurrentResultAndDispatch(result);
        });
        interceptorChain.setTaskFuture(future);
    } catch (RejectedExecutionException ex) {
        Object result = interceptorChain.applyPostProcess(this.asyncWebRequest, callable, ex);
        setConcurrentResultAndDispatch(result);
        throw ex;
    }
}