Author: adrianc Date: Sun Jun 2 11:32:30 2013 New Revision: 1488671 URL: http://svn.apache.org/r1488671 Log: Converted JobPoller.java over to the new service config design.
https://issues.apache.org/jira/browse/OFBIZ-5204 Modified: ofbiz/trunk/framework/service/src/org/ofbiz/service/job/JobPoller.java Modified: ofbiz/trunk/framework/service/src/org/ofbiz/service/job/JobPoller.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/service/src/org/ofbiz/service/job/JobPoller.java?rev=1488671&r1=1488670&r2=1488671&view=diff ============================================================================== --- ofbiz/trunk/framework/service/src/org/ofbiz/service/job/JobPoller.java (original) +++ ofbiz/trunk/framework/service/src/org/ofbiz/service/job/JobPoller.java Sun Jun 2 11:32:30 2013 @@ -33,10 +33,12 @@ import java.util.concurrent.ThreadPoolEx import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; +import org.ofbiz.base.config.GenericConfigException; import org.ofbiz.base.start.Start; import org.ofbiz.base.util.Assert; import org.ofbiz.base.util.Debug; import org.ofbiz.service.config.ServiceConfigUtil; +import org.ofbiz.service.config.model.ThreadPool; /** * Job poller. Queues and runs jobs. @@ -51,8 +53,8 @@ public final class JobPoller { private static final int QUEUE_SIZE = 100; private static final long THREAD_TTL = 120000; // Idle thread lifespan - 2 minutes. private static final ConcurrentHashMap<String, JobManager> jobManagers = new ConcurrentHashMap<String, JobManager>(); - private static final ThreadPoolExecutor executor = new ThreadPoolExecutor(minThreads(), maxThreads(), getTTL(), - TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(queueSize()), new JobInvokerThreadFactory(), new ThreadPoolExecutor.AbortPolicy()); + // TODO: Put the executor in a cache so Job Poller settings can be changed at run-time. + private static final ThreadPoolExecutor executor = createThreadPoolExecutor(); private static final JobPoller instance = new JobPoller(); /** @@ -62,79 +64,26 @@ public final class JobPoller { return instance; } - private static long getTTL() { - String threadTTLAttr = ServiceConfigUtil.getElementAttr("thread-pool", "ttl"); - if (!threadTTLAttr.isEmpty()) { - try { - int threadTTL = Integer.parseInt(threadTTLAttr); - if (threadTTL > 0) { - return threadTTL; - } - } catch (NumberFormatException e) { - Debug.logError("Exception thrown while parsing thread TTL from serviceengine.xml file [" + e + "]. Using default value.", module); - } - } - return THREAD_TTL; - } - - private static int maxThreads() { - String maxThreadsAttr = ServiceConfigUtil.getElementAttr("thread-pool", "max-threads"); - if (!maxThreadsAttr.isEmpty()) { - try { - int maxThreads = Integer.parseInt(maxThreadsAttr); - if (maxThreads > 0) { - return maxThreads; - } - } catch (NumberFormatException e) { - Debug.logError("Exception thrown while parsing maximum threads from serviceengine.xml file [" + e + "]. Using default value.", module); - } - } - return MAX_THREADS; - } - - private static int minThreads() { - String minThreadsAttr = ServiceConfigUtil.getElementAttr("thread-pool", "min-threads"); - if (!minThreadsAttr.isEmpty()) { - try { - int minThreads = Integer.parseInt(minThreadsAttr); - if (minThreads > 0) { - return minThreads; - } - } catch (NumberFormatException e) { - Debug.logError("Exception thrown while parsing minimum threads from serviceengine.xml file [" + e + "]. Using default value.", module); - } + private static ThreadPoolExecutor createThreadPoolExecutor() { + try { + ThreadPool threadPool = ServiceConfigUtil.getServiceEngine(ServiceConfigUtil.engine).getThreadPool(); + return new ThreadPoolExecutor(threadPool.getMinThreads(), threadPool.getMaxThreads(), threadPool.getTtl(), + TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(threadPool.getJobs()), new JobInvokerThreadFactory(), new ThreadPoolExecutor.AbortPolicy()); + } catch (GenericConfigException e) { + Debug.logError(e, "Exception thrown while getting <thread-pool> model, using default <thread-pool> values: ", module); + return new ThreadPoolExecutor(MIN_THREADS, MAX_THREADS, THREAD_TTL, + TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(QUEUE_SIZE), new JobInvokerThreadFactory(), new ThreadPoolExecutor.AbortPolicy()); } - return MIN_THREADS; } private static int pollWaitTime() { - String pollIntervalAttr = ServiceConfigUtil.getElementAttr("thread-pool", "poll-db-millis"); - if (!pollIntervalAttr.isEmpty()) { - try { - int pollInterval = Integer.parseInt(pollIntervalAttr); - if (pollInterval > 0) { - return pollInterval; - } - } catch (NumberFormatException e) { - Debug.logError("Exception thrown while parsing database polling interval from serviceengine.xml file [" + e + "]. Using default value.", module); - } - } - return POLL_WAIT; - } - - private static int queueSize() { - String queueSizeAttr = ServiceConfigUtil.getElementAttr("thread-pool", "jobs"); - if (!queueSizeAttr.isEmpty()) { - try { - int queueSize = Integer.parseInt(queueSizeAttr); - if (queueSize > 0) { - return queueSize; - } - } catch (NumberFormatException e) { - Debug.logError("Exception thrown while parsing queue size from serviceengine.xml file [" + e + "]. Using default value.", module); - } + try { + ThreadPool threadPool = ServiceConfigUtil.getServiceEngine(ServiceConfigUtil.engine).getThreadPool(); + return threadPool.getPollDbMillis(); + } catch (GenericConfigException e) { + Debug.logError(e, "Exception thrown while getting <thread-pool> model, using default <thread-pool> values: ", module); + return POLL_WAIT; } - return QUEUE_SIZE; } /**