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

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

Introduction

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

Prototype

@Beta
@GwtIncompatible("concurrency")
public static ExecutorService getExitingExecutorService(ThreadPoolExecutor executor) 

Source Link

Document

Converts the given ThreadPoolExecutor into an ExecutorService that exits when the application is complete.

Usage

From source file:org.apache.abdera2.common.misc.MoreExecutors2.java

public static ExecutorService getExitingExecutor() {
    ThreadPoolExecutor tpe = (ThreadPoolExecutor) Executors.newCachedThreadPool();
    return MoreExecutors.getExitingExecutorService(tpe);
}

From source file:org.apache.abdera2.common.misc.MoreExecutors2.java

public static ExecutorService getExitingExecutor(ThreadFactory factory) {
    ThreadPoolExecutor tpe = (ThreadPoolExecutor) Executors.newCachedThreadPool(factory);
    return MoreExecutors.getExitingExecutorService(tpe);
}

From source file:org.apache.abdera2.common.misc.MoreExecutors2.java

public static ExecutorService getExitingFixedExecutor(int n) {
    ThreadPoolExecutor tpe = (ThreadPoolExecutor) Executors.newFixedThreadPool(n);
    return MoreExecutors.getExitingExecutorService(tpe);
}

From source file:org.apache.abdera2.common.misc.MoreExecutors2.java

public static ExecutorService getExitingFixedExecutor(int n, ThreadFactory factory) {
    ThreadPoolExecutor tpe = (ThreadPoolExecutor) Executors.newFixedThreadPool(n, factory);
    return MoreExecutors.getExitingExecutorService(tpe);
}

From source file:com.dangdang.ddframe.job.api.executor.handler.impl.DefaultExecutorServiceHandler.java

@Override
public ExecutorService createExecutorService() {
    return MoreExecutors.listeningDecorator(MoreExecutors
            .getExitingExecutorService(new ThreadPoolExecutor(0, Runtime.getRuntime().availableProcessors() * 2,
                    1L, TimeUnit.MINUTES, new LinkedBlockingQueue<Runnable>())));
}

From source file:com.dangdang.ddframe.job.util.concurrent.ExecutorServiceObject.java

/**
 * ?./*w w w .  j  a va  2s.c  o  m*/
 *
 * @return ?
 */
public ExecutorService createExecutorService() {
    return MoreExecutors.listeningDecorator(MoreExecutors.getExitingExecutorService(threadPoolExecutor));
}

From source file:com.google.gerrit.server.git.ReceiveCommitsExecutorModule.java

@Provides
@Singleton/*from ww  w  .  j  a  v a 2 s  .c o m*/
@ChangeUpdateExecutor
public ListeningExecutorService createChangeUpdateExecutor(@GerritServerConfig Config config) {
    int poolSize = config.getInt("receive", null, "changeUpdateThreads", 1);
    if (poolSize <= 1) {
        return MoreExecutors.sameThreadExecutor();
    }
    return MoreExecutors.listeningDecorator(MoreExecutors.getExitingExecutorService(new ThreadPoolExecutor(1,
            poolSize, 10, TimeUnit.MINUTES, new ArrayBlockingQueue<Runnable>(poolSize),
            new ThreadFactoryBuilder().setNameFormat("ChangeUpdate-%d").setDaemon(true).build(),
            new ThreadPoolExecutor.CallerRunsPolicy())));
}

From source file:com.dangdang.ddframe.rdb.sharding.executor.ExecutorEngine.java

public ExecutorEngine(final ShardingProperties shardingProperties) {
    int executorMinIdleSize = shardingProperties.getValue(ShardingPropertiesConstant.EXECUTOR_MIN_IDLE_SIZE);
    int executorMaxSize = shardingProperties.getValue(ShardingPropertiesConstant.EXECUTOR_MAX_SIZE);
    long executorMaxIdleTimeoutMilliseconds = shardingProperties
            .getValue(ShardingPropertiesConstant.EXECUTOR_MAX_IDLE_TIMEOUT_MILLISECONDS);
    executorService = MoreExecutors.listeningDecorator(MoreExecutors.getExitingExecutorService(
            new ThreadPoolExecutor(executorMinIdleSize, executorMaxSize, executorMaxIdleTimeoutMilliseconds,
                    TimeUnit.MILLISECONDS, new SynchronousQueue<Runnable>())));
}

From source file:com.addthis.codec.utils.ExecutorServiceBuilder.java

public ExecutorService build() {
    ThreadPoolExecutor service = new ThreadPoolExecutor(coreThreads, maxThreads, keepAlive,
            TimeUnit.MILLISECONDS, queue, threadFactory);
    if (shutdownHook) {
        return MoreExecutors.getExitingExecutorService(service);
    } else {/*from  w w  w .j a v a 2  s . c om*/
        return service;
    }
}

From source file:org.apache.pig.backend.hadoop.executionengine.physicalLayer.util.MonitoredUDFExecutor.java

@SuppressWarnings("unchecked")
public MonitoredUDFExecutor(EvalFunc udf) {
    // is 10 enough? This is pretty arbitrary.
    exec = MoreExecutors//from   w ww. ja va 2s .  co  m
            .listeningDecorator(MoreExecutors.getExitingExecutorService(new ScheduledThreadPoolExecutor(1)));
    this.evalFunc = udf;
    MonitoredUDF anno = udf.getClass().getAnnotation(MonitoredUDF.class);
    timeUnit = anno.timeUnit();
    duration = anno.duration();
    errorCallback = anno.errorCallback();

    // The exceptions really should not happen since our handlers are defined by the parent class which
    // must be extended by all custom handlers.
    try {
        errorHandler = errorCallback.getMethod("handleError", EvalFunc.class, Exception.class);
        timeoutHandler = errorCallback.getMethod("handleTimeout", EvalFunc.class, Exception.class);
    } catch (SecurityException e1) {
        throw new RuntimeException(
                "Unable to use the monitored callback due to a Security Exception while working with "
                        + evalFunc.getClass().getName());
    } catch (NoSuchMethodException e1) {
        throw new RuntimeException(
                "Unable to use the monitored callback because a required method not found while working with "
                        + evalFunc.getClass().getName());
    }

    Type retType = udf.getReturnType();
    defaultValue = getDefaultValue(anno, retType);
    closure = new Function<Tuple, Object>() {
        @Override
        public Object apply(Tuple input) {
            try {
                return evalFunc.exec(input);
            } catch (IOException e) {
                // I don't see a CheckedFunction in Guava. Resorting to this hackery.
                throw new RuntimeException(e);
            }
        }
    };
}