KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > module > builders > Vwmtasks


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10 package org.mmbase.module.builders;
11
12 import java.util.*;
13
14 import org.mmbase.module.core.*;
15 import org.mmbase.util.*;
16 import org.mmbase.util.logging.*;
17
18 /**
19  * Virtual Web Master task builder.
20  * This builder holds the tasks that need to be performed by the various VWMs (which are registered in
21  * the VWMS builder). It also runs a taskscheduler that periodically cehcks whether any new tasks need to be handled<br />
22  * Task nodes have a number of fields that define if, when, and how a task is to be performed.
23  * These fields are : <br/>
24  * Requested Machine: the machine on which the task is to be performed (wantedcpu)<br />
25  * VWM: the name of the vwm (as known in the VWMS builder) trhat should perform the task<br />
26  * Status : the state of the task (i.e. requesting, claimed, error, done) <br />
27  * Claiming Machine : the cpu that claimed the task for performance (claimedcpu)<br />
28  * Wanted Time : time at which to start the task<br />
29  * Changed Time : time at which the task was last changed<br />
30  * Expire Time : time at which the task expires<br />
31  * Task : name of the task<br />
32  * Id : unknown<br />
33  * Data : field for including properties/parameters<br />
34  *
35  * @application VWMs
36  * @rename VwmTasks
37  * @author Arjan Houtman
38  * @author Pierre van Rooden (javadocs)
39  * @version $Id: Vwmtasks.java,v 1.18 2005/01/25 12:45:18 pierre Exp $
40  */

