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

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

Introduction

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

Prototype

@GwtIncompatible("TODO")
public static ListeningScheduledExecutorService listeningDecorator(ScheduledExecutorService delegate) 

Source Link

Document

Creates a ScheduledExecutorService whose submit and invokeAll methods submit ListenableFutureTask instances to the given delegate executor.

Usage

From source file:org.apache.s4.comm.staging.BlockingThreadPoolExecutorService.java

/**
 * /*from   w  w w  .j  ava 2 s .  c o  m*/
 * @param parallelism
 *            Maximum number of threads in the pool
 * @param fairParallelism
 *            If true, in case of contention, waiting threads will be
 *            scheduled in a first-in first-out manner. This can be help
 *            ensure ordering, though there is an associated performance
 *            cost (typically small).
 * @param threadName
 *            Naming scheme
 * @param workQueueSize
 *            Queue capacity
 * @param classLoader
 *            ClassLoader used as contextClassLoader for spawned threads
 */
public BlockingThreadPoolExecutorService(int parallelism, boolean fairParallelism, String threadName,
        int workQueueSize, final ClassLoader classLoader) {
    super();
    this.parallelism = parallelism;
    this.streamName = threadName;
    this.classLoader = classLoader;
    this.workQueueSize = workQueueSize;
    queueingPermits = new Semaphore(workQueueSize + parallelism, false);
    ThreadFactory threadFactory = new ThreadFactoryBuilder().setDaemon(true).setNameFormat(threadName)
            .setThreadFactory(new ThreadFactory() {

                @Override
                public Thread newThread(Runnable r) {
                    Thread t = new Thread(r);
                    t.setContextClassLoader(classLoader);
                    return t;
                }
            }).build();
    // queueingPermits semaphore controls the size of the queue, thus no
    // need to use a bounded queue
    workQueue = new LinkedBlockingQueue<Runnable>(workQueueSize + parallelism);
    ThreadPoolExecutor eventProcessingExecutor = new ThreadPoolExecutor(parallelism, parallelism, 60,
            TimeUnit.SECONDS, workQueue, threadFactory, new RejectedExecutionHandler() {

                @Override
                public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
                    // This is not expected to happen.
                    logger.error("Could not submit task to executor {}", executor.toString());
                }
            });
    eventProcessingExecutor.allowCoreThreadTimeOut(true);
    executorDelegatee = MoreExecutors.listeningDecorator(eventProcessingExecutor);
}

From source file:org.attribyte.relay.AsyncPublisher.java

/**
 * Creates a publisher with a specified notification queue size.
 * @param numProcessors The number of threads processing the notification queue.
 * @param maxQueueSize The maximum queue size. If &lt; 1, notification queue is unbounded.
 * @param notificationTimeoutSeconds The notification send timeout.
 * @param acceptCodes The set of HTTP response codes that map to 'Accept'.
 * @param logger An optional logger.//from  www .ja  v a2 s.c o m
 * @throws Exception on initialization error.
 */
public AsyncPublisher(final int numProcessors, final int maxQueueSize, final int notificationTimeoutSeconds,
        final Set<Integer> acceptCodes, final Optional<Logger> logger) throws Exception {
    final BlockingQueue<Runnable> notifications;
    assert (numProcessors > 0);
    if (maxQueueSize > 0) {
        notifications = new ArrayBlockingQueue<>(maxQueueSize);
    } else {
        notifications = new LinkedBlockingQueue<>();
    }

    ThreadPoolExecutor executor = new ThreadPoolExecutor(numProcessors, numProcessors, 0L,
            TimeUnit.MILLISECONDS, notifications,
            new ThreadFactoryBuilder().setNameFormat("async-publisher-%d").build(),
            new ThreadPoolExecutor.AbortPolicy());
    executor.prestartAllCoreThreads();
    this.notificationExecutor = MoreExecutors.listeningDecorator(executor);
    this.notificationQueueSize = new CachedGauge<Integer>(15L, TimeUnit.SECONDS) {
        protected Integer loadValue() {
            return notifications.size();
        }
    };
    SslContextFactory sslContextFactory = new SslContextFactory();
    this.httpClient = new HttpClient(sslContextFactory);
    this.httpClient.setFollowRedirects(false);
    this.httpClient.setConnectTimeout(notificationTimeoutSeconds * 1000L);
    this.httpClient.setCookieStore(new HttpCookieStore.Empty());
    this.notificationTimeoutSeconds = notificationTimeoutSeconds;
    this.acceptCodes = ImmutableSet.copyOf(acceptCodes);
    this.logger = logger.or(new ConsoleLogger());
}

From source file:com.netflix.astyanax.thrift.ThriftKeyspaceImpl.java

