KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > example > WorkResource


1 package example;
2
3 import java.util.logging.Logger JavaDoc;
4
5 import javax.resource.spi.ResourceAdapter JavaDoc;
6 import javax.resource.spi.ResourceAdapterInternalException JavaDoc;
7 import javax.resource.spi.BootstrapContext JavaDoc;
8
9 import javax.resource.spi.work.Work JavaDoc;
10 import javax.resource.spi.work.WorkManager JavaDoc;
11 import javax.resource.spi.work.WorkException JavaDoc;
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 JavaDoc log =
21     Logger.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   {
34     return _sleepTime;
35   }
36   
37   /**
38    * Adds to the count.
39    */

40   public void addCount()
41   {
42     _count++;
43   }
44   
45   /**
46    * The start method is called when the resource adapter starts, i.e.
47    * when the web-app or host initializes.
48    */

49   public void start(BootstrapContext JavaDoc ctx)
50     throws ResourceAdapterInternalException JavaDoc
51   {
52     log.info("WorkResource[] starting");
53
54     WorkManager JavaDoc workManager = ctx.getWorkManager();
55     
56     Work JavaDoc work = new WorkTask(this);
57
58     try {
59       // Submits the work, but does not wait for the result.
60
// In other words, it spawns a new thread
61
workManager.startWork(work);
62     } catch (WorkException JavaDoc e) {
63       throw new ResourceAdapterInternalException JavaDoc(e);
64     }
65   }
66   
67   /**
68    * Called when the resource adapter is stopped, i.e. when the
69    * web-app or host closes down.
70    */

71   public void stop()
72     throws ResourceAdapterInternalException JavaDoc
73   {
74     log.info("Resource[" + _count + "] stopping");
75   }
76
77   /**
78    * Returns a printable version of the resource.
79    */

80   public String JavaDoc toString()
81   {
82     return "WorkResource[" + _count + "]";
83   }
84 }
85
Popular Tags