41 public class Vwmtasks extends MMObjectBuilder implements Runnable JavaDoc {
42     /**
43     * Status value for a task that requests to be performed
44     */

45     public static final int STATUS_REQUEST = 1;
46     /**
47     * Status value for a task that is being performed
48     */

49     public static final int STATUS_CLAIMED = 2;
50     /**
51     * Status value for a task that was succesfully performed
52     */

53     public static final int STATUS_DONE = 3;
54     /**
55     * Status value for a task that timed out
56     */

57     public static final int STATUS_TIMEOUT = 4;
58     /**
59     * Status value for a task that failed to be performed
60     */

61     public static final int STATUS_ERROR = 5;
62
63     // Logger class
64
private static Logger log = Logging.getLoggerInstance(Vwmtasks.class.getName());
65
66     /**
67     * The interval (in seconds) between checks for new nodes by the task scheduler.
68     */

69     public static final int SLEEP_TIME= 37;
70
71     // unused
72
public boolean replaceCache=true;
73
74     /**
75      * Thread in which the task scheduler runms
76      */

77     Thread JavaDoc kicker;
78
79     /**
80      * Reference to the VWMs builder
81      */

82     Vwms vwms;
83
84     // unused
85
Hashtable vwm_cache = new Hashtable ();
86
87     /**
88      * Time (in seconds since 1/1/1970) that the builder last checked for new tasknodes to en handled
89      */

90     int lastchecked=0;
91
92     public boolean init () {
93         if (oType != -1) {
94             return true;
95         } else {
96             boolean success = super.init ();
97             if (success) {
98                 start();
99             }
100             return success;
101         }
102     }
103
104     /**
105      * Starts the thread for the task scheduler
106      */

107     public void start() {
108         /* Start up the main thread */
109         if (kicker == null) {
110             kicker = new Thread JavaDoc(this,"VwmTasks");
111             kicker.setDaemon(true);
112             kicker.start();
113         }
114     }
115
116     /**
117      * Stops the thread for the task scheduler
118      * Sets the kicker field to null, which causes the run method to terminate.
119      */

120     public void stop() {
121         /* Stop thread */
122         kicker.interrupt();
123         kicker = null;
124     }
125
126     /**
127      * VWM maintenance scheduler.
128      * Calls the {@link #getVwmTasks} method, after which the thread sleeps
129      * for a number of seconds as set in {@link #SLEEP_TIME}.
130      */

131     public void run() {
132         log.info("Thread started, entering while loop");
133
134         while (kicker!=null) {
135             log.service("Periodically sleep "+SLEEP_TIME
136                 +" seconds and add all new vwmtasks that were created since last check ("
137                 +DateSupport.date2string(lastchecked)+").");
138             try {Thread.sleep(SLEEP_TIME*1000);} catch (InterruptedException JavaDoc e){return;}
139             getVwmTasks();
140         }
141     }
142
143     /**
144      * This method is called just before an actual write to the database is performed.
145      * It sets the last changed time to the current time (so the scheduler will select this node).
146      * @param node The node to be committed.
147      * @return the node to be committed (after changes have been made).
148      */

149     public MMObjectNode preCommit(MMObjectNode node) {
150         node.setValue("changedtime",(int)(System.currentTimeMillis()/1000));
151         return node;
152     }
153
154     /**
155      * What should a GUI display for this node.
156      * Returns the contents of the task field (the task's description).
157      * @param node The node to display
158      * @return the display of the node as a <code>String</code>
159      */

160     public String JavaDoc getGUIIndicator (MMObjectNode node) {
161         String JavaDoc str = node.getStringValue ("task");
162         if (str.length () > 15) {
163             return str.substring (0,12) + "...";
164         } else {
165             return str;
166         }
167     }
168
169     /**
170      * What should a GUI display for this node/field combo.
171      * Returns a descriptive text for the status field, or a formatted date struing in case of a time-value.
172      * @param node The node to display
173      * @param field the name field of the field to display
174      * @return the display of the node's field as a <code>String</code>, null if not specified
175      */

176     public String JavaDoc getGUIIndicator (String JavaDoc field, MMObjectNode node) {
177         if (field.equals ("status")) {
178             int val = node.getIntValue ("status");
179
180             if (val==STATUS_REQUEST) {
181                 return "verzoek"; // return "request";
182
} else if (val==STATUS_CLAIMED) {
183                 return "onderweg"; // return "claimed";
184
} else if (val==STATUS_DONE) {
185                 return "gedaan"; // return "done";
186
} else if (val==STATUS_TIMEOUT) {
187                 return "timeout";
188             } else if (val==STATUS_ERROR) {
189                 return "error emailed";
190             } else {
191                 return "unknown";
192             }
193         } else if (field.equals("changedtime")) {
194             int str=node.getIntValue("changedtime");
195             return DateSupport.getTimeSec(str)+" op "+DateSupport.getMonthDay(str)+"/"+DateSupport.getMonth(str)+"/"+DateSupport.getYear(str);
196         } else if (field.equals("wantedtime")) {
197             int str=node.getIntValue("wantedtime");
198             return DateSupport.getTimeSec(str)+" op "+DateSupport.getMonthDay(str)+"/"+DateSupport.getMonth(str)+"/"+DateSupport.getYear(str);
199         } else if (field.equals("expiretime")) {
200             int str=node.getIntValue("expiretime");
201             return DateSupport.getTimeSec(str)+" op "+DateSupport.getMonthDay(str)+"/"+DateSupport.getMonth(str)+"/"+DateSupport.getYear(str);
202         }
203         return null;
204     }
205
206     /**
207      * Task scheduling routine.
208      * This routine selects all tasks that are to be performed.
209      * In order to be selected, a task should be new or recently changed, it's status should indicate that it needs a request, and
210      * the desired machine to execute the code should match this machine.
211      * The tasks are passed to the VWM that is associated with the task.
212      */

213     protected void getVwmTasks() {
214         String JavaDoc vwm,task;
215         // get out alter ego Vwms Builder to pass the new tasks
216
if (vwms==null) {
217             vwms = (Vwms)mmb.getMMObject("vwms");
218             vwms.init(); // make sure it is initialized
219
}
220         int checktime = lastchecked;
221         lastchecked= (int)(System.currentTimeMillis()/1000);
222         //Enumeration e=search("WHERE changedtime>"+checktime+" AND wantedcpu='"+getMachineName()+"' AND status=1");
223
log.service("Search vwmtasks "+"WHERE changedtime>"+checktime
224                 +" AND wantedcpu='"+getMachineName()+"'"
225                 +" AND "+mmb.getStorageManagerFactory().getStorageIdentifier("status")+"="+STATUS_REQUEST);
226         Enumeration e = search("WHERE changedtime>"+checktime
227                 +" AND wantedcpu='"+getMachineName()+"'"
228                 +" AND "+mmb.getStorageManagerFactory().getStorageIdentifier("status")+"="+STATUS_REQUEST);
229
230         for (MMObjectNode node=null; e.hasMoreElements();) {
231             node = (MMObjectNode)e.nextElement();
232             vwm = node.getStringValue("vwm");
233             task = node.getStringValue("task");
234             log.debug("Adding "+vwm+" tasknode "+node);
235             vwms.putTask(vwm,node);
236         }
237     }
238 }
239
Popular Tags