public ThriftKeyspaceImpl(String ksName, ConnectionPool<Cassandra.Client> pool, AstyanaxConfiguration config,
        final KeyspaceTracerFactory tracerFactory) {
    this.connectionPool = pool;
    this.config = config;
    this.ksName = ksName;
    this.executor = MoreExecutors.listeningDecorator(config.getAsyncExecutor());
    this.tracerFactory = tracerFactory;
    this.cache = CacheBuilder.newBuilder().expireAfterWrite(10, TimeUnit.MINUTES).build();
    this.cqlStatementFactory = ThriftCqlFactoryResolver.createFactory(config);
}

From source file:org.eclipse.osee.executor.admin.internal.ExecutorAdminImpl.java

private ListeningExecutorService createExecutor(String id, int poolSize) {
    ThreadFactory threadFactory = new ThreadFactoryBuilder()//
            .setNameFormat(id + "- [%s]")//
            .setPriority(Thread.NORM_PRIORITY)//
            .build();//  w  w w .java 2s  . c om

    ExecutorService executor = null;
    if (poolSize > 0) {
        executor = Executors.newFixedThreadPool(poolSize, threadFactory);
    } else {
        executor = Executors.newCachedThreadPool(threadFactory);
    }

    ListeningExecutorService listeningExecutor = MoreExecutors.listeningDecorator(executor);
    cache.put(id, listeningExecutor);
    return listeningExecutor;
}

From source file:io.druid.query.metadata.SegmentMetadataQueryRunnerFactory.java

@Override
public QueryRunner<SegmentAnalysis> mergeRunners(ExecutorService exec,
        Iterable<QueryRunner<SegmentAnalysis>> queryRunners) {
    final ListeningExecutorService queryExecutor = MoreExecutors.listeningDecorator(exec);
    return new ConcatQueryRunner<SegmentAnalysis>(Sequences.map(Sequences.simple(queryRunners),
            new Function<QueryRunner<SegmentAnalysis>, QueryRunner<SegmentAnalysis>>() {
                @Override/*from   w w w .  j a v  a 2s.co  m*/
                public QueryRunner<SegmentAnalysis> apply(final QueryRunner<SegmentAnalysis> input) {
                    return new QueryRunner<SegmentAnalysis>() {
                        @Override
                        public Sequence<SegmentAnalysis> run(final Query<SegmentAnalysis> query,
                                final Map<String, Object> responseContext) {
                            final int priority = query.getContextPriority(0);
                            ListenableFuture<Sequence<SegmentAnalysis>> future = queryExecutor.submit(
                                    new AbstractPrioritizedCallable<Sequence<SegmentAnalysis>>(priority) {
                                        @Override
                                        public Sequence<SegmentAnalysis> call() throws Exception {
                                            return Sequences
                                                    .simple(Sequences.toList(input.run(query, responseContext),
                                                            new ArrayList<SegmentAnalysis>()));
                                        }
                                    });
                            try {
                                queryWatcher.registerQuery(query, future);
                                final Number timeout = query.getContextValue(QueryContextKeys.TIMEOUT,
                                        (Number) null);
                                return timeout == null ? future.get()
                                        : future.get(timeout.longValue(), TimeUnit.MILLISECONDS);
                            } catch (InterruptedException e) {
                                log.warn(e, "Query interrupted, cancelling pending results, query id [%s]",
                                        query.getId());
                                future.cancel(true);
                                throw new QueryInterruptedException("Query interrupted");
                            } catch (CancellationException e) {
                                throw new QueryInterruptedException("Query cancelled");
                            } catch (TimeoutException e) {
                                log.info("Query timeout, cancelling pending results for query id [%s]",
                                        query.getId());
                                future.cancel(true);
                                throw new QueryInterruptedException("Query timeout");
                            } catch (ExecutionException e) {
                                throw Throwables.propagate(e.getCause());
                            }
                        }
                    };
                }
            }));
}

From source file:org.eclipse.neoscada.protocol.iec60870.server.data.AbstractBaseDataModel.java

@Override
public synchronized void start() {
    if (this.executor != null) {
        // double start
        return;/*  w w w .  java2  s .c o  m*/
    }

    this.executor = MoreExecutors.listeningDecorator(
            Executors.newSingleThreadScheduledExecutor(new NamedThreadFactory(this.threadName, false, true)));
}

From source file:org.opendaylight.controller.sal.binding.codegen.impl.SingletonHolder.java

/**
 * @deprecated This method is only used from configuration modules and thus callers of it
 *             should use service injection to make the executor configurable.
 *//*from  w w  w  .j  av  a 2s  .  co  m*/
