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

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

Introduction

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

Prototype

public static <V> SettableFuture<V> create() 

Source Link

Document

Creates a new SettableFuture that can be completed or cancelled by a later method call.

Usage

From source file:org.apache.twill.internal.AbstractExecutionServiceController.java

protected AbstractExecutionServiceController(RunId runId) {
    this.runId = runId;
    this.listenerExecutors = new ListenerExecutors();
    this.serviceDelegate = new ServiceDelegate();
    this.terminationFuture = SettableFuture.create();
    addListener(new ServiceListenerAdapter() {
        @Override/*w  ww.ja  va 2  s  .  c o  m*/
        public void failed(State from, Throwable failure) {
            terminationFuture.setException(failure);
        }

        @Override
        public void terminated(State from) {
            terminationFuture.set(State.TERMINATED);
        }
    }, Threads.SAME_THREAD_EXECUTOR);
}

From source file:com.facebook.presto.memory.MemoryPool.java

/**
 * Reserves the given number of bytes. Caller should wait on the returned future, before allocating more memory.
 *///from  w  w  w .jav  a  2s.com
public synchronized ListenableFuture<?> reserve(QueryId queryId, long bytes) {
    checkArgument(bytes >= 0, "bytes is negative");
    queryMemoryReservations.merge(queryId, bytes, Long::sum);
    freeBytes -= bytes;
    if (freeBytes <= 0) {
        if (future == null) {
            future = SettableFuture.create();
        }
        checkState(!future.isDone(), "future is already completed");
        return future;
    }
    return NOT_BLOCKED;
}

From source file:com.google.bitcoin.utils.MockTransactionBroadcaster.java

@Override
public SettableFuture<Transaction> broadcastTransaction(Transaction tx) {
    // Use a lock just to catch lock ordering inversions e.g. wallet->broadcaster.
    lock.lock();/*from w  w w.  ja v  a  2  s . com*/
    try {
        SettableFuture<Transaction> result = SettableFuture.create();
        broadcasts.put(new TxFuturePair(tx, result));
        Futures.addCallback(result, new FutureCallback<Transaction>() {
            @Override
            public void onSuccess(Transaction result) {
                try {
                    wallet.receivePending(result, null);
                } catch (VerificationException e) {
                    throw new RuntimeException(e);
                }
            }

            @Override
            public void onFailure(Throwable t) {
            }
        });
        return result;
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    } finally {
        lock.unlock();
    }
}

From source file:org.opendaylight.ocpplugin.impl.services.SalObjectLifecycleServiceImpl.java

@Override
public Future<RpcResult<CreateObjOutput>> createObj(final CreateObjInput input) {
    ListenableFuture<RpcResult<org.opendaylight.yang.gen.v1.urn.opendaylight.ocp.protocol.rev150811.CreateObjOutput>> future = createObj
            .handleServiceCall(input);/*ww  w.  j  av a2  s . c  o  m*/
    final SettableFuture<RpcResult<CreateObjOutput>> finalFuture = SettableFuture.create();
    Futures.addCallback(future,
            new FutureCallback<RpcResult<org.opendaylight.yang.gen.v1.urn.opendaylight.ocp.protocol.rev150811.CreateObjOutput>>() {
                @Override
                public void onSuccess(
                        final RpcResult<org.opendaylight.yang.gen.v1.urn.opendaylight.ocp.protocol.rev150811.CreateObjOutput> result) {
                    org.opendaylight.yang.gen.v1.urn.opendaylight.ocp.protocol.rev150811.CreateObjOutput output = result
                            .getResult();
                    CreateObjOutputBuilder builder = new CreateObjOutputBuilder();
                    builder.setObjId(output.getObjId());
                    builder.setParam(output.getParam());
                    builder.setGlobResult(output.getGlobResult());
                    RpcResultBuilder<CreateObjOutput> rpcResultBuilder = RpcResultBuilder.success(builder);
                    finalFuture.set(rpcResultBuilder.build());
                }

                @Override
                public void onFailure(final Throwable t) {
                    RpcResultBuilder<CreateObjOutput> rpcResultBuilder = RpcResultBuilder.failed();
                    finalFuture.set(rpcResultBuilder.build());
                }
            });

    return finalFuture;
}

From source file:org.loadui.testfx.utils.FXTestUtils.java

/**
 * Runs the given Callable in the JavaFX thread, waiting for it to complete before returning.
 * Also attempts to wait for any other JavaFX events that may have been queued in the Callable
 * to complete. If any Exception is thrown during execution of the Callable, that exception
 * will be re-thrown from invokeAndWait.
 *
 * @param task/*from  ww w. j av  a2 s .  c  om*/
 * @param timeoutInSeconds
 * @throws Exception
 */
