All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.ibm.cloud.objectstorage.waiters.WaiterImpl Maven / Gradle / Ivy

Go to download

A single bundled dependency that includes all service and dependent JARs with third-party libraries relocated to different namespaces.

There is a newer version: 2.13.4
Show newest version
/*
 * Copyright 2010-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License").
 * You may not use this file except in compliance with the License.
 * A copy of the License is located at
 *
 *  http://aws.amazon.com/apache2.0
 *
 * or in the "license" file accompanying this file. This file is distributed
 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 * express or implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */

package com.ibm.cloud.objectstorage.waiters;

import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;

import com.ibm.cloud.objectstorage.AmazonServiceException;
import com.ibm.cloud.objectstorage.AmazonWebServiceRequest;
import com.ibm.cloud.objectstorage.annotation.SdkProtectedApi;
import com.ibm.cloud.objectstorage.util.ValidationUtils;

@SdkProtectedApi
public class WaiterImpl implements Waiter{

    /**
     * Represents the operation function
     */
    private final SdkFunction sdkFunction;

    /**
     * List of acceptors
     */
    private final List> acceptors;

    /**
     * Represents the default polling strategy
     */
    private final PollingStrategy defaultPollingStrategy;

    private final ExecutorService executorService;

    /**
     * Constructs a new waiter with the given internal parameters
     *
     * @param waiterBuilder Takes in default parameters and builds a
     *                      basic waiter. Excludes request and custom
     *                      polling strategy parameters.
     */
    @SdkProtectedApi
    public WaiterImpl(WaiterBuilder waiterBuilder) {
        this.sdkFunction = ValidationUtils.assertNotNull(waiterBuilder.getSdkFunction(), "sdkFunction");
        this.acceptors = ValidationUtils.assertNotNull(waiterBuilder.getAcceptor(), "acceptors");
        this.defaultPollingStrategy = ValidationUtils.assertNotNull(waiterBuilder.getDefaultPollingStrategy(), "defaultPollingStrategy");
        this.executorService = ValidationUtils.assertNotNull(waiterBuilder.getExecutorService(), "executorService");
    }

    /**
     * Polls synchronously until it is determined that the resource
     * transitioned into the desired state or not.
     *
     * @param waiterParameters Custom provided parameters. Includes request and
     *                         optional custom polling strategy
     * @throws AmazonServiceException       If the service exception thrown doesn't match any of the expected
     *                                      exceptions, it's re-thrown.
     * @throws WaiterUnrecoverableException If the resource transitions into a failure/unexpected state.
     * @throws WaiterTimedOutException      If the resource doesn't transition into the desired state
     *                                      even after a certain number of retries.
     */
    public void run(WaiterParameters waiterParameters)
            throws AmazonServiceException, WaiterTimedOutException, WaiterUnrecoverableException {

        ValidationUtils.assertNotNull(waiterParameters, "waiterParameters");
        @SuppressWarnings("unchecked")
        Input request = (Input) ValidationUtils.assertNotNull(waiterParameters.getRequest(), "request").clone();
        request.getRequestClientOptions().appendUserAgent("waiter-request");
        WaiterExecution waiterExecution = new WaiterExecutionBuilder()
                .withRequest(request)
                .withPollingStrategy(waiterParameters.getPollingStrategy() != null ? waiterParameters.getPollingStrategy() : defaultPollingStrategy)
                .withAcceptors(acceptors)
                .withSdkFunction(sdkFunction)
                .build();

        waiterExecution.pollResource();

    }

    /**
     * Polls asynchronously until it is determined that the resource
     * transitioned into the desired state or not. Includes additional
     * callback.
     *
     * @param waiterParameters Custom provided parameters. Includes request and
     *                         optional custom polling strategy
     * @param callback         Custom callback
     * @return Future object that holds the result of an asynchronous
     * computation of waiter
     */
    public Future runAsync(final WaiterParameters waiterParameters, final WaiterHandler callback)
            throws AmazonServiceException, WaiterTimedOutException, WaiterUnrecoverableException {

        return executorService.submit(new java.util.concurrent.Callable() {
            @Override
            public Void call() throws Exception {
                try {
                    run(waiterParameters);
                    callback.onWaitSuccess(waiterParameters.getRequest());
                } catch (Exception ex) {
                    callback.onWaitFailure(ex);

                    throw ex;
                }
                return null;
            }
        });

    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy