Example usage for com.google.common.util.concurrent SettableFuture setException

List of usage examples for com.google.common.util.concurrent SettableFuture setException

Introduction

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

Prototype

@Override
    public boolean setException(Throwable throwable) 

Source Link

Usage

From source file:com.microsoft.windowsazure.mobileservices.table.sync.MobileServiceSyncTable.java

/**
 * Retrieve results from the local table.
 *
 * @param query an optional query to filter results
 * @return A ListenableFuture that is done when the results have been
 * retrieved./*from ww w . j av  a2  s  .com*/
 */
public ListenableFuture<MobileServiceList<E>> read(Query query) {
    final SettableFuture<MobileServiceList<E>> future = SettableFuture.create();

    ListenableFuture<JsonElement> internalFuture = mInternalTable.read(query);

    Futures.addCallback(internalFuture, new FutureCallback<JsonElement>() {
        @Override
        public void onFailure(Throwable throwable) {
            future.setException(throwable);
        }

        @Override
        public void onSuccess(JsonElement result) {
            try {
                if (result.isJsonObject()) {
                    JsonObject jsonObject = result.getAsJsonObject();

                    int count = jsonObject.get("count").getAsInt();
                    JsonElement elements = jsonObject.get("results");

                    List<E> list = parseResults(elements);
                    future.set(new MobileServiceList<E>(list, count));
                } else {
                    List<E> list = parseResults(result);
                    future.set(new MobileServiceList<E>(list, list.size()));
                }
            } catch (Exception e) {
                future.setException(e);
            }
        }
    });

    return future;
}

From source file:com.twitter.heron.statemgr.zookeeper.curator.CuratorStateManager.java

protected ListenableFuture<Boolean> deleteNode(String path) {
    final SettableFuture<Boolean> result = SettableFuture.create();

    try {//from   w  w  w.j a  va2  s  . c om
        client.delete().withVersion(-1).forPath(path);
        LOG.info("Deleted node for path: " + path);
        result.set(true);

        // Suppress it since forPath() throws Exception
        // SUPPRESS CHECKSTYLE IllegalCatch
    } catch (Exception e) {
        result.setException(new RuntimeException("Could not deleteNode", e));
    }

    return result;
}

From source file:org.opendaylight.openflowjava.protocol.impl.core.UdpHandler.java

@Override
public ListenableFuture<Boolean> shutdown() {
    final SettableFuture<Boolean> result = SettableFuture.create();
    group.shutdownGracefully()/*w  ww  .j av a  2s.  c  om*/
            .addListener(new GenericFutureListener<io.netty.util.concurrent.Future<Object>>() {

                @Override
                public void operationComplete(final io.netty.util.concurrent.Future<Object> downResult)
                        throws Exception {
                    result.set(downResult.isSuccess());
                    if (downResult.cause() != null) {
                        result.setException(downResult.cause());
                    }
                }

            });
    return result;
}

From source file:zipkin.storage.cassandra.DeduplicatingExecutor.java

/**
 * @param session which conditionally executes bound statements
 * @param ttl how long the results of statements are remembered, in milliseconds.
 *///w w w  .j av  a  2  s . c o m
DeduplicatingExecutor(Session session, long ttl) {
    this.session = session;
    this.cache = CacheBuilder.newBuilder().expireAfterWrite(ttl, TimeUnit.MILLISECONDS).ticker(new Ticker() {
        @Override
        public long read() {
            return nanoTime();
        }
    })
            // TODO: maximum size or weight
            .build(new CacheLoader<BoundStatementKey, ListenableFuture<Void>>() {
                @Override
                public ListenableFuture<Void> load(final BoundStatementKey key) {
                    ListenableFuture<?> cassandraFuture = executeAsync(key.statement);

                    // Drop the cassandra future so that we don't hold references to cassandra state for
                    // long periods of time.
                    final SettableFuture<Void> disconnectedFuture = SettableFuture.create();
                    Futures.addCallback(cassandraFuture, new FutureCallback<Object>() {

                        @Override
                        public void onSuccess(Object result) {
                            disconnectedFuture.set(null);
                        }

                        @Override
                        public void onFailure(Throwable t) {
                            cache.invalidate(key);
                            disconnectedFuture.setException(t);
                        }
                    });
                    return disconnectedFuture;
                }
            });
}

From source file:io.crate.blob.v2.BlobAdminClient.java

