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:io.druid.indexing.overlord.ForkingTaskRunner.java

@Inject
public ForkingTaskRunner(ForkingTaskRunnerConfig config, TaskConfig taskConfig, WorkerConfig workerConfig,
        Properties props, TaskLogPusher taskLogPusher, ObjectMapper jsonMapper, @Self DruidNode node) {
    this.config = config;
    this.taskConfig = taskConfig;
    this.props = props;
    this.taskLogPusher = taskLogPusher;
    this.jsonMapper = jsonMapper;
    this.node = node;
    this.portFinder = new PortFinder(config.getStartPort());

    this.exec = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(workerConfig.getCapacity()));
}

From source file:gobblin.writer.http.AbstractHttpWriter.java

@SuppressWarnings("rawtypes")
public AbstractHttpWriter(AbstractHttpWriterBuilder builder) {
    super(builder.getState());
    this.log = builder.getLogger().isPresent() ? (Logger) builder.getLogger()
            : LoggerFactory.getLogger(this.getClass());
    this.debugLogEnabled = this.log.isDebugEnabled();

    HttpClientBuilder httpClientBuilder = builder.getHttpClientBuilder();
    httpClientBuilder.setConnectionManager(
            new HttpClientConnectionManagerWithConnTracking(builder.getHttpConnManager()));
    this.client = httpClientBuilder.build();
    this.singleThreadPool = MoreExecutors.listeningDecorator(Executors.newSingleThreadExecutor());

    if (builder.getSvcEndpoint().isPresent()) {
        setCurServerHost((URI) builder.getSvcEndpoint().get());
    }/*w  w  w.j  a  v  a 2s.  c o  m*/
}

From source file:io.druid.server.lookup.namespace.cache.NamespaceExtractionCacheManager.java

public NamespaceExtractionCacheManager(Lifecycle lifecycle, final ServiceEmitter serviceEmitter,
        final Map<Class<? extends ExtractionNamespace>, ExtractionNamespaceCacheFactory<?>> namespaceFunctionFactoryMap) {
    this.listeningScheduledExecutorService = MoreExecutors.listeningDecorator(Executors.newScheduledThreadPool(
            1, new ThreadFactoryBuilder().setDaemon(true).setNameFormat("NamespaceExtractionCacheManager-%d")
                    .setPriority(Thread.MIN_PRIORITY).build()));
    ExecutorServices.manageLifecycle(lifecycle, listeningScheduledExecutorService);
    this.serviceEmitter = serviceEmitter;
    this.namespaceFunctionFactoryMap = namespaceFunctionFactoryMap;
    listeningScheduledExecutorService.scheduleAtFixedRate(new Runnable() {
        long priorTasksStarted = 0L;

        @Override//from   www  .  j a v  a  2  s  .co m
        public void run() {
            try {
                final long tasks = tasksStarted.get();
                serviceEmitter.emit(ServiceMetricEvent.builder().build("namespace/deltaTasksStarted",
                        tasks - priorTasksStarted));
                priorTasksStarted = tasks;
                monitor(serviceEmitter);
            } catch (Exception e) {
                log.error(e, "Error emitting namespace stats");
                if (Thread.currentThread().isInterrupted()) {
                    throw Throwables.propagate(e);
                }
            }
        }
    }, 1, 10, TimeUnit.MINUTES);
}

From source file:com.ibm.stocator.fs.cos.BlockingThreadPoolExecutorService.java

private BlockingThreadPoolExecutorService(int permitCount, ThreadPoolExecutor eventProcessingExecutorT) {
    super(MoreExecutors.listeningDecorator(eventProcessingExecutorT), permitCount, false);
    eventProcessingExecutor = eventProcessingExecutorT;
}

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.
 *//*ww w . ja  v a  2  s.co  m*/
