User: ejort Date: 02/04/20 04:37:49 Modified: src/main/javax/management/monitor Monitor.java Log: Improved Timer and Monitor Services Revision Changes Path 1.6 +18 -176 jmx/src/main/javax/management/monitor/Monitor.java Index: Monitor.java =================================================================== RCS file: /cvsroot/jboss/jmx/src/main/javax/management/monitor/Monitor.java,v retrieving revision 1.5 retrieving revision 1.6 diff -u -r1.5 -r1.6 --- Monitor.java 14 Apr 2002 15:22:37 -0000 1.5 +++ Monitor.java 20 Apr 2002 11:37:49 -0000 1.6 @@ -21,7 +21,8 @@ import javax.management.NotificationBroadcasterSupport; import javax.management.ObjectName; -import org.jboss.mx.util.ThreadPool; +import org.jboss.mx.util.RunnableScheduler; +import org.jboss.mx.util.SchedulableRunnable; /** * The monitor service. @@ -33,7 +34,7 @@ * </ul> * * @author <a href="mailto:[EMAIL PROTECTED]">Adrian Brock</a> - * @version $Revision: 1.5 $ + * @version $Revision: 1.6 $ */ public abstract class Monitor extends NotificationBroadcasterSupport @@ -128,50 +129,17 @@ // Static -------------------------------------------------------- /** - * The controlling thread. + * The scheduler. */ - static Controller controller = new Controller(); + static RunnableScheduler scheduler; /** - * The next run date of the next monitor. + * Start the scheduler */ - static long nextRunDate; - - /** - * The monitor runnables - */ - static Set monitors = Collections.synchronizedSet(new HashSet()); - - /** - * Add a monitor to the active list - */ - static void add(Monitor monitor) + static { - monitors.add(monitor.monitorRunnable); - } - - /** - * Remove a monitor from the active list - */ - static void remove(Monitor monitor) - { - monitors.remove(monitor.monitorRunnable); - } - - /** - * Recalculate the next run for active monitors but not running monitors. - */ - static void reschedule() - { - // Loop through the monitors to find the next run. - nextRunDate = 0; - Iterator iterator = monitors.iterator(); - while (iterator.hasNext()) - { - MonitorRunnable runnable = (MonitorRunnable) iterator.next(); - if (runnable.running == false) - runnable.reschedule(false); - } + scheduler = new RunnableScheduler(); + scheduler.start(); } // Constructors -------------------------------------------------- @@ -240,7 +208,8 @@ // Stop the monitor runnable active = false; - monitors.remove(monitorRunnable); + monitorRunnable.setScheduler(null); + monitorRunnable = null; } // MBeanRegistrationImplementation overrides --------------------- @@ -450,167 +419,40 @@ * A monitor runnable. */ private class MonitorRunnable - implements Runnable + extends SchedulableRunnable { // Attributes ---------------------------------------------------- // The monitoring to perform private Monitor monitor; - /** - * The next monitor date. - */ - long nextDate = 0; - - /** - * Is this running - */ - boolean running = false; - // Constructors -------------------------------------------------- /** * Create a monitor runnable to periodically perform monitoring. - * It will add it to the list of monitors to run. * - * @param monitor the montioring to perform. + * @param monitor the monitoring to perform. */ public MonitorRunnable(Monitor monitor) { this.monitor = monitor; - monitors.add(this); - reschedule(true); + setScheduler(scheduler); } // Public -------------------------------------------------------- - /** - * Calculate the next notification date. Add on the period until - * the number of occurences is exhausted. - * - * @return false when there are no more occurences, true otherwise. - */ - void calcNextDate() - { - // Calculate the next occurence - nextDate = System.currentTimeMillis() + monitor.getGranularityPeriod(); - } - - /** - * See whether this monitor is the next one. - * - * @param runnable the monitor runnable to check - * @param notify whether to notify the controller of changes - */ - void reschedule(boolean notify) - { - synchronized(monitors) - { - if (nextRunDate == 0 || nextDate < nextRunDate) - { - nextRunDate = nextDate; - if (notify == true) - monitors.notify(); - } - } - } - - // Runnable overrides ------------------------------------------- + // SchedulableRunnable overrides --------------------------------- /** * Run the montior */ - public void run() + public void doRun() { // Perform the monitoring monitor.runMonitor(); - // Calculate the next date - calcNextDate(); - - // We've finished with this notification - running = false; - - // Work out if this is the next to run - reschedule(true); + // Reschedule + setNextRun(System.currentTimeMillis() + monitor.granularityPeriod); } - } - - /** - * The controlling thread. - */ - private static class Controller - extends Thread - { - // Attributes ---------------------------------------------------- - - /** - * The thread pool used to obtain threads to run monitors. - */ - ThreadPool threadPool; - - // Constructors -------------------------------------------------- - - /** - * This controller runs in a different thread as a daemon. - * It has a thread pool to run monitors. - */ - public Controller() - { - super(); - setDaemon(true); - threadPool = new ThreadPool(); - threadPool.setActive(true); - super.start(); - } - - // Public -------------------------------------------------------- - - // Thread Overrides----------------------------------------------- - - /** - * Wait until the next run date or until we are interrupted by - * a reschedule. When active, run the monitor - * to see whether notifications should be sent. - */ - public void run() - { - while (true) - { - synchronized (monitors) - { - Iterator iterator = monitors.iterator(); - while (iterator.hasNext()) - { - MonitorRunnable runnable = (MonitorRunnable) iterator.next(); - - // Run required? - if (runnable.running == false - && runnable.nextDate <= System.currentTimeMillis()) - { - runnable.running = true; - threadPool.run(runnable); - } - } - - // Reschedule the waiting monitors - reschedule(); - - // Wait until the next monitor required or we are interrupted - try - { - if (nextRunDate == 0) - monitors.wait(999999); - else - { - long waitMillis = nextRunDate - System.currentTimeMillis(); - if (waitMillis > 0) - monitors.wait(waitMillis); - } - } - catch (InterruptedException ignored) {} - } - } - } } }
_______________________________________________ Jboss-development mailing list [EMAIL PROTECTED] https://lists.sourceforge.net/lists/listinfo/jboss-development