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

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

Introduction

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

Prototype

void onFailure(Throwable t);

Source Link

Document

Invoked when a Future computation fails or is canceled.

Usage

From source file:com.github.zhongl.api.Storage.java

private static void onFailure(Collection<? extends FutureCallback<Void>> callbacks, Throwable t) {
    for (FutureCallback<Void> callback : callbacks)
        callback.onFailure(t);
}

From source file:com.vmware.photon.controller.common.dcp.TaskUtils.java

public static <T extends ServiceDocument> void startTaskAsync(final Service service, String factoryLink,
        ServiceDocument startState, final Predicate<T> predicate, final Class<T> type, final int taskPollDelay,
        final FutureCallback<T> callback) {

    Operation.CompletionHandler completionHandler = new Operation.CompletionHandler() {
        @Override/*from ww  w .j a  v  a  2 s.  c o  m*/
        public void handle(Operation operation, Throwable throwable) {
            if (null != throwable) {
                callback.onFailure(throwable);
                return;
            }

            String serviceLink = operation.getBody(ServiceDocument.class).documentSelfLink;
            checkProgress(service, serviceLink, predicate, type, taskPollDelay, callback);
        }
    };

    Operation post = Operation.createPost(UriUtils.buildUri(service.getHost(), factoryLink, null))
            .setBody(startState).setCompletion(completionHandler);
    service.sendRequest(post);
}

From source file:com.vmware.photon.controller.common.dcp.TaskUtils.java

public static <T extends ServiceDocument> void checkProgress(final Service service, final String serviceLink,
        final Predicate<T> predicate, final Class<T> type, final int taskPollDelay,
        final FutureCallback<T> callback) {

    Operation.CompletionHandler completionHandler = new Operation.CompletionHandler() {
        @Override/*  w ww.jav  a 2 s.c  o  m*/
        public void handle(Operation operation, Throwable throwable) {
            if (null != throwable) {
                callback.onFailure(throwable);
                return;
            }
            T state = operation.getBody(type);

            if (predicate.apply(state)) {
                callback.onSuccess(state);
                return;
            }

            Runnable runnable = new Runnable() {
                @Override
                public void run() {
                    try {
                        checkProgress(service, serviceLink, predicate, type, taskPollDelay, callback);
                    } catch (Throwable t) {
                        callback.onFailure(t);
                    }
                }
            };
            service.getHost().schedule(runnable, taskPollDelay, TimeUnit.MILLISECONDS);
        }
    };

    Operation get = Operation.createGet(UriUtils.buildUri(service.getHost(), serviceLink))
            .setCompletion(completionHandler);
    service.sendRequest(get);
}

From source file:de.xaniox.heavyspleef.core.persistence.MoreFutures.java

/**
 * Add a callback to a {@link ListenableFuture}
 * to be run on the bukkit main thread//  www  .  jav a2  s.c o  m
 * 
 * @param plugin The plugin registering the callback
 * @param future The {@link ListenableFuture} to add this callback
 * @param callback The callback to be called
 */
public static <T> void addBukkitSyncCallback(final Plugin plugin, ListenableFuture<T> future,
        final FutureCallback<T> callback) {
    Futures.addCallback(future, new FutureCallback<T>() {
        @Override
        public void onFailure(final Throwable cause) {
            Bukkit.getScheduler().runTask(plugin, new Runnable() {

                @Override
                public void run() {
                    callback.onFailure(cause);
                }
            });
        }

        @Override
        public void onSuccess(final T result) {
            Bukkit.getScheduler().runTask(plugin, new Runnable() {

                @Override
                public void run() {
                    callback.onSuccess(result);
                }
            });
        }
    });
}

From source file:com.vmware.photon.controller.deployer.dcp.util.MiscUtils.java

public static void waitForTaskToFinish(Service service, @Nullable Task task, final Integer taskPollDelay,
        final FutureCallback<Task> callback) {
    if (null == task) {
        callback.onFailure(new IllegalStateException("task is null"));
        return;//from w  w w . j ava  2s  .  c om
    }

    try {
        processTask(service, taskPollDelay, task, callback);
    } catch (Throwable t) {
        callback.onFailure(t);
    }
}

From source file:io.github.trulyfree.easyaspi.lib.util.Utils.java

/**
 * Generates an OnClickListener from a given FutureCallback.
 *
 * @param callback The callback to execute when the click event occurs.
 * @return listener The OnClickListener.
 *//*from  w w w. j a  v  a2s .c  o  m*/
