1 package example; 2 3 import java.util.logging.Logger; 4 5 import javax.resource.spi.ResourceAdapter; 6 import javax.resource.spi.ResourceAdapterInternalException; 7 import javax.resource.spi.BootstrapContext; 8 9 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 = 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 ctx) 50 throws ResourceAdapterInternalException 51 { 52 log.info("WorkResource[] starting"); 53 54 WorkManager workManager = ctx.getWorkManager(); 55 56 Work 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 e) { 63 throw new ResourceAdapterInternalException(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 73 { 74 log.info("Resource[" + _count + "] stopping"); 75 } 76 77 /** 78 * Returns a printable version of the resource. 79 */ 80 public String toString() 81 { 82 return "WorkResource[" + _count + "]"; 83 } 84 } 85