KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > timerservice > quartz > TimerServiceImpl


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.ejb3.timerservice.quartz;
23
24 import java.io.Serializable JavaDoc;
25 import java.util.Collection JavaDoc;
26 import java.util.Date JavaDoc;
27
28 import javax.ejb.EJBException JavaDoc;
29 import javax.ejb.Timer JavaDoc;
30 import javax.ejb.TimerService JavaDoc;
31 import javax.management.ObjectName JavaDoc;
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 /**
42  * Implements the EJB3 Timer Service specification (EJB3 chapter 18).
43  *
44  * Each bean container has its own job and trigger group.
45  *
46  * @author <a HREF="mailto:carlo@nerdnet.nl">Carlo de Wolf</a>
47  * @version $Revision: 58478 $
48  */

49 public class TimerServiceImpl implements TimerService JavaDoc
50 {
51    private static final Logger log = Logger.getLogger(TimerServiceImpl.class);
52    
53    private Scheduler scheduler;
54    private ObjectName JavaDoc objectName;
55    private String JavaDoc groupName;
56    private long jobNum = 0;
57    private long triggerNum = 0;
58    
59    protected TimerServiceImpl(Scheduler scheduler, ObjectName JavaDoc 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 JavaDoc createTimer(Trigger trigger, Serializable JavaDoc info)
70    {
71       try {
72          String JavaDoc name = "myJob" + jobNum;
73          jobNum++;
74          
75          Class JavaDoc jobClass = QuartzTimerJob.class;
76          
77          Timer JavaDoc 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          // translate the exception, because the client might not have quartz
90
log.error("createTimer failed", e);
91          throw new EJBException JavaDoc(e.getMessage());
92       }
93       
94    }
95    
96    /**
97     * Create a single-action timer that expires after a specified duration.
98     *
99     * @param duration The number of milliseconds that must elapse before the timer expires.
100     * @param info Application information to be delivered along with the timer expiration notification. This can be null.
101     * @return The newly created Timer.
102     * @throws IllegalArgumentException If duration is negative.
103     * @throws IllegalStateException If this method is invoked while the instance is in a state that does not allow access to this method.
104     * @throws EJBException If this method fails due to a system-level failure.
105     */

106    public Timer JavaDoc createTimer(long duration, Serializable JavaDoc info) throws IllegalArgumentException JavaDoc, IllegalStateException JavaDoc,
107          EJBException JavaDoc
108    {
109       if(duration < 0) throw new IllegalArgumentException JavaDoc("duration must not be negative");
110       // TODO: check state
111

112       Date JavaDoc expiration = new Date JavaDoc(System.currentTimeMillis() + duration);
113       return createTimer(expiration, info);
114    }
115
116    /**
117     * Create an interval timer whose first expiration occurs after a specified duration,
118     * and whose subsequent expirations occur after a specified interval.
119     *
120     * @param initialDuration The number of milliseconds that must elapse before the first timer expiration notification.
121     * @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".
122     * @param info Application information to be delivered along with the timer expiration. This can be null.
123     * @return The newly created Timer.
124     * @throws IllegalArgumentException If initialDuration is negative, or intervalDuration is negative.
125     * @throws IllegalStateException If this method is invoked while the instance is in a state that does not allow access to this method.
126     * @throws EJBException If this method could not complete due to a system-level failure.
127     */

128    public Timer JavaDoc createTimer(long initialDuration, long intervalDuration, Serializable JavaDoc info)
129          throws IllegalArgumentException JavaDoc, IllegalStateException JavaDoc, EJBException JavaDoc
130    {
131       if(initialDuration < 0) throw new IllegalArgumentException JavaDoc("initialDuration must not be negative");
132       if(intervalDuration < 0) throw new IllegalArgumentException JavaDoc("intervalDuration must not be negative");
133       // TODO: check state
134

135       Date JavaDoc initialExpiration = new Date JavaDoc(System.currentTimeMillis() + initialDuration);
136       
137       return createTimer(initialExpiration, intervalDuration, info);
138    }
139
140    /**
141     * Create a single-action timer that expires at a given point in time.
142     *
143     * @param expiration The point in time at which the timer must expire.
144     * @param info Application information to be delivered along with the timer expiration notification. This can be null.
145     * @return The newly created Timer.
146     * @throws IllegalArgumentException If expiration is null, or expiration.getTime() is negative.
147     * @throws IllegalStateException If this method is invoked while the instance is in a state that does not allow access to this method.
148     * @throws EJBException If this method could not complete due to a system-level failure.
149     */

150    public Timer JavaDoc createTimer(Date JavaDoc expiration, Serializable JavaDoc info) throws IllegalArgumentException JavaDoc, IllegalStateException JavaDoc,
151          EJBException JavaDoc
152    {
153       if(expiration == null) throw new IllegalArgumentException JavaDoc("expiration must not be null");
154       if(expiration.getTime() < 0) throw new IllegalArgumentException JavaDoc("expiration.time must not be negative");
155       // TODO: check state
156

157       String JavaDoc triggerName = "myTrigger" + triggerNum;
158       triggerNum++;
159       
160       Trigger trigger = new SimpleTrigger(triggerName, groupName, expiration);
161       
162       return createTimer(trigger, info);
163    }
164
165    /**
166     * Create an interval timer whose first expiration occurs at a given point in time and whose subsequent expirations occur after a specified interval.
167     *
168     * @param initialExpiration The point in time at which the first timer expiration must occur.
169     * @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".
170     * @param info Application information to be delivered along with the timer expiration notification. This can be null.
171     * @return The newly created Timer.
172     * @throws IllegalArgumentException If initialExpiration is null, or initialExpiration.getTime() is negative, or intervalDuration is negative.
173     * @throws IllegalStateException If this method is invoked while the instance is in a state that does not allow access to this method.
174     * @throws EJBException If this method could not complete due to a system-level failure.
175     */

176    public Timer JavaDoc createTimer(Date JavaDoc initialExpiration, long intervalDuration, Serializable JavaDoc info)
177          throws IllegalArgumentException JavaDoc, IllegalStateException JavaDoc, EJBException JavaDoc
178    {
179       if(initialExpiration == null) throw new IllegalArgumentException JavaDoc("initialExpiration must not be null");
180       if(initialExpiration.getTime() < 0) throw new IllegalArgumentException JavaDoc("initialExpiration.time must not be negative");
181       if(intervalDuration < 0) throw new IllegalArgumentException JavaDoc("intervalDuration must not be negative");
182       // TODO: check state
183

184       String JavaDoc triggerName = "myTrigger" + triggerNum;
185       triggerNum++;
186       Date JavaDoc 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    /**
199     * Get all the active timers associated with this bean.
200     *
201     * @return A collection of javax.ejb.Timer objects.
202     * @throws IllegalStateException If this method is invoked while the instance is in a state that does not allow access to this method.
203     * @throws EJBException If this method could not complete due to a system-level failure.
204     */

205    public Collection JavaDoc getTimers() throws IllegalStateException JavaDoc, EJBException JavaDoc
206    {
207       throw new RuntimeException JavaDoc("NYI");
208    }
209
210    protected void shutdown()
211    {
212       log.debug("shutting down " + this);
213       try
214       {
215          String JavaDoc triggerNames[] = scheduler.getTriggerNames(groupName);
216          for(String JavaDoc triggerName : triggerNames)
217             scheduler.unscheduleJob(triggerName, groupName);
218          String JavaDoc jobNames[] = scheduler.getJobNames(groupName);
219          for(String JavaDoc jobName : jobNames)
220             scheduler.deleteJob(jobName, groupName);
221       }
222       catch(SchedulerException e)
223       {
224          log.error("shutdown failed", e);
225          // TODO: ignore?
226
}
227    }
228    
229    public String JavaDoc toString()
230    {
231       return "Timer Service " + objectName;
232    }
233 }
234
Popular Tags