public ListenableFuture<Void> createBlobTable(String tableName, Settings indexSettings) {
    Settings.Builder builder = Settings.builder();
    builder.put(indexSettings);//  w w w  .  j  av  a  2 s . com
    builder.put(SETTING_INDEX_BLOBS_ENABLED, true);

    final SettableFuture<Void> result = SettableFuture.create();
    createIndexAction.execute(new CreateIndexRequest(fullIndexName(tableName), builder.build()),
            new ActionListener<CreateIndexResponse>() {
                @Override
                public void onResponse(CreateIndexResponse createIndexResponse) {
                    assert createIndexResponse.isAcknowledged() : "createIndexResponse must be acknowledged";
                    result.set(null);
                }

                @Override
                public void onFailure(Throwable e) {
                    result.setException(e);
                }
            });
    return result;
}

From source file:org.eclipse.neoscada.protocol.iec60870.client.Client.java

protected synchronized void handleOperationComplete(final SettableFuture<Void> result,
        final ChannelFuture future) {
    if (this.connectFuture != result) {
        // this should never happen
        return;/*  w  w w  .  j a v  a2s .  c  o  m*/
    }

    this.connectFuture = null;

    try {
        future.get();
        this.channel = future.channel();

        fireConnected(this.channel);
        result.set(null);
    } catch (final InterruptedException | ExecutionException e) {
        fireDisconnected(e);
        result.setException(e);
    }
}

From source file:c5db.log.LogService.java

@Override
public ListenableFuture<ReplicatorLog> getReplicatorLog(String quorumId) {
    SettableFuture<ReplicatorLog> logFuture = SettableFuture.create();

    fiber.execute(() -> {// w  w w  .  ja  v a2  s  . c om
        if (moorings.containsKey(quorumId)) {
            logFuture.set(moorings.get(quorumId));
            return;
        }

        try {
            // TODO this blocks on a fiber, and should be changed to use a callback.
            Mooring mooring = new Mooring(oLog, quorumId);
            moorings.put(quorumId, mooring);
            logFuture.set(mooring);

        } catch (IOException e) {
            logFuture.setException(e);
        }
    });

    return logFuture;
}

From source file:io.crate.executor.transport.distributed.DownstreamOperationContext.java

public DownstreamOperationContext(DownstreamOperation downstreamOperation,
        final SettableFuture<Object[][]> listener, Streamer<?>[] streamers,
        DistributedRequestContextManager.DoneCallback doneCallback) {
    this.mergeOperationsLeft = new AtomicInteger(downstreamOperation.numUpstreams());
    this.downstreamOperation = downstreamOperation;
    this.listener = listener;
    Futures.addCallback(downstreamOperation.result(), new FutureCallback<Object[][]>() {
        @Override//  w  ww . j a  v  a2 s  . c o m
        public void onSuccess(@Nullable Object[][] result) {
            listener.set(result);
        }

        @Override
        public void onFailure(Throwable t) {
            listener.setException(t);

        }
    });
    this.streamers = streamers;
    this.doneCallback = doneCallback;
}

From source file:com.continuuity.weave.internal.kafka.client.SimpleKafkaClient.java

private <V> ChannelFutureListener getPublishChannelFutureListener(final SettableFuture<V> result,
        final V resultObj, final ConnectionPool.ConnectionReleaser releaser) {
    return new ChannelFutureListener() {
        @Override/*w ww  .j  av a  2s  . c  o  m*/
        public void operationComplete(ChannelFuture future) throws Exception {
            try {
                if (future.isSuccess()) {
                    result.set(resultObj);
                } else if (future.isCancelled()) {
                    result.cancel(true);
                } else {
                    result.setException(future.getCause());
                }
            } finally {
                releaser.release();
            }
        }
    };
}

From source file:org.apache.bookkeeper.stream.segment.BKSegmentWriter.java

private void errorOutEntriesIfNecessary(Throwable closeException) {
    if (!pendingEntries.isEmpty()) {
        return;//from   w ww. j  a v a2  s  .c  o  m
    }
    // drain error queue
    WriteCancelledException wce = new WriteCancelledException("Writing record cancelled because segment "
            + segmentName + "@" + streamName + " is : state = " + state + ", in error state = " + inErrorState);
    SettableFuture<SSN> future;
    while ((future = errorQueue.poll()) != null) {
        future.setException(wce);
    }
    // drain close futures
    while ((future = closeFutures.poll()) != null) {
        if (null == closeException) {
            future.set(lastFlushedSSN);
        } else {
            future.setException(closeException);
        }
    }
}