@Deprecated
public static synchronized ListeningExecutorService getDefaultNotificationExecutor() {

    if (NOTIFICATION_EXECUTOR == null) {
        int queueSize = MAX_NOTIFICATION_QUEUE_SIZE;
        final String queueValue = System.getProperty(NOTIFICATION_QUEUE_SIZE_PROPERTY);
        if (StringUtils.isNotBlank(queueValue)) {
            try {
                queueSize = Integer.parseInt(queueValue);
                logger.trace("Queue size was set to {}", queueSize);
            } catch (final NumberFormatException e) {
                logger.warn("Cannot parse {} as set by {}, using default {}", queueValue,
                        NOTIFICATION_QUEUE_SIZE_PROPERTY, queueSize);
            }
        }

        // Overriding the queue:
        // ThreadPoolExecutor would not create new threads if the queue is not full, thus adding
        // occurs in RejectedExecutionHandler.
        // This impl saturates threadpool first, then queue. When both are full caller will get blocked.
        final BlockingQueue<Runnable> delegate = new LinkedBlockingQueue<>(queueSize);
        final BlockingQueue<Runnable> queue = new ForwardingBlockingQueue<Runnable>() {
            @Override
            protected BlockingQueue<Runnable> delegate() {
                return delegate;
            }

            @Override
            public boolean offer(final Runnable r) {
                // ThreadPoolExecutor will spawn a new thread after core size is reached only
                // if the queue.offer returns false.
                return false;
            }
        };

        final ThreadFactory factory = new ThreadFactoryBuilder().setDaemon(true)
                .setNameFormat("md-sal-binding-notification-%d").build();

        final ThreadPoolExecutor executor = new ThreadPoolExecutor(CORE_NOTIFICATION_THREADS,
                MAX_NOTIFICATION_THREADS, NOTIFICATION_THREAD_LIFE, TimeUnit.SECONDS, queue, factory,
                new RejectedExecutionHandler() {
                    // if the max threads are met, then it will raise a rejectedExecution. We then push to the queue.
                    @Override
                    public void rejectedExecution(final Runnable r, final ThreadPoolExecutor executor) {
                        try {
                            executor.getQueue().put(r);
                        } catch (final InterruptedException e) {
                            throw new RejectedExecutionException("Interrupted while waiting on the queue", e);
                        }
                    }
                });

        NOTIFICATION_EXECUTOR = MoreExecutors.listeningDecorator(executor);
    }

    return NOTIFICATION_EXECUTOR;
}

From source file:com.facebook.presto.mysql.CachingMySQLSchemaProvider.java

public CachingMySQLSchemaProvider(String connectorId, MySQLSession session, ExecutorService executor,
        Duration cacheTtl, Duration refreshInterval) {
    this.connectorId = checkNotNull(connectorId, "connectorId is null");
    this.session = checkNotNull(session, "mysqlSession is null");

    checkNotNull(executor, "executor is null");

    long expiresAfterWriteMillis = checkNotNull(cacheTtl, "cacheTtl is null").toMillis();
    long refreshMills = checkNotNull(refreshInterval, "refreshInterval is null").toMillis();

    ListeningExecutorService listeningExecutor = MoreExecutors.listeningDecorator(executor);

    schemaNamesCache = CacheBuilder.newBuilder().expireAfterWrite(expiresAfterWriteMillis, MILLISECONDS)
            .refreshAfterWrite(refreshMills, MILLISECONDS)
            .build(new BackgroundCacheLoader<String, Map<String, String>>(listeningExecutor) {
                @Override/*  w w w  . java  2s  . c  om*/
                public Map<String, String> load(String key) throws Exception {
                    return loadAllSchemas();
                }
            });

    tableNamesCache = CacheBuilder.newBuilder().expireAfterWrite(expiresAfterWriteMillis, MILLISECONDS)
            .refreshAfterWrite(refreshMills, MILLISECONDS)
            .build(new BackgroundCacheLoader<String, Map<String, String>>(listeningExecutor) {
                @Override
                public Map<String, String> load(String databaseName) throws Exception {
                    return loadAllTables(databaseName);
                }
            });

    tableCache = CacheBuilder.newBuilder().expireAfterWrite(expiresAfterWriteMillis, MILLISECONDS)
            .refreshAfterWrite(refreshMills, MILLISECONDS)
            .build(new BackgroundCacheLoader<SchemaTableName, MySQLTable>(listeningExecutor) {
                @Override
                public MySQLTable load(SchemaTableName tableName) throws Exception {
                    return loadTable(tableName);
                }
            });

    partitionsCache = CacheBuilder.newBuilder().expireAfterWrite(expiresAfterWriteMillis, MILLISECONDS)
            .refreshAfterWrite(refreshMills, MILLISECONDS)
            .build(new BackgroundCacheLoader<PartitionListKey, List<MySQLPartition>>(listeningExecutor) {
                @Override
                public List<MySQLPartition> load(PartitionListKey key) throws Exception {
                    return loadPartitions(key);
                }
            });

    partitionsCacheFull = CacheBuilder.newBuilder().expireAfterWrite(expiresAfterWriteMillis, MILLISECONDS)
            .build(new BackgroundCacheLoader<PartitionListKey, List<MySQLPartition>>(listeningExecutor) {
                @Override
                public List<MySQLPartition> load(PartitionListKey key) throws Exception {
                    return loadPartitions(key);
                }
            });
}

