Example usage for com.google.common.util.concurrent FutureCallback onSuccess

List of usage examples for com.google.common.util.concurrent FutureCallback onSuccess

Introduction

In this page you can find the example usage for com.google.common.util.concurrent FutureCallback onSuccess.

Prototype

void onSuccess(@Nullable V result);

Source Link

Document

Invoked with the result of the Future computation when it is successful.

Usage

From source file:org.thingsboard.server.service.security.AccessValidator.java

private <T, V> FutureCallback<T> getCallback(FutureCallback<ValidationResult> callback,
        Function<T, ValidationResult<V>> transformer) {
    return new FutureCallback<T>() {
        @Override/*from ww w. j  a  v a  2s . co  m*/
        public void onSuccess(@Nullable T result) {
            callback.onSuccess(transformer.apply(result));
        }

        @Override
        public void onFailure(Throwable t) {
            callback.onFailure(t);
        }
    };
}

From source file:ch.icclab.netfloc.impl.NetflocServiceImpl.java

private void commitWriteTransaction(final ReadWriteTransaction wt, final FutureCallback<Void> cb,
        final int totalTries, final int tries) {
    Futures.addCallback(wt.submit(), new FutureCallback<Void>() {
        public void onSuccess(Void result) {
            logger.info("Transaction success after {} tries for {}", totalTries - tries + 1, wt);
            cb.onSuccess(result);
        }/* w  w  w.jav  a  2  s.  c o  m*/

        public void onFailure(Throwable t) {
            if (t instanceof OptimisticLockFailedException) {
                if ((tries - 1) > 0) {
                    logger.warn("Transaction retry {} for {}", totalTries - tries + 1, wt);
                    commitWriteTransaction(wt, cb, totalTries, tries - 1);
                } else {
                    logger.error("Transaction out of retries: ", wt);
                    cb.onFailure(t);
                }
            } else {
                if (t instanceof DataValidationFailedException) {
                    logger.error("Transaction validation failed {}", t.getMessage());
                } else {
                    logger.error("Transaction failed {}", t.getMessage());
                }
                cb.onFailure(t);
            }
        }
    });
}

From source file:co.cask.cdap.common.zookeeper.coordination.ResourceCoordinatorClient.java

/**
 * Wraps a FutureCallback so that it only get triggered if this service is running.
 *
 * @param callback The callback to wrap.
 * @param <V> Type of the callback result.
 * @return A wrapped FutureCallback.//from w w w. j  av  a 2 s.c  om
 */
private <V> FutureCallback<V> wrapCallback(final FutureCallback<V> callback) {
    return new FutureCallback<V>() {
        @Override
        public void onSuccess(V result) {
            if (isRunning()) {
                callback.onSuccess(result);
            }
        }

        @Override
        public void onFailure(Throwable t) {
            if (isRunning()) {
                callback.onFailure(t);
            }
        }
    };
}

From source file:com.vmware.photon.controller.clustermanager.statuschecks.MesosStatusChecker.java

private void checkStatus(String nodeAddress, final FutureCallback<String> callback) throws IOException {
    Preconditions.checkNotNull(nodeAddress, "nodeAddress cannot be null");
    logger.info("Checking Mesos: {}", nodeAddress);

    String connectionString = createConnectionString(nodeAddress);
    mesosClient.getMasterLeader(connectionString, new FutureCallback<String>() {
        @Override//from w  w w.j av  a2  s. co  m
        public void onSuccess(@Nullable String leaderConnectionString) {
            callback.onSuccess(leaderConnectionString);
        }

        @Override
        public void onFailure(Throwable t) {
            logger.warn("Mesos call failed: ", t);
            callback.onFailure(t);
        }
    });
}

From source file:com.vmware.photon.controller.clustermanager.clients.MesosClient.java

/**
 * This method calls into the Marathon API endpoint and determine its status
 * based on http status./*  w w w  . j a va  2s.  co m*/
 *
 * @param connectionString          connectionString of the marathon Node
 * @param callback                  callback that is invoked on completion of the operation.
 * @throws IOException
 */
