Example usage for org.springframework.web.context.request.async CallableProcessingInterceptor RESULT_NONE

List of usage examples for org.springframework.web.context.request.async CallableProcessingInterceptor RESULT_NONE

Introduction

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

Prototype

Object RESULT_NONE

To view the source code for org.springframework.web.context.request.async CallableProcessingInterceptor RESULT_NONE.

Click Source Link

Document

Constant indicating that no result has been determined by this interceptor, giving subsequent interceptors a chance.

Usage

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

public Object triggerAfterTimeout(NativeWebRequest request, Callable<?> task) {
    cancelTask();/*from   w ww  .  j a  va 2  s.c  o  m*/
    for (CallableProcessingInterceptor interceptor : this.interceptors) {
        try {
            Object result = interceptor.handleTimeout(request, task);
            if (result == CallableProcessingInterceptor.RESPONSE_HANDLED) {
                break;
            } else if (result != CallableProcessingInterceptor.RESULT_NONE) {
                return result;
            }
        } catch (Throwable t) {
            return t;
        }
    }
    return CallableProcessingInterceptor.RESULT_NONE;
}

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

public Object triggerAfterError(NativeWebRequest request, Callable<?> task, Throwable throwable) {
    cancelTask();//w  ww.  j  av a  2 s .  c  o  m
    for (CallableProcessingInterceptor interceptor : this.interceptors) {
        try {
            Object result = interceptor.handleError(request, task, throwable);
            if (result == CallableProcessingInterceptor.RESPONSE_HANDLED) {
                break;
            } else if (result != CallableProcessingInterceptor.RESULT_NONE) {
                return result;
            }
        } catch (Throwable t) {
            return t;
        }
    }
    return CallableProcessingInterceptor.RESULT_NONE;
}

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. j  a va  2s  .  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;
    }
}