Example usage for com.google.common.util.concurrent Futures immediateFailedFuture

List of usage examples for com.google.common.util.concurrent Futures immediateFailedFuture

Introduction

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

Prototype

@CheckReturnValue
public static <V> ListenableFuture<V> immediateFailedFuture(Throwable throwable) 

Source Link

Document

Returns a ListenableFuture which has an exception set immediately upon construction.

Usage

From source file:org.opendaylight.mdsal.dom.store.inmemory.InMemoryDOMDataTreeShardThreePhaseCommitCohort.java

@SuppressWarnings("checkstyle:IllegalCatch")
@Override//from   w  w w  . ja  v a 2  s .c  o m
public ListenableFuture<Void> preCommit() {
    try {
        candidate = dataTree.prepare(modification);
        LOG.debug("DataTreeModification {} prepared", modification);
        return SUCCESSFUL_FUTURE;
    } catch (Exception e) {
        LOG.warn("Unexpected failure in preparation phase", e);
        return Futures.immediateFailedFuture(e);
    }
}

From source file:com.proofpoint.event.monitor.test.SerialScheduledExecutorService.java

@Override
public <T> Future<T> submit(Callable<T> tCallable) {
    try {// w  w w.j  a va2 s . c  om
        return Futures.immediateFuture(tCallable.call());
    } catch (Exception e) {
        return Futures.immediateFailedFuture(e);
    }
}

From source file:org.opendaylight.mdsal.dom.store.inmemory.InMemoryDOMStoreThreePhaseCommitCohort.java

@Override
public final ListenableFuture<Boolean> canCommit() {
    try {/*w ww  .  j a v  a2s  .  co  m*/
        store.validate(modification);
        LOG.debug("Store Transaction: {} can be committed", getTransaction().getIdentifier());
        return CAN_COMMIT_FUTURE;
    } catch (ConflictingModificationAppliedException e) {
        LOG.warn("Store Tx: {} Conflicting modification for {}.", getTransaction().getIdentifier(),
                e.getPath());
        warnDebugContext(getTransaction());
        return Futures.immediateFailedFuture(new OptimisticLockFailedException("Optimistic lock failed.", e));
    } catch (DataValidationFailedException e) {
        LOG.warn("Store Tx: {} Data Precondition failed for {}.", getTransaction().getIdentifier(), e.getPath(),
                e);
        warnDebugContext(getTransaction());

        // For debugging purposes, allow dumping of the modification. Coupled with the above
        // precondition log, it should allow us to understand what went on.
        LOG.trace("Store Tx: {} modifications: {} tree: {}", modification, store);

        return Futures.immediateFailedFuture(
                new TransactionCommitFailedException("Data did not pass validation.", e));
    } catch (Exception e) {
        LOG.warn("Unexpected failure in validation phase", e);
        return Futures.immediateFailedFuture(e);
    }
}

From source file:com.orangerhymelabs.helenus.cassandra.table.ViewService.java

public ListenableFuture<List<View>> readAll(String database, String table, Object... parms) {
    ListenableFuture<Boolean> tableFuture = tables.exists(database, table);
    return Futures.transformAsync(tableFuture, new AsyncFunction<Boolean, List<View>>() {
        @Override/*from  w  w  w. ja v  a 2  s.c  om*/
        public ListenableFuture<List<View>> apply(Boolean exists) throws Exception {
            if (exists) {
                return views.readAll(parms);
            } else {
                return Futures.immediateFailedFuture(new ItemNotFoundException("Table not found: " + table));
            }
        }
    });
}

From source file:io.radiowitness.kinesis.producer.KinesisRecordProducer.java

