Example usage for java.util.concurrent CompletionStage whenComplete

List of usage examples for java.util.concurrent CompletionStage whenComplete

Introduction

In this page you can find the example usage for java.util.concurrent CompletionStage whenComplete.

Prototype

public CompletionStage<T> whenComplete(BiConsumer<? super T, ? super Throwable> action);

Source Link

Document

Returns a new CompletionStage with the same result or exception as this stage, that executes the given action when this stage completes.

Usage

From source file:fi.minedu.oiva.backend.spring.handler.CompletionStageReturnValueHandler.java

@SuppressWarnings("unchecked")
@Override//from w w  w .  j a v  a 2s.c  o m
public void handleReturnValue(Object returnValue, MethodParameter returnType,
        ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception {
    if (returnValue == null) {
        mavContainer.setRequestHandled(true);
        return;
    }

    final DeferredResult<Object> deferredResult = new DeferredResult<>();
    WebAsyncUtils.getAsyncManager(webRequest).startDeferredResultProcessing(deferredResult, mavContainer);

    CompletionStage<?> future = (CompletionStage<?>) returnValue;
    future.whenComplete((result, ex) -> {
        if (ex != null) {
            deferredResult.setErrorResult(ex);
        } else {
            deferredResult.setResult(result);
        }
    });
}