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.google.cloud.examples.pubsub.snippets.CreateSubscriptionAndConsumeMessages.java

public static void main(String... args) throws Exception {
    TopicName topic = TopicName.of("my-project-id", "my-topic-id");
    SubscriptionName subscription = SubscriptionName.of("my-project-id", "my-subscription-id");

    try (SubscriptionAdminClient subscriptionAdminClient = SubscriptionAdminClient.create()) {
        subscriptionAdminClient.createSubscription(subscription, topic, PushConfig.getDefaultInstance(), 0);
    }//from  w w  w  .  j a va  2 s  .c  o m

    MessageReceiver receiver = new MessageReceiver() {
        @Override
        public void receiveMessage(PubsubMessage message, AckReplyConsumer consumer) {
            System.out.println("Received message: " + message.getData().toStringUtf8());
            consumer.ack();
        }
    };
    Subscriber subscriber = null;
    try {
        subscriber = Subscriber.newBuilder(subscription, receiver).build();
        subscriber.addListener(new Subscriber.Listener() {
            @Override
            public void failed(Subscriber.State from, Throwable failure) {
                // Handle failure. This is called when the Subscriber encountered a fatal error and is shutting down.
                System.err.println(failure);
            }
        }, MoreExecutors.directExecutor());
        subscriber.startAsync().awaitRunning();

        // In this example, we will pull messages for one minute (60,000ms) then stop.
        // In a real application, this sleep-then-stop is not necessary.
        // Simply call stopAsync().awaitTerminated() when the server is shutting down, etc.
        Thread.sleep(60000);
    } finally {
        if (subscriber != null) {
            subscriber.stopAsync().awaitTerminated();
        }
    }
}

From source file:com.google.cloud.examples.pubsub.snippets.CreateSubscriptionAndPullMessages.java

public static void main(String... args) throws Exception {
    TopicName topic = TopicName.create("test-project", "test-topic");
    SubscriptionName subscription = SubscriptionName.create("test-project", "test-subscription");

    try (SubscriberClient subscriberClient = SubscriberClient.create()) {
        subscriberClient.createSubscription(subscription, topic, PushConfig.getDefaultInstance(), 0);
    }//ww  w  .j a  v  a 2s  .co m

    MessageReceiver receiver = new MessageReceiver() {
        @Override
        public void receiveMessage(PubsubMessage message, AckReplyConsumer consumer) {
            System.out.println("got message: " + message.getData().toStringUtf8());
            consumer.accept(AckReply.ACK, null);
        }
    };
    Subscriber subscriber = null;
    try {
        subscriber = Subscriber.newBuilder(subscription, receiver).build();
        subscriber.addListener(new Subscriber.SubscriberListener() {
            @Override
            public void failed(Subscriber.State from, Throwable failure) {
                // Handle failure. This is called when the Subscriber encountered a fatal error and is shutting down.
                System.err.println(failure);
            }
        }, MoreExecutors.directExecutor());
        subscriber.startAsync().awaitRunning();
        Thread.sleep(60000);
    } finally {
        if (subscriber != null) {
            subscriber.stopAsync();
        }
    }
}

From source file:needs18.Guava18.java

/**
 * Returns an executor that runs immediately, which is
 * present in Guava 18, but absent in 17.
 * /*from www  . ja  v  a  2 s  .c o  m*/
 * http://google.github.io/guava/releases/18.0/api/diffs/changes/com.google.common.util.concurrent.MoreExecutors.html#com.google.common.util.concurrent.MoreExecutors.newDirectExecutorService_added()
 */
public static Executor sameThread() {
    return MoreExecutors.directExecutor();
}

From source file:org.opendaylight.infrautils.utils.concurrent.ListenableToCompletableFutureWrapper.java

public static <T> CompletionStage<T> create(ListenableFuture<T> guavaListenableFuture) {
    ListenableToCompletableFutureWrapper<T> instance = new ListenableToCompletableFutureWrapper<>(
            guavaListenableFuture);//from  w w  w . j  a  va 2 s .c o m
    Futures.addCallback(guavaListenableFuture, instance, MoreExecutors.directExecutor());
    return instance;
}

From source file:com.sk89q.worldedit.command.util.FutureProgressListener.java

public static void addProgressListener(ListenableFuture<?> future, Actor sender, String message) {
    future.addListener(new FutureProgressListener(sender, message), MoreExecutors.directExecutor());
}

From source file:io.crate.operation.RejectionAwareExecutor.java

/**
 * returns an Executor that will either execute the command given the Executor delegate or
 * call the callback.onFailure if it receives a (Es)RejectedExecutionException
 *///from  w  w w  .  j  av  a 2 s.c om
public static Executor wrapExecutor(Executor delegate, FutureCallback<?> callback) {
    if (delegate == MoreExecutors.directExecutor()) {
        // directExecutor won't reject anything...
        return delegate;
    }

    return new RejectionAwareExecutor(delegate, callback);
}

From source file:org.graylog2.events.ClusterEventBus.java

public ClusterEventBus() {
    this(MoreExecutors.directExecutor());
}

From source file:dagger.functional.producers.ExecutorModule.java

public ExecutorModule() {
    this(MoreExecutors.directExecutor());
}

From source file:io.soabase.core.listening.ListenerContainer.java

@Override
public void addListener(T listener) {
    addListener(listener, MoreExecutors.directExecutor());
}

From source file:io.atomix.core.map.DistributedMap.java

/**
 * Registers the specified listener to be notified whenever the map is updated.
 *
 * @param listener listener to notify about map events
 *///w  w w .ja v a 2 s  . co  m
default void addListener(MapEventListener<K, V> listener) {
    addListener(listener, MoreExecutors.directExecutor());
}