public ListenableFuture<String> put(BaseMessage message) throws MessagePackingException {
    synchronized (txnLock) {
        ListenableFuture<String> putFuture;

        if (!scheduledTask.isPresent()) {
            scheduledTask = Optional.of(nextTask());
            putFuture = scheduledTask.get().getFuture();
            timer.schedule(scheduledTask.get(), messagePutDelayMsMax);
            scheduledTask.get().packOrRun(message);
        } else if (scheduledTask.get().packOrRun(message)) {
            putFuture = scheduledTask.get().getFuture();
        } else if (queue.offer(new QueuedMessage(message))) {
            putFuture = queuedFuture;/* ww  w  . j av  a  2 s  . com*/
        } else {
            putFuture = Futures.immediateFailedFuture(new QueueFullException("message queue is full"));
        }

        return putFuture;
    }
}

From source file:com.facebook.buck.core.build.engine.cache.manager.ManifestRuleKeyServiceImpl.java

@Override
public ListenableFuture<Void> storeManifest(RuleKey manifestKey, Path manifestToUpload,
        long artifactBuildTimeMs) {
    String key = toManifestServiceKey(manifestKey);
    Manifest manifest = new Manifest();
    manifest.setKey(key);//from  ww w . j  a  v a  2s  .c o  m
    try {
        manifest.addToValues(ByteBuffer.wrap(Files.readAllBytes(manifestToUpload)));
    } catch (IOException e) {
        LOG.error(e, "Failed to store key [%s] and path [%s] into the ManifestService.", key, manifestToUpload);
        return Futures.immediateFailedFuture(e);
    }

    return manifestService.setManifest(manifest);
}

From source file:io.v.x.jni.test.fortune.FortuneServerImpl.java

@Override
public ListenableFuture<Map<String, String>> parameterizedGet(VContext context, ServerCall call) {
    if (lastAddedFortune == null) {
        return Futures.immediateFailedFuture(new NoFortunesException(context));
    }/*from   w w w  .  j a  va  2s. c o  m*/
    return Futures.immediateFuture((Map<String, String>) ImmutableMap.of(lastAddedFortune, lastAddedFortune));
}

From source file:com.spotify.futures.ConcurrencyLimiter.java

/**
 * the callable function will run as soon as the currently active set of
 * futures is less than the maxConcurrency limit.
 *
 * @param callable - a function that creates a future.
 * @returns a proxy future that completes with the future created by the
 *          input function.//from   w  ww .  j a v a 2 s . co  m
 *          This future will be immediately failed with
 *          {@link CapacityReachedException} if the soft queue size limit is exceeded.
 * @throws {@link NullPointerException} if callable is null
 */
public ListenableFuture<T> add(Callable<? extends ListenableFuture<T>> callable) {
    Preconditions.checkNotNull(callable);
    final SettableFuture<T> response = SettableFuture.create();
    final Job<T> job = new Job<T>(callable, response);
    if (queueSize.get() >= maxQueueSize) {
        final String message = "Queue size has reached capacity: " + maxQueueSize;
        return Futures.immediateFailedFuture(new CapacityReachedException(message));
    }
    queue.add(job);
    queueSize.incrementAndGet();
    pump();
    return response;
}

From source file:com.facebook.buck.distributed.RemoteExecutionStorageService.java

@Override
public ListenableFuture<ByteBuffer> fetch(Digest digest) {
    try {//from w w  w . j a v a 2s.c o m
        try (ByteArrayOutputStream outStream = new ByteArrayOutputStream()) {
            return Futures.transform(fetchToStreamInternal(digest, outStream),
                    future -> ByteBuffer.wrap(outStream.toByteArray()), MoreExecutors.directExecutor());
        }
    } catch (IOException e) {
        return Futures.immediateFailedFuture(e);
    }
}

From source file:com.facebook.buck.util.concurrent.JobLimiter.java

private <V> ListenableFuture<V> send(ThrowingSupplier<ListenableFuture<V>, Exception> callable) {
    ListenableFuture<V> future;/*from  w w w.j  a v  a2 s .c om*/
    try {
        future = callable.get();
    } catch (Throwable e) {
        future = Futures.immediateFailedFuture(e);
    }
    Futures.addCallback(future, MoreFutures.finallyCallback(this::release));
    return future;
}