KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > example > WorkTask


1 package example;
2
3 import java.util.logging.Logger JavaDoc;
4 import java.util.logging.Level JavaDoc;
5
6 import javax.resource.spi.work.Work JavaDoc;
7
8 /**
9  * Implements the work task. This task just loops until the resource stops.
10  */

11 public class WorkTask implements Work JavaDoc {
12   private static final Logger JavaDoc log =
13     Logger.getLogger(WorkTask.class.getName());
14
15   private WorkResource _resource;
16
17   // main lifecycle variable
18
private volatile boolean _isActive = true;
19
20   /**
21    * Creates the work task.
22    */

23   WorkTask(WorkResource resource)
24   {
25     _resource = resource;
26   }
27
28   /**
29    * The method called to execute the task, like Runnable
30    */

31   public void run()
32   {
33     log.fine("work starting");
34       
35     while (_isActive) {
36       log.fine("work adding count");
37       
38       _resource.addCount();
39
40       try {
41     synchronized (this) {
42       wait(_resource.getSleepTime());
43     }
44       } catch (Throwable JavaDoc e) {
45     log.log(Level.WARNING, e.toString(), e);
46       }
47     }
48     
49     log.fine("work complete");
50   }
51
52   /**
53    * Resin will call the release() method when the server shuts down
54    * to tell the task to close.
55    */

56   public void release()
57   {
58     _isActive = false;
59     
60     synchronized (this) {
61       notifyAll();
62     }
63   }
64 }
65
Popular Tags