public static void invokeAndWait(final Callable<?> task, int timeoutInSeconds) throws Exception {
    final SettableFuture<Void> future = SettableFuture.create();

    Platform.runLater(new Runnable() {
        @Override
        public void run() {
            try {
                task.call();
                future.set(null);
            } catch (Throwable e) {
                future.setException(e);
            }
        }
    });

    try {
        future.get(timeoutInSeconds, TimeUnit.SECONDS);
        awaitEvents();
    } catch (ExecutionException exception) {
        if (exception.getCause() instanceof Exception) {
            throw (Exception) exception.getCause();
        } else {
            throw exception;
        }
    }
}

From source file:org.opendaylight.ocpplugin.impl.services.SalObjectStateMgmtServiceImpl.java

@Override
public Future<RpcResult<GetStateOutput>> getState(final GetStateInput input) {
    ListenableFuture<RpcResult<org.opendaylight.yang.gen.v1.urn.opendaylight.ocp.protocol.rev150811.GetStateOutput>> future = getState
            .handleServiceCall(input);/*w  ww  .  j  a v  a 2  s.c o  m*/
    final SettableFuture<RpcResult<GetStateOutput>> finalFuture = SettableFuture.create();
    Futures.addCallback(future,
            new FutureCallback<RpcResult<org.opendaylight.yang.gen.v1.urn.opendaylight.ocp.protocol.rev150811.GetStateOutput>>() {
                @Override
                public void onSuccess(
                        final RpcResult<org.opendaylight.yang.gen.v1.urn.opendaylight.ocp.protocol.rev150811.GetStateOutput> result) {
                    org.opendaylight.yang.gen.v1.urn.opendaylight.ocp.protocol.rev150811.GetStateOutput output = result
                            .getResult();
                    GetStateOutputBuilder builder = new GetStateOutputBuilder();
                    builder.setObj(output.getObj());
                    builder.setResult(output.getResult());
                    RpcResultBuilder<GetStateOutput> rpcResultBuilder = RpcResultBuilder.success(builder);
                    finalFuture.set(rpcResultBuilder.build());
                }

                @Override
                public void onFailure(final Throwable t) {
                    RpcResultBuilder<GetStateOutput> rpcResultBuilder = RpcResultBuilder.failed();
                    finalFuture.set(rpcResultBuilder.build());
                }
            });

    return finalFuture;
}

From source file:io.crate.executor.transport.task.elasticsearch.ESSearchTask.java

public ESSearchTask(ESSearchNode searchNode, TransportSearchAction transportSearchAction) {
    this.searchNode = searchNode;
    this.transportSearchAction = transportSearchAction;
    this.queryBuilder = new ESQueryBuilder();

    result = SettableFuture.create();
    results = Arrays.<ListenableFuture<QueryResult>>asList(result);
}

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

public <T> ListenableFuture<T> retry(final Supplier<ListenableFuture<T>> code, final int retries,
        final long delay, final TimeUnit timeUnit, final Predicate<T> retryCondition) {

    SettableFuture<T> future = SettableFuture.create();
    startRetry(future, code, retries, delay, timeUnit, retryCondition);
    return future;
}

From source file:c5db.replication.SingleNodeFakeReplicator.java

@Override
public synchronized ListenableFuture<ReplicatorReceipt> logData(List<ByteBuffer> data) {
    SettableFuture<ReplicatorReceipt> receiptFuture = SettableFuture.create();
    final long thisSeqNum = nextSeqNum;
    nextSeqNum++;/*from w  w w.j a v  a  2 s.c  o  m*/

    doLater(() -> {
        receiptFuture.set(new ReplicatorReceipt(term, thisSeqNum));
        doLater(() -> commitNoticeChannel
                .publish(new IndexCommitNotice(quorumId, nodeId, thisSeqNum, thisSeqNum, term)));
    });

    return receiptFuture;
}

From source file:org.robotninjas.protobuf.netty.client.NettyRpcChannel.java

private ListenableFuture<Message> doCallMethod(MethodDescriptor method, final RpcController controller,
        Message request, final Message responsePrototype, boolean blocking) {

    ListenableFuture<NettyRpcProto.RpcResponse> result = new RpcCall(buildRequest(blocking, method, request));
    channel.writeAndFlush(result);// ww  w .  ja v  a2s .  co m
    return Futures.transform(result, new AsyncFunction<NettyRpcProto.RpcResponse, Message>() {
        public ListenableFuture<Message> apply(NettyRpcProto.RpcResponse input) {
            SettableFuture<Message> response = SettableFuture.create();
            try {
                final Message.Builder builder = responsePrototype.newBuilderForType();
                Message result = builder.mergeFrom(input.getResponseMessage()).build();
                response.set(result);
            } catch (InvalidProtocolBufferException e) {
                controller.setFailed(nullToEmpty(e.getMessage()));
                response.setException(e);
            }
            return response;
        }
    });

}