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.ProjectRestApi.java

/**
 * Get the list of persistent disks in the specified project.
 *
 * @param projectId/*from w  ww .j  a v a  2 s .  c  o  m*/
 * @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// w  w  w . ja v a2 s  . com
 * @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:org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.java

/**
 * Execute a task in {@link CpsVmExecutorService} to safely access {@link CpsThreadGroup} internal states.
 *
 * <p>//from   ww w  .ja v  a  2s . c  om
 * If the {@link CpsThreadGroup} deserializatoin fails, {@link FutureCallback#onFailure(Throwable)} will
 * be invoked (on a random thread, since CpsVmThread doesn't exist without a valid program.)
 */
void runInCpsVmThread(final FutureCallback<CpsThreadGroup> callback) {
    if (programPromise == null) {
        throw new IllegalStateException("build storage unloadable, or build already finished");
    }
    // first we need to wait for programPromise to fullfil CpsThreadGroup, then we need to run in its runner, phew!
    Futures.addCallback(programPromise, new FutureCallback<CpsThreadGroup>() {
        final Exception source = new Exception(); // call stack of this object captures who called this. useful during debugging.

        @Override
        public void onSuccess(final CpsThreadGroup g) {
            g.runner.submit(new Runnable() {
                @Override
                public void run() {
                    callback.onSuccess(g);
                }
            });
        }

        /**
         * Program state failed to load.
         */
        @Override
        public void onFailure(Throwable t) {
            callback.onFailure(t);
        }
    });
}

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

public <V> void addFuture(ListenableFuture<V> future, FutureCallback<? super V> callback) {
    futures.add(future);//from w w  w .  j a  v  a2  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:org.apache.qpid.server.model.AbstractConfiguredObject.java

protected static <V> void addFutureCallback(ListenableFuture<V> future, final FutureCallback<V> callback,
        Executor taskExecutor) {//w  ww .  j  a  v  a2s. c o  m
    final Subject subject = Subject.getSubject(AccessController.getContext());

    Futures.addCallback(future, new FutureCallback<V>() {
        @Override
        public void onSuccess(final V result) {
            Subject.doAs(subject, new PrivilegedAction<Void>() {
                @Override
                public Void run() {
                    callback.onSuccess(result);
                    return null;
                }
            });
        }

        @Override
        public void onFailure(final Throwable t) {
            Subject.doAs(subject, new PrivilegedAction<Void>() {
                @Override
                public Void run() {
                    callback.onFailure(t);
                    return null;
                }
            });
        }
    }, taskExecutor);
}

From source file:org.elasticsearch.action.bulk.BulkShardProcessor.java

private void createPendingIndices() {
    final List<PendingRequest> pendings = new ArrayList<>();
    final Set<String> indices;

    synchronized (requestsForNewIndices) {
        indices = ImmutableSet/*from   www  .java2 s .co  m*/
                .copyOf(Iterables.filter(Sets.difference(requestsForNewIndices.keySet(), indicesCreated),
                        shouldAutocreateIndexPredicate));
        for (Map.Entry<String, List<PendingRequest>> entry : requestsForNewIndices.entrySet()) {
            pendings.addAll(entry.getValue());
        }
        requestsForNewIndices.clear();
        pendingNewIndexRequests.set(0);
    }

    if (pendings.size() > 0 || indices.size() > 0) {
        LOGGER.debug("create {} pending indices in bulk...", indices.size());
        BulkCreateIndicesRequest bulkCreateIndicesRequest = new BulkCreateIndicesRequest(indices, jobId);

        final FutureCallback<Void> indicesCreatedCallback = new FutureCallback<Void>() {
            @Override
            public void onSuccess(@Nullable Void result) {
                if (failure.get() != null) {
                    return;
                }
                trace("applying pending requests for created indices...");
                Iterator<PendingRequest> it = pendings.iterator();
                while (it.hasNext()) {
                    PendingRequest pendingRequest = it.next();
                    // add pending requests for created indices
                    ShardId shardId = shardId(pendingRequest.indexName, pendingRequest.item.id(),
                            pendingRequest.routing);
                    if (shardId == null) {
                        // seems like index is deleted meanwhile, mark item as failed and remove pending
                        indicesDeleted.add(pendingRequest.indexName);
                        it.remove();
                        synchronized (responsesLock) {
                            responses.set(globalCounter.getAndIncrement(), false);
                        }
                        setResultIfDone(1);
                        continue;
                    }
                    partitionRequestByShard(shardId, pendingRequest.item, pendingRequest.routing);
                }
                trace("added %d pending requests, lets see if we can execute them", pendings.size());
                executeRequestsIfNeeded();
            }

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

        if (indices.isEmpty()) {
            indicesCreatedCallback.onSuccess(null);
        } else {
            transportBulkCreateIndicesAction.execute(bulkCreateIndicesRequest,
                    new ActionListener<BulkCreateIndicesResponse>() {
                        @Override
                        public void onResponse(BulkCreateIndicesResponse response) {
                            indicesCreated.addAll(indices);
                            indicesCreatedCallback.onSuccess(null);
                        }

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

}