1 package example; 2 3 import java.util.Timer ; 4 import java.util.TimerTask ; 5 6 import java.util.logging.Logger ; 7 8 import javax.resource.spi.ResourceAdapter ; 9 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 24 public class TimerResource extends AbstractResourceAdapter { 25 private static final Logger log = 26 Logger.getLogger(TimerResource.class.getName()); 27 28 private long _initialDelay = 0; 30 31 private long _period = 10000L; 33 34 private int _count; 36 37 private Timer _timer; 38 39 43 public void setInitialDelay(Period initialDelay) 44 { 45 _initialDelay = initialDelay.getPeriod(); 46 } 47 48 52 public void setPeriod(Period period) 53 { 54 _period = period.getPeriod(); 55 } 56 57 60 public void addCount() 61 { 62 _count++; 63 } 64 65 69 public void start(BootstrapContext ctx) 70 throws ResourceAdapterInternalException 71 { 72 log.info("WorkResource[] starting"); 73 74 WorkManager workManager = ctx.getWorkManager(); 75 76 Work work = new WorkTask(this); 77 78 TimerTask timerTask = new WorkScheduleTimerTask(workManager, work); 79 80 _timer = ctx.createTimer(); 81 82 _timer.schedule(timerTask, _initialDelay, _period); 83 } 84 85 89 public void stop() 90 throws ResourceAdapterInternalException 91 { 92 log.info("Resource[" + _count + "] stopping"); 93 94 _timer.cancel(); 95 } 96 97 100 public String toString() 101 { 102 return "WorkResource[" + _count + "]"; 103 } 104 } 105 | Popular Tags |