001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.ejb3.timerservice.quartz;
023:
024: import java.io.Serializable;
025: import java.util.Collection;
026: import java.util.Date;
027:
028: import javax.ejb.EJBException;
029: import javax.ejb.Timer;
030: import javax.ejb.TimerService;
031: import javax.management.ObjectName;
032:
033: import org.jboss.ejb3.timerservice.TimedObjectInvoker;
034: import org.jboss.logging.Logger;
035: import org.quartz.JobDetail;
036: import org.quartz.Scheduler;
037: import org.quartz.SchedulerException;
038: import org.quartz.SimpleTrigger;
039: import org.quartz.Trigger;
040:
041: /**
042: * Implements the EJB3 Timer Service specification (EJB3 chapter 18).
043: *
044: * Each bean container has its own job and trigger group.
045: *
046: * @author <a href="mailto:carlo@nerdnet.nl">Carlo de Wolf</a>
047: * @version $Revision: 60233 $
048: */
049: public class TimerServiceImpl implements TimerService {
050: private static final Logger log = Logger
051: .getLogger(TimerServiceImpl.class);
052:
053: private Scheduler scheduler;
054: private ObjectName objectName;
055: private String groupName;
056: private long jobNum = 0;
057: private long triggerNum = 0;
058:
059: protected TimerServiceImpl(Scheduler scheduler,
060: ObjectName objectName, TimedObjectInvoker invoker) {
061: assert scheduler != null;
062: assert objectName != null;
063: assert invoker != null;
064:
065: this .scheduler = scheduler;
066: this .objectName = objectName;
067: this .groupName = objectName.getCanonicalName();
068: }
069:
070: protected Timer createTimer(Trigger trigger, Serializable info) {
071: try {
072: String name = "myJob" + jobNum;
073: jobNum++;
074:
075: Class jobClass = QuartzTimerJob.class;
076:
077: Timer timer = new TimerImpl(scheduler, trigger, info);
078:
079: PersistentTimer persistentTimer = new PersistentTimer(
080: trigger, objectName, info);
081:
082: JobDetail jobDetail = new JobDetail(name, groupName,
083: jobClass);
084: jobDetail.getJobDataMap().put("timer", persistentTimer);
085:
086: scheduler.scheduleJob(jobDetail, trigger);
087:
088: return timer;
089: } catch (SchedulerException e) {
090: // translate the exception, because the client might not have quartz
091: log.error("createTimer failed", e);
092: throw new EJBException(e.getMessage());
093: }
094:
095: }
096:
097: /**
098: * Create a single-action timer that expires after a specified duration.
099: *
100: * @param duration The number of milliseconds that must elapse before the timer expires.
101: * @param info Application information to be delivered along with the timer expiration notification. This can be null.
102: * @return The newly created Timer.
103: * @throws IllegalArgumentException If duration is negative.
104: * @throws IllegalStateException If this method is invoked while the instance is in a state that does not allow access to this method.
105: * @throws EJBException If this method fails due to a system-level failure.
106: */
107: public Timer createTimer(long duration, Serializable info)
108: throws IllegalArgumentException, IllegalStateException,
109: EJBException {
110: if (duration < 0)
111: throw new IllegalArgumentException(
112: "duration must not be negative");
113: // TODO: check state
114:
115: Date expiration = new Date(System.currentTimeMillis()
116: + duration);
117: return createTimer(expiration, info);
118: }
119:
120: /**
121: * Create an interval timer whose first expiration occurs after a specified duration,
122: * and whose subsequent expirations occur after a specified interval.
123: *
124: * @param initialDuration The number of milliseconds that must elapse before the first timer expiration notification.
125: * @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".
126: * @param info Application information to be delivered along with the timer expiration. This can be null.
127: * @return The newly created Timer.
128: * @throws IllegalArgumentException If initialDuration is negative, or intervalDuration is negative.
129: * @throws IllegalStateException If this method is invoked while the instance is in a state that does not allow access to this method.
130: * @throws EJBException If this method could not complete due to a system-level failure.
131: */
132: public Timer createTimer(long initialDuration,
133: long intervalDuration, Serializable info)
134: throws IllegalArgumentException, IllegalStateException,
135: EJBException {
136: if (initialDuration < 0)
137: throw new IllegalArgumentException(
138: "initialDuration must not be negative");
139: if (intervalDuration < 0)
140: throw new IllegalArgumentException(
141: "intervalDuration must not be negative");
142: // TODO: check state
143:
144: Date initialExpiration = new Date(System.currentTimeMillis()
145: + initialDuration);
146:
147: return createTimer(initialExpiration, intervalDuration, info);
148: }
149:
150: /**
151: * Create a single-action timer that expires at a given point in time.
152: *
153: * @param expiration The point in time at which the timer must expire.
154: * @param info Application information to be delivered along with the timer expiration notification. This can be null.
155: * @return The newly created Timer.
156: * @throws IllegalArgumentException If expiration is null, or expiration.getTime() is negative.
157: * @throws IllegalStateException If this method is invoked while the instance is in a state that does not allow access to this method.
158: * @throws EJBException If this method could not complete due to a system-level failure.
159: */
160: public Timer createTimer(Date expiration, Serializable info)
161: throws IllegalArgumentException, IllegalStateException,
162: EJBException {
163: if (expiration == null)
164: throw new IllegalArgumentException(
165: "expiration must not be null");
166: if (expiration.getTime() < 0)
167: throw new IllegalArgumentException(
168: "expiration.time must not be negative");
169: // TODO: check state
170:
171: String triggerName = "myTrigger" + triggerNum;
172: triggerNum++;
173:
174: Trigger trigger = new SimpleTrigger(triggerName, groupName,
175: expiration);
176:
177: return createTimer(trigger, info);
178: }
179:
180: /**
181: * Create an interval timer whose first expiration occurs at a given point in time and whose subsequent expirations occur after a specified interval.
182: *
183: * @param initialExpiration The point in time at which the first timer expiration must occur.
184: * @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".
185: * @param info Application information to be delivered along with the timer expiration notification. This can be null.
186: * @return The newly created Timer.
187: * @throws IllegalArgumentException If initialExpiration is null, or initialExpiration.getTime() is negative, or intervalDuration is negative.
188: * @throws IllegalStateException If this method is invoked while the instance is in a state that does not allow access to this method.
189: * @throws EJBException If this method could not complete due to a system-level failure.
190: */
191: public Timer createTimer(Date initialExpiration,
192: long intervalDuration, Serializable info)
193: throws IllegalArgumentException, IllegalStateException,
194: EJBException {
195: if (initialExpiration == null)
196: throw new IllegalArgumentException(
197: "initialExpiration must not be null");
198: if (initialExpiration.getTime() < 0)
199: throw new IllegalArgumentException(
200: "initialExpiration.time must not be negative");
201: if (intervalDuration < 0)
202: throw new IllegalArgumentException(
203: "intervalDuration must not be negative");
204: // TODO: check state
205:
206: String triggerName = "myTrigger" + triggerNum;
207: triggerNum++;
208: Date endTime = null;
209:
210: Trigger trigger = new SimpleTrigger(triggerName, groupName,
211: initialExpiration, endTime,
212: SimpleTrigger.REPEAT_INDEFINITELY, intervalDuration);
213:
214: return createTimer(trigger, info);
215: }
216:
217: protected Scheduler getScheduler() {
218: return scheduler;
219: }
220:
221: /**
222: * Get all the active timers associated with this bean.
223: *
224: * @return A collection of javax.ejb.Timer objects.
225: * @throws IllegalStateException If this method is invoked while the instance is in a state that does not allow access to this method.
226: * @throws EJBException If this method could not complete due to a system-level failure.
227: */
228: public Collection getTimers() throws IllegalStateException,
229: EJBException {
230: throw new RuntimeException("NYI");
231: }
232:
233: protected void shutdown() {
234: log.debug("shutting down " + this );
235: try {
236: String triggerNames[] = scheduler
237: .getTriggerNames(groupName);
238: for (String triggerName : triggerNames)
239: scheduler.unscheduleJob(triggerName, groupName);
240: String jobNames[] = scheduler.getJobNames(groupName);
241: for (String jobName : jobNames)
242: scheduler.deleteJob(jobName, groupName);
243: } catch (SchedulerException e) {
244: log.error("shutdown failed", e);
245: // TODO: ignore?
246: }
247: }
248:
249: public String toString() {
250: return "Timer Service " + objectName;
251: }
252: }
|