KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > rift > coad > daemon > messageservice > ProcessMonitor


1 /*
2  * MessageService: The message service daemon
3  * Copyright (C) 2007 Rift IT Contracting
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * ProcessMonitor.java
20  */

21
22 package com.rift.coad.daemon.messageservice;
23
24 // logging import
25
import org.apache.log4j.Logger;
26
27
28 /**
29  * The object responsible for monitoring the idle message processors.
30  *
31  * @author Brett Chaldecott
32  */

33 public class ProcessMonitor {
34     
35     // singleton
36
private static ProcessMonitor singleton = null;
37     
38     // the logger reference
39
protected Logger log =
40         Logger.getLogger(ProcessMonitor.class.getName());
41     
42     // private member variables
43
private boolean terminated = false;
44     
45     /**
46      * Creates a new instance of ProcessMonitor
47      */

48     private ProcessMonitor() {
49     }
50     
51     
52     /**
53      * This method returns an instance of the process monitor.
54      *
55      * @return A reference to the process monitor singleton.
56      */

57     public static synchronized ProcessMonitor getInstance() {
58         if (singleton == null) {
59             singleton = new ProcessMonitor();
60         }
61         return singleton;
62     }
63     
64     
65     /**
66      * This object will wait for the specified period of time before continuing.
67      *
68      * @param delay The length of time to delay processing.
69      */

70     public synchronized void monitor(long delay) {
71         try {
72             if (terminated) {
73                 return;
74             }
75             
76             if (delay < 0) {
77                 log.warn("Negative value of [" + delay
78                         + "] supplied to monitor");
79                 return;
80             } else if (delay == 0) {
81                 wait();
82             } else {
83                 wait(delay);
84             }
85         } catch (Exception JavaDoc ex) {
86             log.error("The monitor exited abnormally because : " +
87                     ex.getMessage(),ex);
88         }
89     }
90     
91     
92     /**
93      * This method notifies any waiting threads of the fact that they can
94      * continue processing.
95      */

96     public synchronized void notifyProcessor() {
97         notify();
98     }
99     
100     
101     /**
102      * This method terminates this object so no threads will wait on it.
103      */

104     public synchronized void terminate() {
105         terminated = true;
106         notify();
107     }
108     
109 }
110
Popular Tags