01: package com.xoetrope.task;
02:
03: import java.util.Date;
04: import java.util.Vector;
05:
06: /**
07: * <p>A scheduler for tasks</p>
08: *
09: * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
10: * the GNU Public License (GPL), please see license.txt for more details. If
11: * you make commercial use of this software you must purchase a commercial
12: * license from Xoetrope.</p>
13: * <p> $Revision: 1.4 $</p>
14: */
15: public class XScheduler {
16: /**
17: * A one off job
18: */
19: public static final int ONCE_OFF_JOB = 0;
20: /**
21: * A recurring job
22: */
23: public static final int RECURRING_JOB = 1;
24:
25: private Vector jobs;
26: private int nextJobId;
27:
28: /**
29: * Create a new instance of the scheduler
30: */
31: public XScheduler() {
32: jobs = new Vector();
33: nextJobId = 0;
34: }
35:
36: /**
37: * Adds a job to the scheduler
38: * @param className the className or an instance of an XExecutable object
39: * @param type the job type, ONCE_OFF_JOB or RECURRING_JOB
40: * @param interval the elapsed time interval till the callback is invoked
41: * @param startDate the start time or 0 for immediate start to the countdown
42: * @param args and arguments to the task.
43: * @return the job ID
44: */
45: public int addJob(Object className, int type, long interval,
46: long startDate, Object args[]) {
47: if (startDate == 0)
48: startDate = new Date().getTime();
49:
50: if (type == ONCE_OFF_JOB)
51: startDate += interval;
52:
53: XJob newJob = new XJob(nextJobId, className, type, interval,
54: startDate, args);
55: jobs.addElement(newJob);
56: newJob.start();
57:
58: return nextJobId++;
59: }
60:
61: /**
62: * Stop/Kill the job
63: * @param id the ID of the job/task
64: */
65: public void killJob(int id) {
66: int numJobs = jobs.size();
67: for (int i = 0; i < numJobs; i++) {
68: XJob j = (XJob) jobs.elementAt(i);
69: if (j.id == id) {
70: j.kill();
71: jobs.removeElement(j);
72: break;
73: }
74: }
75:
76: }
77: }
|