public void checkMarathon(final String connectionString, final FutureCallback<Boolean> callback)
        throws IOException {

    final RestClient restClient = new RestClient(connectionString, this.httpClient);

    org.apache.http.concurrent.FutureCallback futureCallback = new org.apache.http.concurrent.FutureCallback<HttpResponse>() {
        @Override
        public void completed(HttpResponse result) {
            callback.onSuccess(result.getStatusLine().getStatusCode() == HttpStatus.SC_OK);
        }

        @Override
        public void failed(Exception ex) {
            callback.onFailure(ex);
        }

        @Override
        public void cancelled() {
            callback.onFailure(new RuntimeException("checkMarathon was cancelled"));
        }
    };

    restClient.performAsync(RestClient.Method.GET, MARATHON_PATH, null, futureCallback);
}

From source file:com.vmware.photon.controller.clustermanager.rolloutplans.SlavesNodeRollout.java

private void waitForNodes(final Service service, final NodeRolloutInput input, final List<String> nodeAddresses,
        final FutureCallback<NodeRolloutResult> responseFutureCallback) {

    FutureCallback<ClusterWaitTaskService.State> callback = new FutureCallback<ClusterWaitTaskService.State>() {
        @Override//from ww w  .  ja va  2 s.  com
        public void onSuccess(@Nullable ClusterWaitTaskService.State result) {
            switch (result.taskState.stage) {
            case FINISHED:
                NodeRolloutResult response = new NodeRolloutResult();
                response.nodeAddresses = nodeAddresses;
                responseFutureCallback.onSuccess(response);
                break;
            case CANCELLED:
                responseFutureCallback.onFailure(new IllegalStateException(
                        String.format("ClusterWaitTaskService was canceled. %s", result.documentSelfLink)));
                break;
            case FAILED:
                responseFutureCallback.onFailure(new IllegalStateException(
                        String.format("ClusterWaitTaskService failed with error %s. %s",
                                result.taskState.failure.message, result.documentSelfLink)));
                break;
            }
        }

        @Override
        public void onFailure(Throwable t) {
            responseFutureCallback.onFailure(t);
        }
    };

    ClusterWaitTaskService.State startState = new ClusterWaitTaskService.State();
    startState.nodeType = input.nodeType;
    startState.serverAddress = input.serverAddress;
    startState.nodeAddresses = nodeAddresses;

    TaskUtils.startTaskAsync(service, ClusterWaitTaskFactoryService.SELF_LINK, startState,
            (state) -> TaskUtils.finalTaskStages.contains(state.taskState.stage),
            ClusterWaitTaskService.State.class, ClusterManagerConstants.DEFAULT_TASK_POLL_DELAY, callback);
}

From source file:com.vmware.photon.controller.clustermanager.rolloutplans.WorkersNodeRollout.java

private void waitForNodes(final Service service, final NodeRolloutInput input, final List<String> nodeAddresses,
        final FutureCallback<NodeRolloutResult> responseFutureCallback) {

    FutureCallback<ClusterWaitTaskService.State> callback = new FutureCallback<ClusterWaitTaskService.State>() {
        @Override//from   w  ww  .ja  v a 2  s .  co  m
        public void onSuccess(@Nullable ClusterWaitTaskService.State result) {
            switch (result.taskState.stage) {
            case FINISHED:
                NodeRolloutResult response = new NodeRolloutResult();
                response.nodeAddresses = nodeAddresses;
                responseFutureCallback.onSuccess(response);
                break;
            case CANCELLED:
                responseFutureCallback.onFailure(new IllegalStateException(
                        String.format("ClusterWaitTaskService for clusterID %s was canceled. %s",
                                input.clusterId, result.documentSelfLink)));
                break;
            case FAILED:
                responseFutureCallback.onFailure(new IllegalStateException(
                        String.format("ClusterWaitTaskService for clusterID %s failed with error %s. %s",
                                input.clusterId, result.taskState.failure.message, result.documentSelfLink)));
                break;
            }
        }

        @Override
        public void onFailure(Throwable t) {
            responseFutureCallback.onFailure(t);
        }
    };

    ClusterWaitTaskService.State startState = new ClusterWaitTaskService.State();
    startState.nodeType = input.nodeType;
    startState.serverAddress = input.serverAddress;
    startState.nodeAddresses = nodeAddresses;
    startState.clusterId = input.clusterId;

    TaskUtils.startTaskAsync(service, ClusterWaitTaskFactoryService.SELF_LINK, startState,
            (state) -> TaskUtils.finalTaskStages.contains(state.taskState.stage),
            ClusterWaitTaskService.State.class, ClusterManagerConstants.DEFAULT_TASK_POLL_DELAY, callback);
}