public static View.OnClickListener generateOnClickListener(final FutureCallback<View> callback) {
    return new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (view instanceof Button) {
                System.out.println(((Button) view).getText() + " pressed");
            }
            try {
                callback.onSuccess(view);
            } catch (Throwable throwable) {
                throwable.printStackTrace();
                callback.onFailure(throwable);
            }
        }
    };
}

From source file:com.vmware.photon.controller.deployer.dcp.util.MiscUtils.java

private static void deleteVm(Service service, final ApiClient client, final String vmId,
        final FutureCallback<Task> callback) {
    ServiceUtils.logInfo(service, "Delete vms..");
    try {//from   www .  j  av  a  2  s. co m
        client.getVmApi().deleteAsync(vmId, callback);
    } catch (Throwable t) {
        callback.onFailure(t);
    }
}

From source file:com.vmware.photon.controller.common.xenon.TaskUtils.java

public static <T extends ServiceDocument> void startTaskAsync(final Service service, String factoryLink,
        ServiceDocument startState, final Predicate<T> predicate, final Class<T> type, final int taskPollDelay,
        final FutureCallback<T> callback) {

    Operation.CompletionHandler completionHandler = new Operation.CompletionHandler() {
        @Override//from w ww .  ja  va2 s  .co  m
        public void handle(Operation operation, Throwable throwable) {
            if (null != throwable) {
                ServiceUtils.logSevere(service,
                        "error when contacting [" + factoryLink + "] " + throwable.getMessage());
                ServiceUtils.logSevere(service, throwable);
                callback.onFailure(throwable);
                return;
            }

            String serviceLink = operation.getBody(ServiceDocument.class).documentSelfLink;
            checkProgress(service, serviceLink, predicate, type, taskPollDelay, callback);
        }
    };

    Operation post = Operation.createPost(UriUtils.buildUri(service.getHost(), factoryLink, null))
            .setBody(startState).setCompletion(completionHandler);
    service.sendRequest(post);
}

From source file:io.airlift.discovery.client.CachingServiceSelector.java

private static <V> ListenableFuture<V> chainedCallback(ListenableFuture<V> future,
        final FutureCallback<? super V> callback, Executor executor) {
    final SettableFuture<V> done = SettableFuture.create();
    Futures.addCallback(future, new FutureCallback<V>() {
        @Override//from   w w  w  .  j av a  2s.co m
        public void onSuccess(V result) {
            try {
                callback.onSuccess(result);
            } finally {
                done.set(result);
            }
        }

        @Override
        public void onFailure(Throwable t) {
            try {
                callback.onFailure(t);
            } finally {
                done.setException(t);
            }
        }
    }, executor);
    return done;
}

From source file:com.vmware.photon.controller.clustermanager.utils.ApiUtils.java

/**
 * This method polls the tasks status asynchronously until all the tasks completes or fails.
 *
 * @param tasks             Supplies a list of task objects.
 * @param client            Supplies the API client object.
 * @param service           Supplies the Xenon micro-service which is waiting on the task completion.
 * @param queryTaskInterval Supplies the time interval between the task status query.
 * @param callback          Supplies the callback to be invoked when the task completes or fails.
 *//*from   w ww  .  jav a  2  s. c o m*/
public static void pollTasksAsync(final List<Task> tasks, final ApiClient client, final Service service,
        final int queryTaskInterval, final FutureCallback<List<Task>> callback) {

    final AtomicInteger latch = new AtomicInteger(tasks.size());
    final List<Task> finalTasks = new ArrayList<>();
    final List<Throwable> failures = new ArrayList<>();

    FutureCallback<Task> internalCallback = new FutureCallback<Task>() {
        @Override
        public void onSuccess(Task result) {
            synchronized (finalTasks) {
                finalTasks.add(result);
            }

            if (0 == latch.decrementAndGet()) {
                if (!failures.isEmpty()) {
                    callback.onFailure(ExceptionUtils.createMultiException(failures));
                } else {
                    callback.onSuccess(finalTasks);
                }
            }
        }

        @Override
        public void onFailure(Throwable throwable) {
            synchronized (failures) {
                failures.add(throwable);
            }

            if (0 == latch.decrementAndGet()) {
                callback.onFailure(ExceptionUtils.createMultiException(failures));
            }
        }
    };

    for (Task task : tasks) {
        pollTaskAsync(task, client, service, queryTaskInterval, internalCallback);
    }
}