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;
}
Aggregations