From source file:co.cask.cdap.common.zookeeper.coordination.ResourceCoordinator.java

/**
 * Wraps a given callback so that it only get triggered if {@link #shouldProcess()} returns true.
 *///from   ww w.  j a va2 s  .c  om
private <T> FutureCallback<T> wrapCallback(final FutureCallback<T> callback) {
    return new FutureCallback<T>() {
        @Override
        public void onSuccess(T result) {
            if (shouldProcess()) {
                callback.onSuccess(result);
            }
        }

        @Override
        public void onFailure(Throwable t) {
            if (shouldProcess()) {
                callback.onFailure(t);
            }
        }
    };
}

From source file:com.vmware.photon.controller.clustermanager.statuschecks.SwarmStatusChecker.java

@Override
public void checkNodeStatus(final String serverAddress, final FutureCallback<Boolean> callback) {
    Preconditions.checkNotNull(serverAddress, "serverAddress cannot be null");
    logger.info("Checking Swarm: {}", serverAddress);

    try {//from   w w  w  . j a va 2  s . co m
        String connectionString = createConnectionString(serverAddress);
        swarmClient.getNodeAddressesAsync(connectionString, new FutureCallback<Set<String>>() {
            @Override
            public void onSuccess(@Nullable Set<String> nodes) {
                if (!nodes.contains(serverAddress)) {
                    logger.info("Node not registered with Swarm: {}", serverAddress);
                    callback.onSuccess(false);
                    return;
                }
                callback.onSuccess(true);
            }

            @Override
            public void onFailure(Throwable t) {
                logger.warn("Swarm call failed: ", t);
                callback.onSuccess(false);
            }
        });
    } catch (Exception e) {
        logger.warn("Swarm call failed: ", e);
        callback.onSuccess(false);
    }
}

From source file:com.vmware.photon.controller.clustermanager.statuschecks.MesosStatusChecker.java

private void checkSlaves(final String connectionString, final List<String> slaveAddresses,
        final FutureCallback<Boolean> callback) {
    logger.info("Checking Mesos: {}", connectionString);

    try {/*from   w  ww .j  a  v  a  2  s.  c  o m*/
        mesosClient.getNodeAddressesAsync(connectionString, new FutureCallback<Set<String>>() {
            @Override
            public void onSuccess(@Nullable Set<String> nodes) {
                try {
                    for (String nodeAddress : slaveAddresses) {
                        if (!nodes.contains(nodeAddress)) {
                            logger.info("Node not registered with Mesos Master: {}", nodeAddress);
                            callback.onSuccess(false);
                            return;
                        }
                    }
                    callback.onSuccess(true);
                } catch (Throwable t) {
                    logger.warn("Failed to process Mesos nodes: ", t);
                    callback.onFailure(t);
                }
            }

            @Override
            public void onFailure(Throwable t) {
                logger.warn("Mesos call failed: ", t);
                callback.onSuccess(false);
            }
        });
    } catch (Exception e) {
        logger.warn("Mesos call failed: ", e);
        callback.onSuccess(false);
    }
}