Example usage for com.google.common.util.concurrent MoreExecutors directExecutor

List of usage examples for com.google.common.util.concurrent MoreExecutors directExecutor

Introduction

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

Prototype

public static Executor directExecutor() 

Source Link

Document

Returns an Executor that runs each task in the thread that invokes Executor#execute execute , as in CallerRunsPolicy .

Usage

From source file:com.navercorp.nbasearc.gcp.Gateway.java

ListenableFuture<?> init(SingleThreadEventLoopTrunk eventLoopTrunk, int reconnectInterval) {
    final SettableFuture<?> sf = SettableFuture.create();

    for (int i = 0; i < concnt; i++) {
        cons[i] = PhysicalConnection.create(ip, port, eventLoopTrunk.roundrobinEventLoop(), this,
                reconnectInterval);/* w w w .  ja va 2 s.  c o m*/

        final ListenableFuture<?> conFuture = cons[i].connect();
        conFuture.addListener(new Runnable() {
            @Override
            public void run() {
                try {
                    conFuture.get();
                    sf.set(null);
                } catch (InterruptedException e) {
                    log.error("Exception occured while connecting to {}", Gateway.this, e);
                } catch (ExecutionException e) {
                    log.error("Exception occured while connecting to {}", Gateway.this, e);
                }
            }
        }, MoreExecutors.directExecutor());
    }

    return sf;
}

From source file:com.google.gerrit.server.index.SiteIndexer.java

protected final void addErrorListener(ListenableFuture<?> future, String desc, ProgressMonitor progress,
        AtomicBoolean ok) {//  www .  j a  v a 2 s. co m
    future.addListener(new ErrorListener(future, desc, progress, ok), MoreExecutors.directExecutor());
}

From source file:io.atomix.rest.resources.EventsResource.java

@GET
@Path("/{subject}")
@Produces(MediaType.TEXT_PLAIN)/*from w  ww  . j  a va2s. co  m*/
public void next(@PathParam("subject") String subject, @Context ClusterEventService eventService,
        @Context EventManager events, @Suspended AsyncResponse response) {
    EventLog<Consumer<String>, String> eventLog = events.getOrCreateEventLog(ClusterEventService.class, subject,
            l -> e -> l.addEvent(e));
    CompletableFuture<Subscription> openFuture;
    if (eventLog.open()) {
        openFuture = eventService.subscribe(subject, eventLog.listener(), MoreExecutors.directExecutor());
    } else {
        openFuture = CompletableFuture.completedFuture(null);
    }

    openFuture.whenComplete((result, error) -> {
        if (error == null) {
            eventLog.nextEvent().whenComplete((event, eventError) -> {
                if (eventError == null) {
                    response.resume(Response.ok(event).build());
                } else {
                    response.resume(Response.noContent().build());
                }
            });
        } else {
            LOGGER.warn("{}", error);
            response.resume(Response.serverError().build());
        }
    });
}

From source file:org.onosproject.store.service.Topic.java

/**
 * Subscribes to messages published to this topic.
 * @param callback callback that will invoked when a message published to the topic is received.
 * @return a future that is completed when subscription request is completed.
 *///from ww  w .  jav a 2 s  .  com
default CompletableFuture<Void> subscribe(Consumer<T> callback) {
    return subscribe(callback, MoreExecutors.directExecutor());
}

From source file:org.apache.qpid.server.model.AbstractConfiguredObjectTypeFactory.java

@Override
public ListenableFuture<X> createAsync(final ConfiguredObjectFactory factory,
        final Map<String, Object> attributes, final ConfiguredObject<?> parent) {
    final SettableFuture<X> returnVal = SettableFuture.create();
    final X instance = createInstance(attributes, parent);
    final ListenableFuture<Void> createFuture = instance.createAsync();
    AbstractConfiguredObject.addFutureCallback(createFuture, new FutureCallback<Void>() {
        @Override//  w w  w . ja  va2 s  . c  om
        public void onSuccess(final Void result) {
            returnVal.set(instance);
        }

        @Override
        public void onFailure(final Throwable t) {
            returnVal.setException(t);
        }
    }, MoreExecutors.directExecutor());

    return returnVal;
}

From source file:com.tinspx.util.concurrent.FutureResult.java

@SuppressWarnings("LeakingThisInConstructor")
private FutureResult(ListenableFuture<T> future) {
    Futures.addCallback(future, this, MoreExecutors.directExecutor());
}

From source file:com.facebook.presto.execution.QueryQueue.java

public void enqueue(QueuedExecution queuedExecution) {
    queryQueueSize.incrementAndGet();/*  ww  w  . ja  va 2 s  .c o  m*/

    // Add a callback to dequeue the entry if it is ever completed.
    // This enables us to remove the entry sooner if is cancelled before starting,
    // and has no effect if called after starting.
    QueueEntry entry = new QueueEntry(queuedExecution, queryQueueSize::decrementAndGet);
    queuedExecution.getCompletionFuture().addListener(entry::dequeue, MoreExecutors.directExecutor());

    asyncSemaphore.submit(entry);
}

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

public ListenableFuture<Table> create(Table table) {
    ListenableFuture<Boolean> dbFuture = databases.exists(table.databaseName());
    return Futures.transformAsync(dbFuture, new AsyncFunction<Boolean, Table>() {
        @Override// w w  w  .  j av  a  2 s  . c o  m
        public ListenableFuture<Table> apply(Boolean exists) throws Exception {
            if (exists) {
                try {
                    ValidationEngine.validateAndThrow(table);
                    return tables.create(table);
                } catch (ValidationException e) {
                    return Futures.immediateFailedFuture(e);
                }
            } else {
                return Futures.immediateFailedFuture(
                        new ItemNotFoundException("Database not found: " + table.databaseName()));
            }
        }
    }, MoreExecutors.directExecutor());
}

From source file:net.javacrumbs.futureconverter.common.test.guava.GuavaConvertedFutureTestHelper.java

@Override
public void addCallbackTo(ListenableFuture<String> convertedFuture) {
    Futures.addCallback(convertedFuture, callback);
    convertedFuture.addListener(new Runnable() {
        @Override/*from w w  w  .  j  a v  a 2 s . c  o m*/
        public void run() {
            callbackCalled();
        }
    }, MoreExecutors.directExecutor());
}

From source file:com.google.devtools.build.lib.remote.ByteStreamBuildEventArtifactUploader.java

@Override
public ListenableFuture<PathConverter> upload(Map<Path, LocalFile> files) {
    if (files.isEmpty()) {
        return Futures.immediateFuture(PathConverter.NO_CONVERSION);
    }/*  w ww .j  av  a  2 s  .  c  om*/
    List<ListenableFuture<PathDigestPair>> uploads = new ArrayList<>(files.size());

    Context prevCtx = ctx.attach();
    try {
        for (Path file : files.keySet()) {
            Chunker chunker = new Chunker(file);
            Digest digest = chunker.digest();
            ListenableFuture<PathDigestPair> upload = Futures.transform(
                    uploader.uploadBlobAsync(chunker, /*forceUpload=*/false),
                    unused -> new PathDigestPair(file, digest), MoreExecutors.directExecutor());
            uploads.add(upload);
        }

        return Futures.transform(Futures.allAsList(uploads),
                (uploadsDone) -> new PathConverterImpl(remoteServerInstanceName, uploadsDone),
                MoreExecutors.directExecutor());
    } catch (IOException e) {
        return Futures.immediateFailedFuture(e);
    } finally {
        ctx.detach(prevCtx);
    }
}