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

/**
 * This method gets all tasks associated with a disk.
 *
 * @param diskId//from  ww  w.  j a v a  2 s.c  om
 * @param responseCallback
 * @throws IOException
 */
public void getTasksForDiskAsync(final String diskId, final FutureCallback<ResourceList<Task>> responseCallback)
        throws IOException {
    String path = String.format("%s/%s/tasks", getBasePath(), diskId);

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

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

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

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

/**
 * Get all deployment vms./*from ww  w  . j  a va2  s. c o m*/
 *
 * @param deploymentId
 * @return
 * @throws IOException
 */
@Override
public void getAllDeploymentVmsAsync(String deploymentId,
        final FutureCallback<ResourceList<Vm>> responseCallback) throws IOException {
    String path = String.format("%s/%s/vms", getBasePath(), deploymentId);
    ResourceList<Vm> vmResourceList = new ResourceList<>();
    FutureCallback<ResourceList<Vm>> callback = new FutureCallback<ResourceList<Vm>>() {
        @Override
        public void onSuccess(@Nullable ResourceList<Vm> result) {
            if (vmResourceList.getItems() == null) {
                vmResourceList.setItems(result.getItems());
            } else {
                vmResourceList.getItems().addAll(result.getItems());
            }
            if (result.getNextPageLink() != null && !result.getNextPageLink().isEmpty()) {
                try {
                    getObjectByPathAsync(result.getNextPageLink(), this, new TypeReference<ResourceList<Vm>>() {
                    });
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } else {
                responseCallback.onSuccess(vmResourceList);
            }
        }

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

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

From source file:org.thingsboard.server.service.telemetry.DefaultTelemetryWebSocketService.java

private <T> FutureCallback<ValidationResult> getAttributesFetchCallback(final TenantId tenantId,
        final EntityId entityId, final FutureCallback<List<AttributeKvEntry>> callback) {
    return new FutureCallback<ValidationResult>() {
        @Override/*from w w w  .  ja  v  a2s.co m*/
        public void onSuccess(@Nullable ValidationResult result) {
            List<ListenableFuture<List<AttributeKvEntry>>> futures = new ArrayList<>();
            for (String scope : DataConstants.allScopes()) {
                futures.add(attributesService.findAll(tenantId, entityId, scope));
            }

            ListenableFuture<List<AttributeKvEntry>> future = mergeAllAttributesFutures(futures);
            Futures.addCallback(future, callback);
        }

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

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

/**
 * This method gets all tasks associated with a disk.
 *
 * @param diskId/*  w  w w  .  j a  v  a 2  s .  co  m*/
 * @param responseCallback
 * @throws IOException
 */
@Override
public void getTasksForDiskAsync(final String diskId, final FutureCallback<ResourceList<Task>> responseCallback)
        throws IOException {
    String path = String.format("%s/%s/tasks", getBasePath(), diskId);

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

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

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

From source file:org.thingsboard.server.service.telemetry.DefaultTelemetryWebSocketService.java

private <T> FutureCallback<ValidationResult> getAttributesFetchCallback(final TenantId tenantId,
        final EntityId entityId, final List<String> keys,
        final FutureCallback<List<AttributeKvEntry>> callback) {
    return new FutureCallback<ValidationResult>() {
        @Override/*from   w  w w  . j  a  v  a  2 s  .c o m*/
        public void onSuccess(@Nullable ValidationResult result) {
            List<ListenableFuture<List<AttributeKvEntry>>> futures = new ArrayList<>();
            for (String scope : DataConstants.allScopes()) {
                futures.add(attributesService.find(tenantId, entityId, scope, keys));
            }

            ListenableFuture<List<AttributeKvEntry>> future = mergeAllAttributesFutures(futures);
            Futures.addCallback(future, callback);
        }

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

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

/**
 * Lists all deployments.//from ww  w . j  a  v  a  2  s.  c  o m
 *
 * @param responseCallback
 * @throws IOException
 */
public void listAllAsync(final FutureCallback<ResourceList<Deployment>> responseCallback) throws IOException {
    ResourceList<Deployment> deploymentResourceList = new ResourceList<>();
    FutureCallback<ResourceList<Deployment>> callback = new FutureCallback<ResourceList<Deployment>>() {
        @Override
        public void onSuccess(@Nullable ResourceList<Deployment> result) {
            if (deploymentResourceList.getItems() == null) {
                deploymentResourceList.setItems(result.getItems());
            } else {
                deploymentResourceList.getItems().addAll(result.getItems());
            }
            if (result.getNextPageLink() != null && !result.getNextPageLink().isEmpty()) {
                try {
                    getObjectByPathAsync(result.getNextPageLink(), this,
                            new TypeReference<ResourceList<Deployment>>() {
                            });
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } else {
                responseCallback.onSuccess(deploymentResourceList);
            }
        }

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

    getObjectByPathAsync(getBasePath(), callback, new TypeReference<ResourceList<Deployment>>() {
    });
}

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

public void run(final Service service, final NodeRolloutInput input,
        final FutureCallback<NodeRolloutResult> responseFutureCallback) {

    Preconditions.checkNotNull(service, "service cannot be null");
    Preconditions.checkNotNull(input, "input cannot be null");
    Preconditions.checkNotNull(responseFutureCallback, "responseFutureCallback cannot be null");
    Preconditions.checkNotNull(input.serverAddress, "serverAddress cannot be null");
    input.validate();//from w  w w.  j  ava 2s . c om

    final Queue<Throwable> exceptions = new ConcurrentLinkedQueue<>();
    final Queue<String> nodeAddresses = new ConcurrentLinkedQueue<>();
    final AtomicInteger latch = new AtomicInteger(input.nodeCount);

    for (int i = 0; i < input.nodeCount; i++) {

        provisionNode(service, input, i, // nodeIndex
                new FutureCallback<String>() {
                    @Override
                    public void onSuccess(String nodeAddress) {
                        nodeAddresses.add(nodeAddress);

                        if (0 == latch.decrementAndGet()) {
                            if (0 == exceptions.size()) {
                                waitForNodes(service, input, new ArrayList<>(nodeAddresses),
                                        responseFutureCallback);
                            } else {
                                responseFutureCallback
                                        .onFailure(ExceptionUtils.createMultiException(exceptions));
                            }
                        }
                    }

                    @Override
                    public void onFailure(Throwable t) {
                        exceptions.add(t);
                        if (0 == latch.decrementAndGet()) {
                            responseFutureCallback.onFailure(ExceptionUtils.createMultiException(exceptions));
                        }
                    }
                });
    }
}

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

/**
 * Lists all deployments.//w  w  w .  j a v  a  2 s. c om
 *
 * @param responseCallback
 * @throws IOException
 */
@Override
public void listAllAsync(final FutureCallback<ResourceList<Deployment>> responseCallback) throws IOException {
    ResourceList<Deployment> deploymentResourceList = new ResourceList<>();
    FutureCallback<ResourceList<Deployment>> callback = new FutureCallback<ResourceList<Deployment>>() {
        @Override
        public void onSuccess(@Nullable ResourceList<Deployment> result) {
            if (deploymentResourceList.getItems() == null) {
                deploymentResourceList.setItems(result.getItems());
            } else {
                deploymentResourceList.getItems().addAll(result.getItems());
            }
            if (result.getNextPageLink() != null && !result.getNextPageLink().isEmpty()) {
                try {
                    getObjectByPathAsync(result.getNextPageLink(), this,
                            new TypeReference<ResourceList<Deployment>>() {
                            });
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } else {
                responseCallback.onSuccess(deploymentResourceList);
            }
        }

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

    getObjectByPathAsync(getBasePath(), callback, new TypeReference<ResourceList<Deployment>>() {
    });
}

From source file:com.vmware.photon.controller.clustermanager.tasks.GarbageCollectionTaskService.java

/**
 * Call VmDeprovisionTask to delete a VM.
 *//*from   w w  w  . j a  v a  2  s  .c o  m*/
private void deleteVm(String vmId, FutureCallback<Void> callback) {
    try {
        VmDeprovisionTaskService.State vmDeprovisionTask = new VmDeprovisionTaskService.State();
        vmDeprovisionTask.vmId = vmId;

        TaskUtils.startTaskAsync(this, VmDeprovisionTaskFactoryService.SELF_LINK, vmDeprovisionTask,
                state -> TaskUtils.finalTaskStages.contains(state.taskState.stage),
                VmDeprovisionTaskService.State.class, ClusterManagerConstants.DEFAULT_TASK_POLL_DELAY,
                new FutureCallback<VmDeprovisionTaskService.State>() {
                    @Override
                    public void onSuccess(@Nullable VmDeprovisionTaskService.State state) {
                        if (state.taskState.stage != TaskState.TaskStage.FINISHED) {
                            String exceptionMessage = String
                                    .format("VmDeprovisionTask did not finish for vm %s.", vmId);
                            if (null != state.taskState.failure) {
                                exceptionMessage += String.format(" Failure: %s",
                                        state.taskState.failure.message);
                            }
                            callback.onFailure(new RuntimeException(exceptionMessage));
                        } else {
                            deleteInactiveVmEntity(vmId, callback);
                        }
                    }

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

        VmDeprovisionTaskService.State startState = new VmDeprovisionTaskService.State();
        startState.vmId = vmId;
    } catch (Throwable t) {
        callback.onFailure(t);
    }
}

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

/**
 * Returns a list of all projects defined for the specified tenant.
 * @param tenantId/*from w w  w  .j ava  2  s  .c o m*/
 * @param responseCallback
 * @throws IOException
 */
public void getProjectsAsync(final String tenantId,
        final FutureCallback<ResourceList<Project>> responseCallback) throws IOException {
    String path = String.format("%s/%s/projects", getBasePath(), tenantId);

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

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

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