01: package example;
02:
03: import java.util.logging.Logger;
04: import java.util.logging.Level;
05:
06: import javax.resource.spi.work.Work;
07:
08: /**
09: * Implements the work task. This task just loops until the resource stops.
10: */
11: public class WorkTask implements Work {
12: private static final Logger log = Logger.getLogger(WorkTask.class
13: .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: _resource = resource;
25: }
26:
27: /**
28: * The method called to execute the task, like Runnable
29: */
30: public void run() {
31: log.fine("work starting");
32:
33: while (_isActive) {
34: log.fine("work adding count");
35:
36: _resource.addCount();
37:
38: try {
39: synchronized (this ) {
40: wait(_resource.getSleepTime());
41: }
42: } catch (Throwable e) {
43: log.log(Level.WARNING, e.toString(), e);
44: }
45: }
46:
47: log.fine("work complete");
48: }
49:
50: /**
51: * Resin will call the release() method when the server shuts down
52: * to tell the task to close.
53: */
54: public void release() {
55: _isActive = false;
56:
57: synchronized (this) {
58: notifyAll();
59: }
60: }
61: }
|