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:io.radiowitness.kinesis.consumer.KinesisRecordConsumerFactory.java

@Override
public IRecordProcessor createProcessor() {
    log.info("creating new record consumer");
    SettableFuture<ShutdownReason> shutdown = SettableFuture.create();
    Futures.addCallback(shutdown, this);
    return create(shutdown);
}

From source file:org.opendaylight.controller.md.sal.dom.broker.impl.PingPongTransaction.java

PingPongTransaction(final DOMDataReadWriteTransaction delegate) {
    this.delegate = Preconditions.checkNotNull(delegate);
    future = SettableFuture.create();
    submitFuture = new PingPongFuture(future);
    commitFuture = AbstractDataTransaction.convertToLegacyCommitFuture(submitFuture);
}

From source file:com.android.build.gradle.internal.process.GradleProcessExecutor.java

@NonNull
@Override/*w  w  w.j a va2  s .  com*/
public ListenableFuture<ProcessResult> submit(@NonNull final ProcessInfo processInfo,
        @NonNull final ProcessOutputHandler processOutputHandler) {
    final SettableFuture<ProcessResult> res = SettableFuture.create();
    new Thread() {
        @Override
        public void run() {
            try {
                ProcessResult result = execute(processInfo, processOutputHandler);
                res.set(result);
            } catch (Exception e) {
                res.setException(e);
            }
        }
    }.start();

    return res;
}

From source file:io.v.v23.InputChannels.java

/**
 * Returns a new {@link ListenableFuture} whose result is the list of all elements received
 * from the provided {@link InputChannel}.
 * <p>/*  w ww.  ja v a2s  . co  m*/
 * The returned future will be executed on the provided {@code executor}.
 */
@CheckReturnValue
public static <T> ListenableFuture<List<T>> asList(final InputChannel<T> channel, Executor executor) {
    final SettableFuture<List<T>> future = SettableFuture.create();
    Futures.addCallback(channel.recv(), newCallbackForList(channel, new ArrayList<T>(), future, executor),
            executor);
    return future;
}

From source file:com.yahoo.yqlplus.engine.internal.compiler.PlanProgramResultAdapter.java

public PlanProgramResultAdapter(ProgramTracer tracer, List<CompiledProgram.ResultSetInfo> resultSetInfos,
        ExecutionScoper scoper) {//from  w ww  .j  a v a 2s  . c  om
    this.tracer = tracer;
    ImmutableList.Builder<String> names = ImmutableList.builder();
    ImmutableMap.Builder<String, SettableFuture<YQLResultSet>> resultSets = ImmutableMap.builder();
    for (CompiledProgram.ResultSetInfo info : resultSetInfos) {
        names.add(info.getName());
        resultSets.put(info.getName(), SettableFuture.<YQLResultSet>create());
    }
    this.end = SettableFuture.create();
    this.names = names.build();
    this.resultSets = resultSets.build();
    this.scoper = scoper;
}

From source file:com.microsoft.office365.snippetapp.helpers.DiscoveryController.java

/**
 * Provides information about the service that corresponds to the provided
 * capability.// w w w. jav  a  2 s. c om
 *
 * @param capability A string that contains the capability of the service that
 *                   is going to be discovered.
 * @return A signal to wait on before continuing execution. The signal contains the
 * ServiceInfo object with extra information about discovered service.
 */