From source file:org.thingsboard.server.dao.sql.timeseries.JpaTimeseriesDao.java

@PostConstruct
public void init() {
    Optional<TsInsertExecutorType> executorTypeOptional = TsInsertExecutorType.parse(insertExecutorType);
    TsInsertExecutorType executorType;/*from   www . j  a  v  a 2s.c o m*/
    if (executorTypeOptional.isPresent()) {
        executorType = executorTypeOptional.get();
    } else {
        executorType = TsInsertExecutorType.FIXED;
    }
    switch (executorType) {
    case SINGLE:
        insertService = MoreExecutors.listeningDecorator(Executors.newSingleThreadExecutor());
        break;
    case FIXED:
        int poolSize = insertFixedThreadPoolSize;
        if (poolSize <= 0) {
            poolSize = 10;
        }
        insertService = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(poolSize));
        break;
    case CACHED:
        insertService = MoreExecutors.listeningDecorator(Executors.newCachedThreadPool());
        break;
    }
}

From source file:io.druid.indexing.kafka.KafkaIndexTaskClient.java

public KafkaIndexTaskClient(HttpClient httpClient, ObjectMapper jsonMapper, TaskInfoProvider taskInfoProvider,
        String dataSource, int numThreads, Duration httpTimeout, long numRetries) {
    this.httpClient = httpClient;
    this.jsonMapper = jsonMapper;
    this.taskInfoProvider = taskInfoProvider;
    this.httpTimeout = httpTimeout;
    this.numRetries = numRetries;
    this.retryPolicyFactory = createRetryPolicyFactory();

    this.executorService = MoreExecutors.listeningDecorator(
            Execs.multiThreaded(numThreads, String.format("KafkaIndexTaskClient-%s-%%d", dataSource)));
}

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

/**
 * /*from  www. ja v a  2s  .c  o  m*/
 * @param parallelism
 *            Maximum number of threads in the pool
 * @param threadName
 *            Naming scheme
 * @param workQueueSize
 *            Queue capacity
 * @param classLoader
 *            ClassLoader used as contextClassLoader for spawned threads
 */
public ThrottlingThreadPoolExecutorService(int parallelism, int rate, String threadName, int workQueueSize,
        final ClassLoader classLoader) {
    super();
    this.parallelism = parallelism;
    this.streamName = threadName;
    this.classLoader = classLoader;
    this.workQueueSize = workQueueSize;
    this.droppingMeter = Metrics.newMeter(getClass(), "throttling-dropping", "throttling-dropping",
            TimeUnit.SECONDS);
    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();
    rateLimitedPermits = RateLimiter.create(rate);
    workQueue = new ArrayBlockingQueue<Runnable>(workQueueSize + parallelism);
    ThreadPoolExecutor eventProcessingExecutor = new ThreadPoolExecutor(parallelism, parallelism, 60,
            TimeUnit.SECONDS, workQueue, threadFactory, new RejectedExecutionHandler() {

                @Override
                public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
                    droppingMeter.mark();
                }
            });
    eventProcessingExecutor.allowCoreThreadTimeOut(true);
    executorDelegatee = MoreExecutors.listeningDecorator(eventProcessingExecutor);

}

From source file:org.apache.hadoop.mapred.LocatedFileStatusFetcher.java

/**
 * @param conf configuration for the job
 * @param dirs the initial list of paths
 * @param recursive whether to traverse the patchs recursively
 * @param inputFilter inputFilter to apply to the resulting paths
 * @param newApi whether using the mapred or mapreduce API
 * @throws InterruptedException//from  ww w . jav a2 s  .c  o  m
 * @throws IOException
 */
public LocatedFileStatusFetcher(Configuration conf, Path[] dirs, boolean recursive, PathFilter inputFilter,
        boolean newApi) throws InterruptedException, IOException {
    int numThreads = conf.getInt(FileInputFormat.LIST_STATUS_NUM_THREADS,
            FileInputFormat.DEFAULT_LIST_STATUS_NUM_THREADS);
    rawExec = Executors.newFixedThreadPool(numThreads,
            new ThreadFactoryBuilder().setDaemon(true).setNameFormat("GetFileInfo #%d").build());
    exec = MoreExecutors.listeningDecorator(rawExec);
    resultQueue = new LinkedBlockingQueue<List<FileStatus>>();
    this.conf = conf;
    this.inputDirs = dirs;
    this.recursive = recursive;
    this.inputFilter = inputFilter;
    this.newApi = newApi;
}