01: package example;
02:
03: import java.util.logging.Logger;
04:
05: import javax.resource.spi.ResourceAdapter;
06: import javax.resource.spi.ResourceAdapterInternalException;
07: import javax.resource.spi.BootstrapContext;
08:
09: import javax.resource.spi.work.Work;
10: import javax.resource.spi.work.WorkManager;
11: import javax.resource.spi.work.WorkException;
12:
13: import com.caucho.jca.AbstractResourceAdapter;
14:
15: /**
16: * Implements a resource which uses uses Work management for
17: * separate threading.
18: */
19: public class WorkResource extends AbstractResourceAdapter {
20: private static final Logger log = Logger
21: .getLogger(WorkResource.class.getName());
22:
23: // The time in milliseconds the resource should sleep
24: private long _sleepTime = 10000L;
25:
26: // The count of times the server has looped
27: private int _count;
28:
29: /**
30: * Returns the sleep time.
31: */
32: public long getSleepTime() {
33: return _sleepTime;
34: }
35:
36: /**
37: * Adds to the count.
38: */
39: public void addCount() {
40: _count++;
41: }
42:
43: /**
44: * The start method is called when the resource adapter starts, i.e.
45: * when the web-app or host initializes.
46: */
47: public void start(BootstrapContext ctx)
48: throws ResourceAdapterInternalException {
49: log.info("WorkResource[] starting");
50:
51: WorkManager workManager = ctx.getWorkManager();
52:
53: Work work = new WorkTask(this );
54:
55: try {
56: // Submits the work, but does not wait for the result.
57: // In other words, it spawns a new thread
58: workManager.startWork(work);
59: } catch (WorkException e) {
60: throw new ResourceAdapterInternalException(e);
61: }
62: }
63:
64: /**
65: * Called when the resource adapter is stopped, i.e. when the
66: * web-app or host closes down.
67: */
68: public void stop() throws ResourceAdapterInternalException {
69: log.info("Resource[" + _count + "] stopping");
70: }
71:
72: /**
73: * Returns a printable version of the resource.
74: */
75: public String toString() {
76: return "WorkResource[" + _count + "]";
77: }
78: }
|