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:com.vmware.photon.controller.api.client.resource.ProjectApi.java

/**
 * Get a list of vms in the specified project.
 *
 * @param projectId/*from  w ww.jav a2 s .  c o  m*/
 * @param responseCallback
 * @throws IOException
 */
public void getVmsInProjectAsync(final String projectId,
        final FutureCallback<ResourceList<FlavoredCompact>> responseCallback) throws IOException {
    final String path = String.format("%s/%s/vms", getBasePath(), projectId);

    ResourceList<FlavoredCompact> flavoredCompactResourceList = new ResourceList<>();
    FutureCallback<ResourceList<FlavoredCompact>> callback = new FutureCallback<ResourceList<FlavoredCompact>>() {
        @Override
        public void onSuccess(@Nullable ResourceList<FlavoredCompact> result) {
            if (flavoredCompactResourceList.getItems() == null) {
                flavoredCompactResourceList.setItems(result.getItems());
            } else {
                flavoredCompactResourceList.getItems().addAll(result.getItems());
            }
            if (result.getNextPageLink() != null && !result.getNextPageLink().isEmpty()) {
                try {
                    getObjectByPathAsync(result.getNextPageLink(), this,
                            new TypeReference<ResourceList<FlavoredCompact>>() {
                            });
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } else {
                responseCallback.onSuccess(flavoredCompactResourceList);
            }
        }

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

    getObjectByPathAsync(path, callback, new TypeReference<ResourceList<FlavoredCompact>>() {
    });
}

From source file:com.vmware.photon.controller.api.client.resource.ProjectRestApi.java

/**
 * Get the list of persistent disks in the specified project.
 *
 * @param projectId/*from   w w w .  j  ava2 s.  com*/
 * @param responseCallback
 * @throws IOException
 */
@Override
public void getDisksInProjectAsync(final String projectId,
        final FutureCallback<ResourceList<PersistentDisk>> responseCallback) throws IOException {
    final String path = String.format("%s/%s/disks", getBasePath(), projectId);

    ResourceList<PersistentDisk> persistentDiskResourceList = new ResourceList<>();
    FutureCallback<ResourceList<PersistentDisk>> callback = new FutureCallback<ResourceList<PersistentDisk>>() {
        @Override
        public void onSuccess(@Nullable ResourceList<PersistentDisk> result) {
            if (persistentDiskResourceList.getItems() == null) {
                persistentDiskResourceList.setItems(result.getItems());
            } else {
                persistentDiskResourceList.getItems().addAll(result.getItems());
            }
            if (result.getNextPageLink() != null && !result.getNextPageLink().isEmpty()) {
                try {
                    getObjectByPathAsync(result.getNextPageLink(), this,
                            new TypeReference<ResourceList<PersistentDisk>>() {
                            });
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } else {
                responseCallback.onSuccess(persistentDiskResourceList);
            }
        }

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

    getObjectByPathAsync(path, callback, new TypeReference<ResourceList<PersistentDisk>>() {
    });
}

From source file:com.vmware.photon.controller.api.client.resource.ProjectRestApi.java

/**
 * Get a list of vms in the specified project.
 *
 * @param projectId/*ww w.  j  av a 2 s.co m*/
 * @param responseCallback
 * @throws IOException
 */
@Override
public void getVmsInProjectAsync(final String projectId,
        final FutureCallback<ResourceList<FlavoredCompact>> responseCallback) throws IOException {
    final String path = String.format("%s/%s/vms", getBasePath(), projectId);

    ResourceList<FlavoredCompact> flavoredCompactResourceList = new ResourceList<>();
    FutureCallback<ResourceList<FlavoredCompact>> callback = new FutureCallback<ResourceList<FlavoredCompact>>() {
        @Override
        public void onSuccess(@Nullable ResourceList<FlavoredCompact> result) {
            if (flavoredCompactResourceList.getItems() == null) {
                flavoredCompactResourceList.setItems(result.getItems());
            } else {
                flavoredCompactResourceList.getItems().addAll(result.getItems());
            }
            if (result.getNextPageLink() != null && !result.getNextPageLink().isEmpty()) {
                try {
                    getObjectByPathAsync(result.getNextPageLink(), this,
                            new TypeReference<ResourceList<FlavoredCompact>>() {
                            });
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } else {
                responseCallback.onSuccess(flavoredCompactResourceList);
            }
        }

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

    getObjectByPathAsync(path, callback, new TypeReference<ResourceList<FlavoredCompact>>() {
    });
}

From source file:bio.gcat.gui.BatchTool.java

public <V> void addFuture(ListenableFuture<V> future, FutureCallback<? super V> callback) {
    futures.add(future);//w w  w . j  a v  a 2  s .c o m
    updateStatus();
    Futures.addCallback(future, new FutureCallback<V>() {
        @Override
        public void onSuccess(V result) {
            if (callback != null)
                callback.onSuccess(result);
            futures.remove(future);
            updateStatus();
        }

        @Override
        public void onFailure(Throwable thrown) {
            if (callback != null)
                callback.onFailure(thrown);
            futures.remove(future);
            updateStatus();
            thrown.printStackTrace();
        }
    });
}

From source file:de.matzefratze123.heavyspleef.persistence.handler.ForwardingAsyncReadWriteHandler.java

public <R> ListenableFuture<R> runCallableThreadDynamic(Callable<R> callable, FutureCallback<R> callback) {
    ListenableFuture<R> future = null;

    if (callback == null) {
        callback = new FutureCallback<R>() {

            @Override//www. jav  a 2 s  .  c o m
            public void onSuccess(R result) {
            }

            @Override
            public void onFailure(Throwable t) {
                plugin.getLogger().log(Level.SEVERE, "Unexpected exception in database thread:", t);
            }
        };
    }

    if (isServerThread() || forceAsync) {
        future = executorService.submit(callable);

        if (plugin.isEnabled()) {
            //MoreFutures invokes the Bukkit Scheduler and since tasks cannot be
            //registered while the plugin is disabled we can only add a synchronous
            //callback when the plugin is enabled
            MoreFutures.addBukkitSyncCallback(plugin, future, callback);
        } else {
            //Just add a default callback as the plugin is disabled
            Futures.addCallback(future, callback);
        }
    } else {
        Throwable throwableThrown = null;
        R result = null;

        try {
            result = callable.call();
        } catch (Exception e) {
            throwableThrown = e;
        }

        if (callback != null) {
            if (throwableThrown != null) {
                callback.onFailure(throwableThrown);
            } else {
                callback.onSuccess(result);
            }
        }
    }

    return future;
}

From source file:com.vmware.photon.controller.deployer.dcp.task.CreateIsoTaskService.java

private void applyMustacheParameters(String mustacheDirectory, ContainerService.State containerService,
        String containerType, FutureCallback<ContainerService.State> callback) {
    ServiceConfigurator serviceConfigurator = HostUtils.getServiceConfiguratorFactory(this).create();

    try {/*from   ww w .  j  ava 2  s. c  om*/
        Map<String, Object> dynamicParameters = new HashMap<>();
        if (containerService.dynamicParameters != null) {
            dynamicParameters.putAll(containerService.dynamicParameters);
        }

        // Deserialize the string to get load balancer servers and zookeeper quorum
        Type lbType = new TypeToken<ArrayList<LoadBalancerServer>>() {
        }.getType();
        dynamicParameters.computeIfPresent(BuildRuntimeConfigurationTaskService.ENV_LOADBALANCER_SERVERS,
                (k, v) -> new Gson().fromJson(v.toString(), lbType));
        Type zkType = new TypeToken<ArrayList<ZookeeperServer>>() {
        }.getType();
        dynamicParameters.computeIfPresent(BuildRuntimeConfigurationTaskService.ENV_ZOOKEEPER_QUORUM,
                (k, v) -> new Gson().fromJson(v.toString(), zkType));

        serviceConfigurator.applyDynamicParameters(mustacheDirectory,
                ContainersConfig.ContainerType.valueOf(containerType), dynamicParameters);
        callback.onSuccess(containerService);
    } catch (Throwable t) {
        callback.onFailure(t);
    }
}

From source file:com.vmware.photon.controller.deployer.dcp.workflow.RemoveDeploymentWorkflowService.java

private void deleteDCPEntities(final FutureCallback<Task> callback) {
    deleteDCPEntities(ContainerTemplateService.State.class);
    deleteDCPEntities(ContainerService.State.class);
    deleteDCPEntities(VmService.State.class);

    ServiceUtils.logInfo(this, "Remove from cloud store..");

    sendRequest(HostUtils.getCloudStoreHelper(this)
            .createBroadcastPost(ServiceUriPaths.CORE_LOCAL_QUERY_TASKS, ServiceUriPaths.DEFAULT_NODE_SELECTOR)
            .setBody(QueryTask.create(buildQuerySpecification(DatastoreService.class)).setDirect(true))
            .setCompletion(createCompletionHandlerForDeleteDCPEntities(true)));

    sendRequest(HostUtils.getCloudStoreHelper(this)
            .createBroadcastPost(ServiceUriPaths.CORE_LOCAL_QUERY_TASKS, ServiceUriPaths.DEFAULT_NODE_SELECTOR)
            .setBody(QueryTask.create(buildQuerySpecification(DeploymentService.class)).setDirect(true))
            .setCompletion(createCompletionHandlerForDeleteDCPEntities(true)));

    callback.onSuccess(null);
}

From source file:com.vmware.photon.controller.deployer.xenon.workflow.RemoveDeploymentWorkflowService.java

private void deleteXenonEntities(final FutureCallback<Task> callback) {
    deleteXenonEntities(ContainerTemplateService.State.class);
    deleteXenonEntities(ContainerService.State.class);
    deleteXenonEntities(VmService.State.class);

    ServiceUtils.logInfo(this, "Remove from cloud store..");

    sendRequest(HostUtils.getCloudStoreHelper(this)
            .createBroadcastPost(ServiceUriPaths.CORE_LOCAL_QUERY_TASKS, ServiceUriPaths.DEFAULT_NODE_SELECTOR)
            .setBody(QueryTask.create(buildQuerySpecification(DatastoreService.class)).setDirect(true))
            .setCompletion(createCompletionHandlerForDeleteXenonEntities(true)));

    sendRequest(HostUtils.getCloudStoreHelper(this)
            .createBroadcastPost(ServiceUriPaths.CORE_LOCAL_QUERY_TASKS, ServiceUriPaths.DEFAULT_NODE_SELECTOR)
            .setBody(QueryTask.create(buildQuerySpecification(DeploymentService.class)).setDirect(true))
            .setCompletion(createCompletionHandlerForDeleteXenonEntities(true)));

    callback.onSuccess(null);
}

From source file:com.vmware.photon.controller.deployer.xenon.workflow.DeprovisionHostWorkflowService.java

private void deleteEntities(State currentState, boolean ignoreError) {
    // TODO(giskender): Delete flavors
    final AtomicInteger latch = new AtomicInteger(currentState.vmServiceStates.size());

    final FutureCallback<Task> finishedCallback = new FutureCallback<Task>() {
        @Override//from  ww  w .ja va 2 s. co m
        public void onSuccess(@Nullable Task result) {
            if (latch.decrementAndGet() == 0) {
                //All api-fe vms are deleted
                deleteDeployerXenonEntities(currentState, ignoreError);
            }
        }

        @Override
        public void onFailure(Throwable t) {
            if (!ignoreError) {
                failTask(t);
            } else {
                ServiceUtils.logSevere(DeprovisionHostWorkflowService.this,
                        "Ignoring error while finish Deprovision ", t);
            }
        }
    };

    // Stop and delete vms from API-FE
    ApiClient client = HostUtils.getApiClient(this);
    final FutureCallback<Task> vmCallback = new FutureCallback<Task>() {
        @Override
        public void onSuccess(@Nullable Task result) {
            MiscUtils.waitForTaskToFinish(DeprovisionHostWorkflowService.this, result,
                    currentState.taskPollDelay, finishedCallback);
        }

        @Override
        public void onFailure(Throwable t) {
            if (!ignoreError) {
                failTask(t);
            } else {
                ServiceUtils.logSevere(DeprovisionHostWorkflowService.this, "Ignoring error while Deprovision ",
                        t);
                finishedCallback.onSuccess(null);
            }
        }
    };

    for (final VmService.State vm : currentState.vmServiceStates) {
        MiscUtils.stopAndDeleteVm(DeprovisionHostWorkflowService.this, client, vm.vmId,
                currentState.taskPollDelay, vmCallback);
    }
}

From source file:com.vmware.photon.controller.deployer.dcp.workflow.DeprovisionHostWorkflowService.java

private void deleteEntities(State currentState, boolean ignoreError) {
    // TODO(giskender): Delete flavors

    final AtomicInteger latch = new AtomicInteger(currentState.vmServiceStates.size());

    final FutureCallback<Task> finishedCallback = new FutureCallback<Task>() {
        @Override//  www. ja v a 2s .  c o m
        public void onSuccess(@Nullable Task result) {
            if (latch.decrementAndGet() == 0) {
                //All api-fe vms are deleted
                deleteDeployerDCPEntities(currentState, ignoreError);
            }
        }

        @Override
        public void onFailure(Throwable t) {
            if (!ignoreError) {
                failTask(t);
            } else {
                ServiceUtils.logSevere(DeprovisionHostWorkflowService.this,
                        "Ignoring error while finish Deprovision ", t);
            }
        }
    };

    // Stop and delete vms from API-FE
    ApiClient client = HostUtils.getApiClient(this);
    final FutureCallback<Task> vmCallback = new FutureCallback<Task>() {
        @Override
        public void onSuccess(@Nullable Task result) {
            MiscUtils.waitForTaskToFinish(DeprovisionHostWorkflowService.this, result,
                    currentState.taskPollDelay, finishedCallback);
        }

        @Override
        public void onFailure(Throwable t) {
            if (!ignoreError) {
                failTask(t);
            } else {
                ServiceUtils.logSevere(DeprovisionHostWorkflowService.this, "Ignoring error while Deprovision ",
                        t);
                finishedCallback.onSuccess(null);
            }
        }
    };

    for (final VmService.State vm : currentState.vmServiceStates) {
        MiscUtils.stopAndDeleteVm(DeprovisionHostWorkflowService.this, client, vm.vmId,
                currentState.taskPollDelay, vmCallback);
    }
}