001: /*
002:
003: This software is OSI Certified Open Source Software.
004: OSI Certified is a certification mark of the Open Source Initiative.
005:
006: The license (Mozilla version 1.0) can be read at the MMBase site.
007: See http://www.MMBase.org/license
008:
009: */
010: package org.mmbase.util;
011:
012: import java.util.*;
013: import java.util.concurrent.*;
014: import org.mmbase.util.logging.*;
015: import org.mmbase.util.xml.UtilReader;
016:
017: /**
018: * Generic MMBase Thread Pools
019: *
020: * @since MMBase 1.8
021: * @author Michiel Meewissen
022: * @version $Id: ThreadPools.java,v 1.10 2007/06/21 15:50:22 nklasens Exp $
023: */
024: public abstract class ThreadPools {
025: private static final Logger log = Logging
026: .getLoggerInstance(ThreadPools.class);
027:
028: /**
029: * Generic Thread Pools which can be used by 'filters'.
030: */
031: public static final ExecutorService filterExecutor = Executors
032: .newCachedThreadPool();
033:
034: /**
035: * For jobs there are 'scheduled', and typically happen on larger time-scales.
036: */
037: public static final ExecutorService jobsExecutor = new ThreadPoolExecutor(
038: 2, 10, 5 * 60, TimeUnit.SECONDS,
039: new ArrayBlockingQueue<Runnable>(200), new ThreadFactory() {
040:
041: public Thread newThread(Runnable r) {
042: Thread t = new Thread(r, "JOBTHREAD") {
043: /**
044: * Overrides run of Thread to catch and log all exceptions. Otherwise they go through to app-server.
045: */
046: public void run() {
047: try {
048: super .run();
049: } catch (Throwable t) {
050: log.error("Error during job: "
051: + t.getClass().getName() + " "
052: + t.getMessage(), t);
053: }
054: }
055: };
056: t.setDaemon(true);
057: return t;
058: }
059: });
060:
061: private static final UtilReader properties = new UtilReader(
062: "threadpools.xml", new Runnable() {
063: public void run() {
064: configure();
065: }
066: });
067:
068: /**
069: * @since MMBase-1.9
070: */
071: public static void configure() {
072:
073: Map<String, String> props = properties.getProperties();
074: String max = props.get("jobs.maxsize");
075: if (max != null) {
076: log.info("Setting max pool size from "
077: + ((ThreadPoolExecutor) jobsExecutor)
078: .getMaximumPoolSize() + " to " + max);
079: ((ThreadPoolExecutor) jobsExecutor)
080: .setMaximumPoolSize(Integer.parseInt(max));
081: }
082: String core = props.get("jobs.coresize");
083: if (core != null) {
084: log.info("Setting core pool size from "
085: + ((ThreadPoolExecutor) jobsExecutor)
086: .getCorePoolSize() + " to " + core);
087: ((ThreadPoolExecutor) jobsExecutor).setCorePoolSize(Integer
088: .parseInt(core));
089: }
090: }
091:
092: /**
093: * @since MMBase-1.8.4
094: */
095: public static void shutdown() {
096: filterExecutor.shutdown();
097: jobsExecutor.shutdown();
098: }
099:
100: }
|