Search in sources :

Example 1 with OfferRejectedExecutionHandler

use of org.apache.openejb.util.executor.OfferRejectedExecutionHandler in project tomee by apache.

the class ExecutorBuilder method build.

public ThreadPoolExecutor build(final Options options) {
    int corePoolSize = options.get(prefix + ".CorePoolSize", size);
    if (corePoolSize < 1) {
        corePoolSize = 1;
    }
    // Default setting is for a fixed pool size, MaximumPoolSize==CorePoolSize
    int maximumPoolSize = Math.max(options.get(prefix + ".MaximumPoolSize", corePoolSize), corePoolSize);
    if (maximumPoolSize < corePoolSize) {
        maximumPoolSize = corePoolSize;
    }
    // Default QueueSize is bounded using the corePoolSize, else bounded pools will never grow
    final int qsize = options.get(prefix + ".QueueSize", corePoolSize);
    // Keep Threads inactive threads alive for 60 seconds by default
    final Duration keepAliveTime = options.get(prefix + ".KeepAliveTime", new Duration(60, TimeUnit.SECONDS));
    // All threads can be timed out by default
    final boolean allowCoreThreadTimeout = options.get(prefix + ".AllowCoreThreadTimeOut", true);
    // If the user explicitly set the QueueSize to 0, we default QueueType to SYNCHRONOUS
    final QueueType defaultQueueType = qsize < 1 ? QueueType.SYNCHRONOUS : QueueType.LINKED;
    final BlockingQueue<Runnable> queue = options.get(prefix + ".QueueType", defaultQueueType).create(options, prefix, qsize);
    ThreadFactory factory = this.threadFactory;
    if (factory == null) {
        factory = new DaemonThreadFactory(prefix);
    }
    RejectedExecutionHandler handler = this.rejectedExecutionHandler;
    if (handler == null) {
        final String rejectedExecutionHandlerClass = options.get(prefix + ".RejectedExecutionHandlerClass", (String) null);
        if (rejectedExecutionHandlerClass == null) {
            final Duration duration = options.get(prefix + ".OfferTimeout", new Duration(30, TimeUnit.SECONDS));
            handler = new OfferRejectedExecutionHandler(duration);
        } else {
            try {
                handler = RejectedExecutionHandler.class.cast(Thread.currentThread().getContextClassLoader().loadClass(rejectedExecutionHandlerClass).newInstance());
            } catch (final Exception e) {
                throw new OpenEJBRuntimeException(e);
            }
        }
    }
    final ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime.getTime(), keepAliveTime.getUnit() != null ? keepAliveTime.getUnit() : TimeUnit.SECONDS, queue, factory, handler);
    threadPoolExecutor.allowCoreThreadTimeOut(allowCoreThreadTimeout);
    return threadPoolExecutor;
}
Also used : ThreadFactory(java.util.concurrent.ThreadFactory) RejectedExecutionHandler(java.util.concurrent.RejectedExecutionHandler) OfferRejectedExecutionHandler(org.apache.openejb.util.executor.OfferRejectedExecutionHandler) OpenEJBRuntimeException(org.apache.openejb.OpenEJBRuntimeException) OpenEJBRuntimeException(org.apache.openejb.OpenEJBRuntimeException) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) OfferRejectedExecutionHandler(org.apache.openejb.util.executor.OfferRejectedExecutionHandler)

Aggregations

RejectedExecutionHandler (java.util.concurrent.RejectedExecutionHandler)1 ThreadFactory (java.util.concurrent.ThreadFactory)1 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)1 OpenEJBRuntimeException (org.apache.openejb.OpenEJBRuntimeException)1 OfferRejectedExecutionHandler (org.apache.openejb.util.executor.OfferRejectedExecutionHandler)1