1 22 package org.jboss.ejb3.timerservice.quartz; 23 24 import java.io.Serializable ; 25 import java.util.Collection ; 26 import java.util.Date ; 27 28 import javax.ejb.EJBException ; 29 import javax.ejb.Timer ; 30 import javax.ejb.TimerService ; 31 import javax.management.ObjectName ; 32 33 import org.jboss.ejb3.timerservice.TimedObjectInvoker; 34 import org.jboss.logging.Logger; 35 import org.quartz.JobDetail; 36 import org.quartz.Scheduler; 37 import org.quartz.SchedulerException; 38 import org.quartz.SimpleTrigger; 39 import org.quartz.Trigger; 40 41 49 public class TimerServiceImpl implements TimerService 50 { 51 private static final Logger log = Logger.getLogger(TimerServiceImpl.class); 52 53 private Scheduler scheduler; 54 private ObjectName objectName; 55 private String groupName; 56 private long jobNum = 0; 57 private long triggerNum = 0; 58 59 protected TimerServiceImpl(Scheduler scheduler, ObjectName objectName, TimedObjectInvoker invoker) { 60 assert scheduler != null; 61 assert objectName != null; 62 assert invoker != null; 63 64 this.scheduler = scheduler; 65 this.objectName = objectName; 66 this.groupName = objectName.getCanonicalName(); 67 } 68 69 protected Timer createTimer(Trigger trigger, Serializable info) 70 { 71 try { 72 String name = "myJob" + jobNum; 73 jobNum++; 74 75 Class jobClass = QuartzTimerJob.class; 76 77 Timer timer = new TimerImpl(scheduler, trigger, info); 78 79 PersistentTimer persistentTimer = new PersistentTimer(trigger, objectName, info); 80 81 JobDetail jobDetail = new JobDetail(name, groupName, jobClass); 82 jobDetail.getJobDataMap().put("timer", persistentTimer); 83 84 scheduler.scheduleJob(jobDetail, trigger); 85 86 return timer; 87 } 88 catch(SchedulerException e) { 89 log.error("createTimer failed", e); 91 throw new EJBException (e.getMessage()); 92 } 93 94 } 95 96 106 public Timer createTimer(long duration, Serializable info) throws IllegalArgumentException , IllegalStateException , 107 EJBException 108 { 109 if(duration < 0) throw new IllegalArgumentException ("duration must not be negative"); 110 112 Date expiration = new Date (System.currentTimeMillis() + duration); 113 return createTimer(expiration, info); 114 } 115 116 128 public Timer createTimer(long initialDuration, long intervalDuration, Serializable info) 129 throws IllegalArgumentException , IllegalStateException , EJBException 130 { 131 if(initialDuration < 0) throw new IllegalArgumentException ("initialDuration must not be negative"); 132 if(intervalDuration < 0) throw new IllegalArgumentException ("intervalDuration must not be negative"); 133 135 Date initialExpiration = new Date (System.currentTimeMillis() + initialDuration); 136 137 return createTimer(initialExpiration, intervalDuration, info); 138 } 139 140 150 public Timer createTimer(Date expiration, Serializable info) throws IllegalArgumentException , IllegalStateException , 151 EJBException 152 { 153 if(expiration == null) throw new IllegalArgumentException ("expiration must not be null"); 154 if(expiration.getTime() < 0) throw new IllegalArgumentException ("expiration.time must not be negative"); 155 157 String triggerName = "myTrigger" + triggerNum; 158 triggerNum++; 159 160 Trigger trigger = new SimpleTrigger(triggerName, groupName, expiration); 161 162 return createTimer(trigger, info); 163 } 164 165 176 public Timer createTimer(Date initialExpiration, long intervalDuration, Serializable info) 177 throws IllegalArgumentException , IllegalStateException , EJBException 178 { 179 if(initialExpiration == null) throw new IllegalArgumentException ("initialExpiration must not be null"); 180 if(initialExpiration.getTime() < 0) throw new IllegalArgumentException ("initialExpiration.time must not be negative"); 181 if(intervalDuration < 0) throw new IllegalArgumentException ("intervalDuration must not be negative"); 182 184 String triggerName = "myTrigger" + triggerNum; 185 triggerNum++; 186 Date endTime = null; 187 188 Trigger trigger = new SimpleTrigger(triggerName, groupName, initialExpiration, endTime, SimpleTrigger.REPEAT_INDEFINITELY, intervalDuration); 189 190 return createTimer(trigger, info); 191 } 192 193 protected Scheduler getScheduler() 194 { 195 return scheduler; 196 } 197 198 205 public Collection getTimers() throws IllegalStateException , EJBException 206 { 207 throw new RuntimeException ("NYI"); 208 } 209 210 protected void shutdown() 211 { 212 log.debug("shutting down " + this); 213 try 214 { 215 String triggerNames[] = scheduler.getTriggerNames(groupName); 216 for(String triggerName : triggerNames) 217 scheduler.unscheduleJob(triggerName, groupName); 218 String jobNames[] = scheduler.getJobNames(groupName); 219 for(String jobName : jobNames) 220 scheduler.deleteJob(jobName, groupName); 221 } 222 catch(SchedulerException e) 223 { 224 log.error("shutdown failed", e); 225 } 227 } 228 229 public String toString() 230 { 231 return "Timer Service " + objectName; 232 } 233 } 234 | Popular Tags |