FileDocCategorySizeDatePackage
TimerServiceImpl.javaAPI DocJBoss 4.2.110799Fri Jul 13 20:53:54 BST 2007org.jboss.ejb3.timerservice.quartz

TimerServiceImpl

public class TimerServiceImpl extends Object implements javax.ejb.TimerService
Implements the EJB3 Timer Service specification (EJB3 chapter 18). Each bean container has its own job and trigger group.
author
Carlo de Wolf
version
$Revision: 60233 $

Fields Summary
private static final Logger
log
private org.quartz.Scheduler
scheduler
private ObjectName
objectName
private String
groupName
private long
jobNum
private long
triggerNum
Constructors Summary
protected TimerServiceImpl(org.quartz.Scheduler scheduler, ObjectName objectName, org.jboss.ejb3.timerservice.TimedObjectInvoker invoker)

   
          
      assert scheduler != null;
      assert objectName != null;
      assert invoker != null;
      
      this.scheduler = scheduler;
      this.objectName = objectName;
      this.groupName = objectName.getCanonicalName();
   
Methods Summary
protected javax.ejb.TimercreateTimer(org.quartz.Trigger trigger, java.io.Serializable info)

      try {
         String name = "myJob" + jobNum;
         jobNum++;
         
         Class jobClass = QuartzTimerJob.class;
         
         Timer timer = new TimerImpl(scheduler, trigger, info);
         
         PersistentTimer persistentTimer = new PersistentTimer(trigger, objectName, info);
         
         JobDetail jobDetail = new JobDetail(name, groupName, jobClass);
         jobDetail.getJobDataMap().put("timer", persistentTimer);
         
         scheduler.scheduleJob(jobDetail, trigger);
         
         return timer;
      }
      catch(SchedulerException e) {
         // translate the exception, because the client might not have quartz
         log.error("createTimer failed", e);
         throw new EJBException(e.getMessage());
      }
      
   
public javax.ejb.TimercreateTimer(long duration, java.io.Serializable info)
Create a single-action timer that expires after a specified duration.

param
duration The number of milliseconds that must elapse before the timer expires.
param
info Application information to be delivered along with the timer expiration notification. This can be null.
return
The newly created Timer.
throws
IllegalArgumentException If duration is negative.
throws
IllegalStateException If this method is invoked while the instance is in a state that does not allow access to this method.
throws
EJBException If this method fails due to a system-level failure.

      if(duration < 0) throw new IllegalArgumentException("duration must not be negative");
      // TODO: check state
      
      Date expiration = new Date(System.currentTimeMillis() + duration);
      return createTimer(expiration, info);
   
public javax.ejb.TimercreateTimer(long initialDuration, long intervalDuration, java.io.Serializable info)
Create an interval timer whose first expiration occurs after a specified duration, and whose subsequent expirations occur after a specified interval.

param
initialDuration The number of milliseconds that must elapse before the first timer expiration notification.
param
intervalDuration The number of milliseconds that must elapse between timer expiration notifications. Expiration notifications are scheduled relative to the time of the first expiration. If expiration is delayed(e.g. due to the interleaving of other method calls on the bean) two or more expiration notifications may occur in close succession to "catch up".
param
info Application information to be delivered along with the timer expiration. This can be null.
return
The newly created Timer.
throws
IllegalArgumentException If initialDuration is negative, or intervalDuration is negative.
throws
IllegalStateException If this method is invoked while the instance is in a state that does not allow access to this method.
throws
EJBException If this method could not complete due to a system-level failure.

      if(initialDuration < 0) throw new IllegalArgumentException("initialDuration must not be negative");
      if(intervalDuration < 0) throw new IllegalArgumentException("intervalDuration must not be negative");
      // TODO: check state
      
      Date initialExpiration = new Date(System.currentTimeMillis() + initialDuration);
      
      return createTimer(initialExpiration, intervalDuration, info);
   
public javax.ejb.TimercreateTimer(java.util.Date expiration, java.io.Serializable info)
Create a single-action timer that expires at a given point in time.

param
expiration The point in time at which the timer must expire.
param
info Application information to be delivered along with the timer expiration notification. This can be null.
return
The newly created Timer.
throws
IllegalArgumentException If expiration is null, or expiration.getTime() is negative.
throws
IllegalStateException If this method is invoked while the instance is in a state that does not allow access to this method.
throws
EJBException If this method could not complete due to a system-level failure.

      if(expiration == null) throw new IllegalArgumentException("expiration must not be null");
      if(expiration.getTime() < 0) throw new IllegalArgumentException("expiration.time must not be negative");
      // TODO: check state
      
      String triggerName = "myTrigger" + triggerNum;
      triggerNum++;
      
      Trigger trigger = new SimpleTrigger(triggerName, groupName, expiration);
      
      return createTimer(trigger, info);
   
public javax.ejb.TimercreateTimer(java.util.Date initialExpiration, long intervalDuration, java.io.Serializable info)
Create an interval timer whose first expiration occurs at a given point in time and whose subsequent expirations occur after a specified interval.

param
initialExpiration The point in time at which the first timer expiration must occur.
param
intervalDuration The number of milliseconds that must elapse between timer expiration notifications. Expiration notifications are scheduled relative to the time of the first expiration. If expiration is delayed(e.g. due to the interleaving of other method calls on the bean) two or more expiration notifications may occur in close succession to "catch up".
param
info Application information to be delivered along with the timer expiration notification. This can be null.
return
The newly created Timer.
throws
IllegalArgumentException If initialExpiration is null, or initialExpiration.getTime() is negative, or intervalDuration is negative.
throws
IllegalStateException If this method is invoked while the instance is in a state that does not allow access to this method.
throws
EJBException If this method could not complete due to a system-level failure.

      if(initialExpiration == null) throw new IllegalArgumentException("initialExpiration must not be null");
      if(initialExpiration.getTime() < 0) throw new IllegalArgumentException("initialExpiration.time must not be negative");
      if(intervalDuration < 0) throw new IllegalArgumentException("intervalDuration must not be negative");
      // TODO: check state
      
      String triggerName = "myTrigger" + triggerNum;
      triggerNum++;
      Date endTime = null;
      
      Trigger trigger = new SimpleTrigger(triggerName, groupName, initialExpiration, endTime, SimpleTrigger.REPEAT_INDEFINITELY, intervalDuration);
      
      return createTimer(trigger, info);
   
protected org.quartz.SchedulergetScheduler()

      return scheduler;
   
public java.util.CollectiongetTimers()
Get all the active timers associated with this bean.

return
A collection of javax.ejb.Timer objects.
throws
IllegalStateException If this method is invoked while the instance is in a state that does not allow access to this method.
throws
EJBException If this method could not complete due to a system-level failure.

      throw new RuntimeException("NYI");
   
protected voidshutdown()

      log.debug("shutting down " + this);
      try
      {
         String triggerNames[] = scheduler.getTriggerNames(groupName);
         for(String triggerName : triggerNames)
            scheduler.unscheduleJob(triggerName, groupName);
         String jobNames[] = scheduler.getJobNames(groupName);
         for(String jobName : jobNames)
            scheduler.deleteJob(jobName, groupName);
      }
      catch(SchedulerException e)
      {
         log.error("shutdown failed", e);
         // TODO: ignore?
      }
   
public java.lang.StringtoString()

      return "Timer Service " + objectName;