01: package example;
02:
03: import java.util.Timer;
04: import java.util.TimerTask;
05:
06: import java.util.logging.Logger;
07:
08: import javax.resource.spi.ResourceAdapter;
09: import javax.resource.spi.ResourceAdapterInternalException;
10: import javax.resource.spi.BootstrapContext;
11:
12: import javax.resource.spi.work.Work;
13: import javax.resource.spi.work.WorkManager;
14: import javax.resource.spi.work.WorkException;
15:
16: import com.caucho.jca.AbstractResourceAdapter;
17:
18: import com.caucho.config.types.Period;
19:
20: /**
21: * Implements a resource which uses uses Work management for
22: * separate threading.
23: */
24: public class TimerResource extends AbstractResourceAdapter {
25: private static final Logger log = Logger
26: .getLogger(TimerResource.class.getName());
27:
28: // The initial delay of the task.
29: private long _initialDelay = 0;
30:
31: // The period of the task
32: private long _period = 10000L;
33:
34: // The count of times the server has looped
35: private int _count;
36:
37: private Timer _timer;
38:
39: /**
40: * Sets the initial delay using the Resin-specific period notation:
41: * 10s, 10m, etc.
42: */
43: public void setInitialDelay(Period initialDelay) {
44: _initialDelay = initialDelay.getPeriod();
45: }
46:
47: /**
48: * Sets the period using the Resin-specific period notation:
49: * 10s, 10m, etc.
50: */
51: public void setPeriod(Period period) {
52: _period = period.getPeriod();
53: }
54:
55: /**
56: * Adds to the count.
57: */
58: public void addCount() {
59: _count++;
60: }
61:
62: /**
63: * The start method is called when the resource adapter starts, i.e.
64: * when the web-app or host initializes.
65: */
66: public void start(BootstrapContext ctx)
67: throws ResourceAdapterInternalException {
68: log.info("WorkResource[] starting");
69:
70: WorkManager workManager = ctx.getWorkManager();
71:
72: Work work = new WorkTask(this );
73:
74: TimerTask timerTask = new WorkScheduleTimerTask(workManager,
75: work);
76:
77: _timer = ctx.createTimer();
78:
79: _timer.schedule(timerTask, _initialDelay, _period);
80: }
81:
82: /**
83: * Called when the resource adapter is stopped, i.e. when the
84: * web-app or host closes down.
85: */
86: public void stop() throws ResourceAdapterInternalException {
87: log.info("Resource[" + _count + "] stopping");
88:
89: _timer.cancel();
90: }
91:
92: /**
93: * Returns a printable version of the resource.
94: */
95: public String toString() {
96: return "WorkResource[" + _count + "]";
97: }
98: }
|