1 22 package org.jboss.ejb3.timerservice.quartz; 23 24 import java.io.Serializable ; 25 import java.util.Date ; 26 27 import javax.ejb.EJBException ; 28 import javax.ejb.NoSuchObjectLocalException ; 29 import javax.ejb.Timer ; 30 import javax.ejb.TimerHandle ; 31 32 import org.jboss.logging.Logger; 33 import org.quartz.Scheduler; 34 import org.quartz.SchedulerException; 35 import org.quartz.Trigger; 36 37 45 public class TimerImpl implements Timer 46 { 47 private static final Logger log = Logger.getLogger(TimerImpl.class); 48 49 private Scheduler scheduler; 50 private Trigger trigger; 51 private Serializable info; 52 53 protected TimerImpl(Scheduler scheduler, Trigger trigger, Serializable info) { 54 assert scheduler != null; 55 assert trigger != null; 56 57 this.scheduler = scheduler; 58 this.trigger = trigger; 59 this.info = info; 60 } 61 62 protected void checkState() 63 { 64 66 if(trigger.getNextFireTime() == null) 67 throw new NoSuchObjectLocalException ("timer has expired"); 68 } 69 70 77 public void cancel() throws IllegalStateException , NoSuchObjectLocalException , EJBException 78 { 79 checkState(); 80 81 try { 82 scheduler.unscheduleJob(trigger.getName(), trigger.getGroup()); 84 } 85 catch(SchedulerException e) { 86 log.error("cancel failed", e); 87 throw new EJBException (e.getMessage()); 88 } 89 } 90 91 99 public long getTimeRemaining() throws IllegalStateException , NoSuchObjectLocalException , EJBException 100 { 101 return getNextTimeout().getTime() - System.currentTimeMillis(); 103 } 104 105 113 public Date getNextTimeout() throws IllegalStateException , NoSuchObjectLocalException , EJBException 114 { 115 checkState(); 116 117 Date nextTimeout = trigger.getNextFireTime(); 118 if(nextTimeout == null) 119 throw new IllegalStateException ("trigger does not have a next fire time"); return nextTimeout; 121 } 122 123 131 public Serializable getInfo() throws IllegalStateException , NoSuchObjectLocalException , EJBException 132 { 133 checkState(); 134 135 return info; 136 } 137 138 146 public TimerHandle getHandle() throws IllegalStateException , NoSuchObjectLocalException , EJBException 147 { 148 checkState(); 149 150 return null; } 152 } 153 | Popular Tags |