Search in sources :

Example 1 with WaiterState

use of software.amazon.awssdk.core.waiters.WaiterState in project aws-sdk-java-v2 by aws.

the class AsyncWaiterExecutor method runAsyncPollingFunction.

private void runAsyncPollingFunction(Supplier<CompletableFuture<T>> asyncPollingFunction, CompletableFuture<WaiterResponse<T>> future, int attemptNumber, long startTime) {
    asyncPollingFunction.get().whenComplete((response, exception) -> {
        try {
            Either<T, Throwable> responseOrException;
            if (exception == null) {
                responseOrException = Either.left(response);
            } else {
                if (exception instanceof CompletionException) {
                    responseOrException = Either.right(exception.getCause());
                } else {
                    responseOrException = Either.right(exception);
                }
            }
            Optional<WaiterAcceptor<? super T>> optionalWaiterAcceptor = executorHelper.firstWaiterAcceptorIfMatched(responseOrException);
            if (optionalWaiterAcceptor.isPresent()) {
                WaiterAcceptor<? super T> acceptor = optionalWaiterAcceptor.get();
                WaiterState state = acceptor.waiterState();
                switch(state) {
                    case SUCCESS:
                        future.complete(executorHelper.createWaiterResponse(responseOrException, attemptNumber));
                        break;
                    case RETRY:
                        maybeRetry(asyncPollingFunction, future, attemptNumber, startTime);
                        break;
                    case FAILURE:
                        future.completeExceptionally(executorHelper.waiterFailureException(acceptor));
                        break;
                    default:
                        future.completeExceptionally(new UnsupportedOperationException());
                }
            } else {
                future.completeExceptionally(executorHelper.noneMatchException(responseOrException));
            }
        } catch (Throwable t) {
            future.completeExceptionally(SdkClientException.create("Encountered unexpected exception.", t));
        }
    });
}
Also used : WaiterAcceptor(software.amazon.awssdk.core.waiters.WaiterAcceptor) WaiterState(software.amazon.awssdk.core.waiters.WaiterState) CompletionException(java.util.concurrent.CompletionException)

Aggregations

CompletionException (java.util.concurrent.CompletionException)1 WaiterAcceptor (software.amazon.awssdk.core.waiters.WaiterAcceptor)1 WaiterState (software.amazon.awssdk.core.waiters.WaiterState)1