KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > module > builders > vwms > VwmProbe


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.vwms;
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  * admin module, keeps track of all the worker pools
20  * and adds/kills workers if needed (depending on
21  * there load and info from the config module).
22  *
23  * @application VWMs
24  * @author Daniel Ockeloen
25  * @version $Id: VwmProbe.java,v 1.11 2004/10/08 10:57:57 pierre Exp $ current version $Id: VwmProbe.java,v 1.11 2004/10/08 10:57:57 pierre Exp $
26  */

27 public class VwmProbe implements Runnable JavaDoc {
28
29     // logging variable
30
private static Logger log = Logging.getLoggerInstance(VwmProbe.class.getName());
31
32     Thread JavaDoc kicker = null;
33     VwmProbeInterface parent=null;
34     SortedVector tasks= new SortedVector(new MMObjectCompare("wantedtime"));
35     // Active Node
36
MMObjectNode anode=null;
37     PerformProbe pp;
38     private final static int TASK_PICKUP_TIMEDIFFERENCE = 3;
39
40     public VwmProbe(VwmProbeInterface parent) {
41         this.parent=parent;
42         init();
43     }
44
45     public void init() {
46         this.start();
47     }
48
49
50     /**
51      * Starts the admin Thread.
52      */

53     public void start() {
54         /* Start up the main thread */
55         if (kicker == null) {
56             kicker = new Thread JavaDoc(this,"Vwmprobe");
57             kicker.setDaemon(true);
58             kicker.start();
59         }
60     }
61
62     /**
63      * Stops the admin Thread.
64      */

65     public void stop() {
66         /* Stop thread */
67         kicker.interrupt();
68         kicker = null;
69     }
70
71     /**
72      * blocked on the first task in the queue
73      */

74     public synchronized void run() {
75         kicker.setPriority(Thread.MIN_PRIORITY+1);
76         log.info("Probe thread started, checking for tasks.");
77         while (kicker!=null) {
78             log.service("Tasks vector:"+tasks+" size:"+tasks.size());
79             if (tasks.size()>0) {
80                 anode=(MMObjectNode)tasks.elementAt(0);
81                 log.service("Getting task at pos 0 in vector, number:"
82                 +anode.getIntValue("number")+" task:"+anode.getStringValue("task"));
83             } else {
84                 anode=null;
85             }
86             try {
87                 if (anode==null) {
88                     // so no task in the future wait a long time then
89
log.info("No tasks, anode=null, waiting 1 hour.");
90                     wait(3600*1000);
91                 } else {
92                     int curTime=(int)((System.currentTimeMillis()/1000));
93                     int timeDifference = anode.getIntValue("wantedtime") - curTime;
94                     if (timeDifference<TASK_PICKUP_TIMEDIFFERENCE) {
95                         log.service("Difference between curtime and task starttime"
96                         +" (curtime - starttime)"
97                         +" is smaller than "+TASK_PICKUP_TIMEDIFFERENCE
98                         +" seconds, it is "+timeDifference+" handle Task NOW");
99                         try {
100                             // parent.performTask(anode);
101
pp=new PerformProbe(parent,anode);
102                         } catch (RuntimeException JavaDoc e) {
103                             log.error("performTask failed "+anode+" : "+e);
104                             log.error(e.getMessage());
105                             log.error(Logging.stackTrace(e));
106                         }
107                         log.service("Removing task number:"+anode.getIntValue("number")
108                         +" task:"+anode.getStringValue("task"));
109                         tasks.removeElement(anode);
110                     } else {
111                         log.service("Task starttime is still further than "
112                         +TASK_PICKUP_TIMEDIFFERENCE+" seconds away, "
113                         +"waiting "+timeDifference+" seconds, before getting task again");
114                         wait(timeDifference*1000);
115                     }
116                 }
117             } catch (InterruptedException JavaDoc e){}
118         }
119     }
120
121     /**
122      * Puts a task node to the vector (sorted on task start time) of new tasks.
123      * If the tasks vector already contains the node, it will be replaced.
124      * @param node task node
125      * @return true always.
126      */

127     public synchronized boolean putTask(MMObjectNode node) {
128         boolean res;
129         if (!containsTask(node)) {
130             tasks.addSorted(node);
131             res=true;
132         } else {
133             res=replaceTask(node);
134         }
135
136         if (tasks.size()==0) {
137             // notiy when tasks size is 0 ?
138
log.service("Tasks vector size is 0, calling notify()");
139             notify();
140         } else if (node==tasks.elementAt(0)) {
141             // node is first in tasks vector.
142
log.service("Node "+node.getIntValue("number")
143             +" task "+node.getStringValue("task")+" calling notify()");
144             notify();
145         }
146         // huh ?!#
147
return true;
148     }
149
150     /**
151      * Checks if a task node already exists in the task nodes vector.
152      * @param node task node
153      * @return true if the nodes objectnr matches a node objectnr
154      * in the vector, false if tasks vector is empty or doesn't contain the node.
155      */

156     public boolean containsTask(MMObjectNode node) {
157         int number = node.getIntValue("number");
158         Enumeration e = tasks.elements();
159         if (!e.hasMoreElements()) {
160             log.info("Task nodes vector is empty.");
161             return false;
162         }
163         for (MMObjectNode nodeFromTasksVec=null; e.hasMoreElements();) {
164             nodeFromTasksVec = (MMObjectNode)e.nextElement();
165             if (number==nodeFromTasksVec.getIntValue("number"))
166                 return true;
167         }
168         return false;
169     }
170
171     /**
172      * Replaces a task node entry in the sorted task nodes vector with a new one.
173      * @param node task node
174      * @return true if task node was found and replaced,
175      * false if tasks vector is empty or doesn't contain the node.
176      */

177     public boolean replaceTask(MMObjectNode node) {
178         int number = node.getIntValue("number");
179         Enumeration e = tasks.elements();
180         if (!e.hasMoreElements()) {
181             log.warn("Task nodes vector is empty.");
182             return false;
183         }
184         for (MMObjectNode nodeFromTasksVec=null; e.hasMoreElements();) {
185             nodeFromTasksVec = (MMObjectNode)e.nextElement();
186             if (number==nodeFromTasksVec.getIntValue("number")) {
187                 tasks.removeElement(nodeFromTasksVec);
188                 tasks.addSorted(node);
189                 return true;
190             }
191         }
192         return false;
193     }
194 }
Popular Tags