Example usage for org.springframework.web.context.request.async WebAsyncTask getCallable

List of usage examples for org.springframework.web.context.request.async WebAsyncTask getCallable

Introduction

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

Prototype

public Callable<?> getCallable() 

Source Link

Document

Return the Callable to use for concurrent handling (never null ).

Usage

From source file:org.springframework.cloud.sleuth.instrument.web.TraceWebAspect.java

@Around("anyControllerOrRestControllerWithPublicWebAsyncTaskMethod()")
public Object wrapWebAsyncTaskWithCorrelationId(ProceedingJoinPoint pjp) throws Throwable {
    final WebAsyncTask<?> webAsyncTask = (WebAsyncTask<?>) pjp.proceed();
    if (this.tracer.isTracing()) {
        try {/*from w  ww.jav  a 2s.c o  m*/
            if (log.isDebugEnabled()) {
                log.debug("Wrapping callable with span [" + this.tracer.getCurrentSpan() + "]");
            }
            Field callableField = WebAsyncTask.class.getDeclaredField("callable");
            callableField.setAccessible(true);
            callableField.set(webAsyncTask, new SpanContinuingTraceCallable<>(this.tracer, this.traceKeys,
                    this.spanNamer, webAsyncTask.getCallable()));
        } catch (NoSuchFieldException ex) {
            log.warn("Cannot wrap webAsyncTask's callable with TraceCallable", ex);
        }
    }
    return webAsyncTask;
}

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
 *//*  ww w .j  a v a  2  s.  co m*/
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;
    }
}