Search in sources :

Example 1 with RejectableThreadPoolExecutor

use of org.apache.camel.util.concurrent.RejectableThreadPoolExecutor in project camel by apache.

the class DefaultThreadPoolFactory method newThreadPool.

public ExecutorService newThreadPool(int corePoolSize, int maxPoolSize, long keepAliveTime, TimeUnit timeUnit, int maxQueueSize, boolean allowCoreThreadTimeOut, RejectedExecutionHandler rejectedExecutionHandler, ThreadFactory threadFactory) throws IllegalArgumentException {
    // the core pool size must be 0 or higher
    if (corePoolSize < 0) {
        throw new IllegalArgumentException("CorePoolSize must be >= 0, was " + corePoolSize);
    }
    // validate max >= core
    if (maxPoolSize < corePoolSize) {
        throw new IllegalArgumentException("MaxPoolSize must be >= corePoolSize, was " + maxPoolSize + " >= " + corePoolSize);
    }
    BlockingQueue<Runnable> workQueue;
    if (corePoolSize == 0 && maxQueueSize <= 0) {
        // use a synchronous queue for direct-handover (no tasks stored on the queue)
        workQueue = new SynchronousQueue<Runnable>();
        // and force 1 as pool size to be able to create the thread pool by the JDK
        corePoolSize = 1;
        maxPoolSize = 1;
    } else if (maxQueueSize <= 0) {
        // use a synchronous queue for direct-handover (no tasks stored on the queue)
        workQueue = new SynchronousQueue<Runnable>();
    } else {
        // bounded task queue to store tasks on the queue
        workQueue = new LinkedBlockingQueue<Runnable>(maxQueueSize);
    }
    ThreadPoolExecutor answer = new RejectableThreadPoolExecutor(corePoolSize, maxPoolSize, keepAliveTime, timeUnit, workQueue);
    answer.setThreadFactory(threadFactory);
    answer.allowCoreThreadTimeOut(allowCoreThreadTimeOut);
    if (rejectedExecutionHandler == null) {
        rejectedExecutionHandler = new ThreadPoolExecutor.CallerRunsPolicy();
    }
    answer.setRejectedExecutionHandler(rejectedExecutionHandler);
    return answer;
}
Also used : SynchronousQueue(java.util.concurrent.SynchronousQueue) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) RejectableThreadPoolExecutor(org.apache.camel.util.concurrent.RejectableThreadPoolExecutor) RejectableScheduledThreadPoolExecutor(org.apache.camel.util.concurrent.RejectableScheduledThreadPoolExecutor) RejectableThreadPoolExecutor(org.apache.camel.util.concurrent.RejectableThreadPoolExecutor) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue)

Aggregations

LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)1 ScheduledThreadPoolExecutor (java.util.concurrent.ScheduledThreadPoolExecutor)1 SynchronousQueue (java.util.concurrent.SynchronousQueue)1 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)1 RejectableScheduledThreadPoolExecutor (org.apache.camel.util.concurrent.RejectableScheduledThreadPoolExecutor)1 RejectableThreadPoolExecutor (org.apache.camel.util.concurrent.RejectableThreadPoolExecutor)1