public SettableFuture<ServiceInfo> getServiceInfo(final String capability) {

    final SettableFuture<ServiceInfo> result = SettableFuture.create();

    // First, look in the locally cached services.
    if (mServices != null) {
        boolean serviceFound = false;
        for (ServiceInfo service : mServices) {
            if (service.getcapability().equals(capability)) {
                Log.i(TAG, "getServiceInfo - " + service.getserviceName() + " service for " + capability
                        + " was found in local cached services");
                result.set(service);
                serviceFound = true;
                break;
            }
        }

        if (!serviceFound) {
            NoSuchElementException noSuchElementException = new NoSuchElementException(
                    "The " + capability + " capability was not found in the local cached services.");
            Log.e(TAG, "getServiceInfo - " + noSuchElementException.getMessage());
            result.setException(noSuchElementException);
        }
    } else { // The services have not been cached yet. Go ask the discovery service.
        AuthenticationController.getInstance().setResourceId(Constants.DISCOVERY_RESOURCE_ID);
        ADALDependencyResolver dependencyResolver = (ADALDependencyResolver) AuthenticationController
                .getInstance().getDependencyResolver();

        DiscoveryClient discoveryClient = new DiscoveryClient(Constants.DISCOVERY_RESOURCE_URL,
                dependencyResolver);

        try {
            ListenableFuture<List<ServiceInfo>> future = discoveryClient.getservices()
                    .select("serviceResourceId,serviceEndpointUri,capability").read();
            Futures.addCallback(future, new FutureCallback<List<ServiceInfo>>() {
                @Override
                public void onSuccess(final List<ServiceInfo> services) {
                    Log.i(TAG, "getServiceInfo - Services discovered\n");
                    // Save the discovered services to serve further requests from the local cache.
                    mServices = services;

                    boolean serviceFound = false;
                    for (ServiceInfo service : services) {
                        if (service.getcapability().equals(capability)) {
                            Log.i(TAG, "getServiceInfo - " + service.getserviceName() + " service for "
                                    + capability + " was found in services retrieved from discovery");
                            result.set(service);
                            serviceFound = true;
                            break;
                        }
                    }

                    if (!serviceFound) {
                        NoSuchElementException noSuchElementException = new NoSuchElementException(
                                "The " + capability + " capability was not found in the user services.");
                        Log.e(TAG, "getServiceInfo - " + noSuchElementException.getMessage());
                        result.setException(noSuchElementException);
                    }
                }

                @Override
                public void onFailure(Throwable t) {
                    Log.e(TAG, "getServiceInfo - " + t.getMessage());
                    result.setException(t);
                }
            });
        } catch (Exception e) {
            Log.e(TAG, "getServiceInfo - " + e.getMessage());
            result.setException(e);
        }
    }
    return result;
}

From source file:com.sam.moca.async.LocalAsynchronousExecutor.java

@Override
public <V> Future<V> executeAsynchronously(Callable<V> callable) {
    SettableFuture<V> future = SettableFuture.create();
    ServerContext contextNullable = ServerUtils.getCurrentContextNullable();
    try {// www  . j  a v  a 2 s . c o  m
        V value = callable.call();
        future.set(value);
        if (contextNullable != null) {
            contextNullable.commit();
        }
    } catch (Throwable e) {
        future.setException(e);
        if (contextNullable != null) {
            try {
                contextNullable.rollback();
            } catch (MocaException e1) {
                _logger.warn("There was a problem rolling back transaction " + "for executing " + callable
                        + " when it failed!");
            }
        }
    }
    return future;
}

From source file:org.robotninjas.barge.jaxrs.client.BargeJaxRsClient.java

@Override
public ListenableFuture<RequestVoteResponse> requestVote(RequestVote request) {
    final SettableFuture<RequestVoteResponse> result = SettableFuture.create();

    client.target(baseUri).path("/raft/vote").request().async()
            .post(Entity.entity(request, MediaType.APPLICATION_JSON_TYPE), new InvocationCallback<Response>() {
                @Override/*  ww w.j  a va  2s.c om*/
                public void completed(Response response) {
                    result.set(response.readEntity(RequestVoteResponse.class));
                }

                @Override
                public void failed(Throwable throwable) {
                    result.setException(throwable);
                }
            });

    return result;
}

From source file:io.prestosql.operator.index.PageBuffer.java

/**
 * Adds a page to the buffer./* w  w  w. ja  va 2  s. com*/
 * Returns a ListenableFuture that is marked as done when the next page can be added.
 */
public synchronized ListenableFuture<?> add(Page page) {
    checkState(!isFull(), "PageBuffer is full!");
    pages.offer(page);
    if (isFull()) {
        if (settableFuture == null) {
            settableFuture = SettableFuture.create();
        }
        return settableFuture;
    }
    return NOT_FULL;
}

From source file:com.eviware.loadui.ui.fx.util.test.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./*from  www  . j a v a  2 s.c o m*/
 * 
 * @param task
 * @param timeoutInSeconds
 * @throws Throwable
 */
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 e) {
        if (e.getCause() instanceof Exception) {
            throw (Exception) e.getCause();
        } else {
            throw e;
        }
    }
}