[CJ-dev] commonjava-projects/commonjava-bgthreads/src/java/org/commonjava/bgthreads BackgroundServic
Brought to you by:
johnqueso
From: <joh...@co...> - 2004-02-16 06:42:33
|
Update of /cvsroot/commonjava/commonjava-projects/commonjava-bgthreads/src/java/org/commonjava/bgthreads In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv27538/src/java/org/commonjava/bgthreads Modified Files: BackgroundServiceManager.java Log Message: added OPL configuration support, and renamed the project to simply be Background Threads. Index: BackgroundServiceManager.java =================================================================== RCS file: /cvsroot/commonjava/commonjava-projects/commonjava-bgthreads/src/java/org/commonjava/bgthreads/BackgroundServiceManager.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -d -r1.1 -r1.2 --- BackgroundServiceManager.java 18 Sep 2003 00:23:20 -0000 1.1 +++ BackgroundServiceManager.java 16 Feb 2004 06:34:46 -0000 1.2 @@ -12,18 +12,16 @@ import org.commonjava.util.PriorityQueue; import org.commonjava.util.Queue; import org.commonjava.util.ThreadThrottle; -import org.commonjava.config.Configuration; -import org.commonjava.config.ConfigurationEvent; -import org.commonjava.config.ConfigurationListener; -import org.commonjava.config.ConfigurationManager; -import org.commonjava.config.ConfigurationManagerException; +import org.commonjava.bgthreads.config.BGServiceMgrConfig; +import org.commonjava.config.snapin.SnapInContainerLoader; +import org.commonjava.config.snapin.SnapInLoaderException; /** Manager class for all services to be run in a background thread. This class is * intended to provide thread throttling en masse to all backgrounded threads. * * @author John Casey */ -public class BackgroundServiceManager implements ConfigurationListener{ +public class BackgroundServiceManager{ private static final Log LOG = LogFactory.getLog(BackgroundServiceManager.class); @@ -37,7 +35,6 @@ Queue.OptimizedAction.PUSH ); - private Configuration config; private long threadWaitMillis; private long threadJoinMillis; @@ -45,12 +42,21 @@ /** Deny instantiation of BackgroundServiceManager */ private BackgroundServiceManager() { - ConfigurationManager.getInstance().addConfigurationListener( - BackgroundServiceConstants.CONFIG_FILE, - this + BGServiceMgrConfig config = loadConfig(); + this.throttle = new ThreadThrottle( + config.getMaxPending(), + config.getMaxRunning(), + config.getWaitMillis() ); - doInit(); + this.servicer = new BackgroundServiceManager.Servicer( + serviceQueue, throttle, config.getWaitMillis() + ); + + this.servicerThread = throttle.getThread(servicer); + this.servicerThread.setPriority(Thread.currentThread().getPriority()-1); + this.servicerThread.setDaemon(true); + this.servicerThread.start(); } /** Return the singleton instance of the manager. @@ -84,68 +90,23 @@ servicer.doResume(); } - /** Initialize the manager. - */ - private synchronized void doInit(){ + private BGServiceMgrConfig loadConfig(){ + BGServiceMgrConfig config = null; try{ - config = ConfigurationManager.getInstance().getConfiguration( - BackgroundServiceConstants.CONFIG_FILE + config = (BGServiceMgrConfig)SnapInContainerLoader.getSnapInContainer().getSnapIn( + BGServiceMgrConfig.SNAP_IN_ID ); - } - catch(ConfigurationManagerException ex){ - if(LOG.isInfoEnabled()){ - LOG.info(BackgroundServiceConstants.CONFIG_FILE + " cannot be loaded. Initializing with defaults."); + } catch (SnapInLoaderException e) { + if (LOG.isInfoEnabled()) { + LOG.info( + "Error loading background services configuration from snap-in container. Using defaults.", + e + ); + config = new BGServiceMgrConfig(); } } - if(config == null){config = new Configuration();} - - if(throttle == null){ - int pending = config.getIntegerProperty( - BackgroundServiceConstants.MAX_PENDING_THREADS, - BackgroundServiceConstants.DEFAULT_MAX_PENDING_THREADS - ); - - int running = config.getIntegerProperty( - BackgroundServiceConstants.MAX_RUNNING_THREADS, - BackgroundServiceConstants.DEFAULT_MAX_RUNNING_THREADS - ) + 1; - - int priority = config.getIntegerProperty( - BackgroundServiceConstants.THREAD_PRIORITY, - Thread.currentThread().getPriority()-1 - ); - - throttle = new ThreadThrottle(pending, running, priority); - - this.threadWaitMillis = config.getLongProperty( - BackgroundServiceConstants.THREAD_WAIT_MILLIS, - BackgroundServiceConstants.DEFAULT_THREAD_WAIT_MILLIS - ); - - this.threadJoinMillis = config.getLongProperty( - BackgroundServiceConstants.THREAD_JOIN_MILLIS, - BackgroundServiceConstants.DEFAULT_THREAD_JOIN_MILLIS - ); - - this.servicer = new BackgroundServiceManager.Servicer( - serviceQueue, throttle, threadWaitMillis - ); - - this.servicerThread = throttle.getThread(servicer); - this.servicerThread.setPriority(priority); - this.servicerThread.setDaemon(true); - this.servicerThread.start(); - } - } - - /** Signals that a configuration file that this listener is bount to - * has changed. - * @param evt The ConfigurationEvent triggering this callback. - * - */ - public void configurationChanged(ConfigurationEvent evt) { - doInit(); + return config; } /** Runnable that handles the execution of background services. |