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

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

Introduction

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

Prototype

@Override
    public boolean set(@Nullable V value) 

Source Link

Usage

From source file:co.cask.cdap.common.zookeeper.ZKExtOperations.java

/**
 * Sets the content of a ZK node. Depends on the {@code createFirst} value,
 * either {@link ZKClient#create(String, byte[], org.apache.zookeeper.CreateMode)} or
 * {@link ZKClient#setData(String, byte[])} wil be called first.
 *
 * @param zkClient The ZKClient to perform the operations.
 * @param path The path in ZK./*from w ww . j  a  v a2 s.c  om*/
 * @param data The content of the ZK node.
 * @param result The result that will be set into the result future when completed successfully.
 * @param maxFailure Maximum number of times to try to create/set the content.
 * @param createFirst If true, create is called first, otherwise setData is called first.
 * @param <V> Type of the result.
 * @return A {@link ListenableFuture} that will be completed when node is created or data is set. The future will
 *         fail if failed to create and to set the data. Calling {@link ListenableFuture#cancel(boolean)} has
 *         no effect.
 */
private static <V> ListenableFuture<V> setContent(final ZKClient zkClient, final String path, final byte[] data,
        final V result, final int maxFailure, boolean createFirst, final List<ACL> createAcls) {

    final SettableFuture<V> resultFuture = SettableFuture.create();
    final AtomicInteger failureCount = new AtomicInteger(0);

    OperationFuture<?> operationFuture;

    if (createFirst) {
        if (createAcls != null) {
            operationFuture = zkClient.create(path, data, CreateMode.PERSISTENT, createAcls);
        } else {
            operationFuture = zkClient.create(path, data, CreateMode.PERSISTENT);
        }
    } else {
        operationFuture = zkClient.setData(path, data);
    }

    Futures.addCallback(operationFuture, new FutureCallback<Object>() {
        @Override
        public void onSuccess(Object zkResult) {
            resultFuture.set(result);
        }

        @Override
        public void onFailure(Throwable t) {
            if (failureCount.getAndIncrement() > maxFailure) {
                resultFuture.setException(new Exception("Failed more than " + maxFailure + "times", t));
            } else if (t instanceof KeeperException.NoNodeException) {
                // If node not exists, create it with the data
                OperationFuture<?> createFuture;
                if (createAcls != null) {
                    createFuture = zkClient.create(path, data, CreateMode.PERSISTENT, createAcls);
                } else {
                    createFuture = zkClient.create(path, data, CreateMode.PERSISTENT);
                }
                Futures.addCallback(createFuture, this, Threads.SAME_THREAD_EXECUTOR);
            } else if (t instanceof KeeperException.NodeExistsException) {
                // If the node exists when trying to create, set data.
                Futures.addCallback(zkClient.setData(path, data), this, Threads.SAME_THREAD_EXECUTOR);
            } else {
                resultFuture.setException(t);
            }
        }
    }, Threads.SAME_THREAD_EXECUTOR);

    return resultFuture;
}

From source file:org.apache.omid.tso.client.MockTSOClient.java

@Override
public TSOFuture<Long> getNewStartTimestamp() {
    synchronized (conflictMap) {
        SettableFuture<Long> f = SettableFuture.create();
        f.set(timestampGenerator.incrementAndGet());
        return new ForwardingTSOFuture<>(f);
    }//from   w  w w.  java 2 s  .c  o m
}

From source file:com.yahoo.omid.tsoclient.MockTSOClient.java

public TSOFuture<Void> close() {
    SettableFuture<Void> f = SettableFuture.<Void>create();
    f.set(null);
    return new ForwardingTSOFuture<Void>(f);
}

From source file:org.opendaylight.controller.cluster.datastore.messages.DataExists.java

@Override
public void processResponse(Object response, SettableFuture<Boolean> returnFuture) {
    if (DataExistsReply.isSerializedType(response)) {
        returnFuture.set(Boolean.valueOf(DataExistsReply.fromSerializable(response).exists()));
    } else {//from  w w  w. j a  v a2s.  c  o  m
        returnFuture.setException(
                new ReadFailedException("Invalid response checking exists for path " + getPath()));
    }
}

From source file:com.yahoo.omid.tsoclient.MockTSOClient.java

public TSOFuture<Long> getNewStartTimestamp() {
    synchronized (conflictMap) {
        SettableFuture<Long> f = SettableFuture.<Long>create();
        f.set(timestampGenerator.incrementAndGet());
        return new ForwardingTSOFuture<Long>(f);
    }/*from w w  w .  j av a2 s .  c  o m*/
}

From source file:com.facebook.presto.execution.buffer.OutputBufferMemoryManager.java

public synchronized void setNoBlockOnFull() {
    blockOnFull.set(false);/*  w w  w  .  j av  a 2  s  . c  o m*/

    // Complete future in a new thread to avoid making a callback on the caller thread.
    SettableFuture<?> future = notFull;
    notificationExecutor.execute(() -> future.set(null));
}

From source file:org.kurento.jsonrpc.internal.ws.PendingRequests.java

public void closeAllPendingRequests() {
    log.debug("Sending error to all pending requests");
    for (SettableFuture<Response<JsonElement>> responseFuture : pendingRequests.values()) {
        responseFuture.set(
                new Response<JsonElement>(new ResponseError(0, "Connection with server have been closed")));
    }//w  ww  .  ja va  2  s . co m
    pendingRequests.clear();
}

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

/**
 * Returns a future that fails with a {@link TimeoutException} if the parent future has not
 * finished before the timeout. The new returned future will always be executed on the provided
 * scheduledExecutorService, even when the parent future does not timeout.
 *
 * @param scheduledExecutorService executor that runs the timeout code. If the future times out,
 *                                 this is also the thread any callbacks will run on.
 * @param future                   the future to wrap as a timeout future.
 * @param timeout                  how long to wait before timing out a future
 * @param unit                     unit of the timeout
 * @return a future that may timeout before the parent future is done.
 *///from   w w  w  .j  av  a2 s.  c om
public static <T> ListenableFuture<T> makeTimeoutFuture(ScheduledExecutorService scheduledExecutorService,
        ListenableFuture<T> future, final long timeout, final TimeUnit unit) {
    final SettableFuture<T> promise = SettableFuture.create();

    scheduledExecutorService.schedule(new Runnable() {
        @Override
        public void run() {
            String message = "Future timed out after " + timeout + " " + unit.name();
            promise.setException(new TimeoutException(message));
        }
    }, timeout, unit);

    Futures.addCallback(future, new FutureCallback<T>() {
        @Override
        public void onSuccess(T result) {
            promise.set(result);
        }

        @Override
        public void onFailure(Throwable t) {
            promise.setException(t);
        }
    }, scheduledExecutorService);

    return promise;
}

From source file:com.yahoo.omid.committable.NullCommitTable.java

@Override
public ListenableFuture<CommitTable.Writer> getWriter() {
    SettableFuture<CommitTable.Writer> f = SettableFuture.<CommitTable.Writer>create();
    f.set(new Writer());
    return f;// w  ww  . jav a 2s .c o  m
}

From source file:com.yahoo.omid.committable.NullCommitTable.java

@Override
public ListenableFuture<CommitTable.Client> getClient() {
    SettableFuture<CommitTable.Client> f = SettableFuture.<CommitTable.Client>create();
    f.set(new Client());
    return f;//from   w  w  w  . j  a va 2s. c  om
}