@Deprecated
public static synchronized ListeningExecutorService getDefaultCommitExecutor() {
    if (COMMIT_EXECUTOR == null) {
        final ThreadFactory factory = new ThreadFactoryBuilder().setDaemon(true)
                .setNameFormat("md-sal-binding-commit-%d").build();
        /*
         * FIXME: this used to be newCacheThreadPool(), but MD-SAL does not have transaction
         *        ordering guarantees, which means that using a concurrent threadpool results
         *        in application data being committed in random order, potentially resulting
         *        in inconsistent data being present. Once proper primitives are introduced,
         *        concurrency can be reintroduced.
         */
        final ExecutorService executor = Executors.newSingleThreadExecutor(factory);
        COMMIT_EXECUTOR = MoreExecutors.listeningDecorator(executor);
    }

    return COMMIT_EXECUTOR;
}

From source file:org.apache.hadoop.hive.llap.tezplugins.LlapDaemonProtocolClientProxy.java

public LlapDaemonProtocolClientProxy(int numThreads, Configuration conf, Token<LlapTokenIdentifier> llapToken) {
    super(LlapDaemonProtocolClientProxy.class.getSimpleName());
    this.hostProxies = new ConcurrentHashMap<>();
    this.socketFactory = NetUtils.getDefaultSocketFactory(conf);
    this.llapToken = llapToken;

    long connectionTimeout = HiveConf.getTimeVar(conf, ConfVars.LLAP_TASK_COMMUNICATOR_CONNECTION_TIMEOUT_MS,
            TimeUnit.MILLISECONDS);
    long retrySleep = HiveConf.getTimeVar(conf,
            ConfVars.LLAP_TASK_COMMUNICATOR_CONNECTION_SLEEP_BETWEEN_RETRIES_MS, TimeUnit.MILLISECONDS);
    this.retryPolicy = RetryPolicies.retryUpToMaximumTimeWithFixedSleep(connectionTimeout, retrySleep,
            TimeUnit.MILLISECONDS);

    this.requestManager = new RequestManager(numThreads);
    ExecutorService localExecutor = Executors.newFixedThreadPool(1,
            new ThreadFactoryBuilder().setNameFormat("RequestManagerExecutor").build());
    this.requestManagerExecutor = MoreExecutors.listeningDecorator(localExecutor);

    LOG.info("Setting up taskCommunicator with" + "numThreads=" + numThreads + "retryTime(millis)="
            + connectionTimeout + "retrySleep(millis)=" + retrySleep);
}

From source file:org.apache.tez.service.impl.ContainerRunnerImpl.java

public ContainerRunnerImpl(int numExecutors, String[] localDirsBase,
        AtomicReference<InetSocketAddress> localAddress, long totalMemoryAvailableBytes,
        TezExecutors sharedExecutor) {/*from w w  w  .  ja  v a 2s. c o m*/
    super("ContainerRunnerImpl");
    Preconditions.checkState(numExecutors > 0,
            "Invalid number of executors: " + numExecutors + ". Must be > 0");
    this.localDirsBase = localDirsBase;
    this.localAddress = localAddress;

    ExecutorService raw = Executors.newFixedThreadPool(numExecutors,
            new ThreadFactoryBuilder().setNameFormat("ContainerExecutor %d").build());
    this.executorService = MoreExecutors.listeningDecorator(raw);

    // 80% of memory considered for accounted buffers. Rest for objects.
    // TODO Tune this based on the available size.
    this.memoryPerExecutor = (long) (totalMemoryAvailableBytes * 0.8 / (float) numExecutors);

    LOG.info("ContainerRunnerImpl config: " + "memoryPerExecutorDerived=" + memoryPerExecutor
            + ", numExecutors=" + numExecutors);
    this.sharedExecutor = sharedExecutor;
}

From source file:org.apache.hadoop.hive.llap.daemon.impl.LlapTaskReporter.java

public LlapTaskReporter(SchedulerFragmentCompletingListener completionListener,
        LlapTaskUmbilicalProtocol umbilical, long amPollInterval, long sendCounterInterval, int maxEventsToGet,
        AtomicLong requestCounter, String containerIdStr, final String fragmentId, TezEvent initialEvent,
        String fragmentRequestId) {
    this.umbilical = umbilical;
    this.pollInterval = amPollInterval;
    this.sendCounterInterval = sendCounterInterval;
    this.maxEventsToGet = maxEventsToGet;
    this.requestCounter = requestCounter;
    this.containerIdStr = containerIdStr;
    this.fragmentId = fragmentId;
    this.initialEvent = initialEvent;
    ExecutorService executor = Executors.newFixedThreadPool(1,
            new ThreadFactoryBuilder().setDaemon(true).setNameFormat("TaskHeartbeatThread").build());
    heartbeatExecutor = MoreExecutors.listeningDecorator(executor);
    this.completionListener = completionListener;
    this.fragmentRequestId = fragmentRequestId;
}