1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.jetspeed.scheduler;
18
19 import java.util.List;
20 import java.util.Vector;
21 import java.util.Collections;
22 import java.util.Comparator;
23
24 /***
25 * Queue for the scheduler.
26 *
27 * @author <a href="mailto:mbryson@mont.mindspring.com">Dave Bryson</a>
28 * @version $Id: JobQueue.java 516448 2007-03-09 16:25:47Z ate $
29 */
30 public class JobQueue
31 {
32 /***
33 * The queue of <code>JobEntry</code> objects.
34 */
35 private Vector queue = null;
36
37 /***
38 * Creates a new instance.
39 *
40 * @exception Exception A generic exception.
41 */
42 public JobQueue()
43 throws Exception
44 {
45 queue = new Vector(10);
46 }
47
48 /***
49 * Return the next job off the top of the queue, or <code>null</code> if
50 * there are no jobs in the queue.
51 *
52 * @return The next job in the queue.
53 */
54 public JobEntry getNext()
55 {
56 if ( queue.size() > 0 )
57 {
58 return (JobEntry)queue.elementAt(0);
59 }
60 else
61 {
62 return null;
63 }
64 }
65
66 /***
67 * Return a specific job.
68 *
69 * @param je The JobEntry we are looking for.
70 * @return A JobEntry.
71 */
72 public JobEntry getJob(JobEntry je)
73 {
74 int index = -1;
75
76 if ( je != null )
77 {
78 index = queue.indexOf(je);
79 }
80
81 if ( index < 0 )
82 {
83 return null;
84 }
85 else
86 {
87 return (JobEntry)queue.elementAt(index);
88 }
89 }
90
91 /***
92 * List jobs in the queue. This is used by the scheduler UI.
93 *
94 * @return A Vector of <code>JobEntry</code> objects.
95 */
96 public Vector list()
97 {
98 if ( queue != null && queue.size() > 0 )
99 {
100 return (Vector)queue.clone();
101 }
102 else
103 {
104 return null;
105 }
106 }
107
108 /***
109 * Add a job to the queue.
110 *
111 * @param je A JobEntry job.
112 */
113 public synchronized void add(JobEntry je)
114 {
115 queue.addElement(je);
116 sortQueue();
117 }
118
119 /***
120 * Batch load jobs. Retains any already enqueued jobs. Called on
121 * <code>SchedulerService</code> start-up.
122 *
123 * @param jobEntries A list of the <code>JobEntry</code> objects to load.
124 */
125 public synchronized void batchLoad(List jobEntries)
126 {
127 if (jobEntries != null)
128 {
129 queue.addAll(jobEntries);
130 sortQueue();
131 }
132
133 }
134
135 /***
136 * Remove a job from the queue.
137 *
138 * @param je A JobEntry with the job to remove.
139 */
140 public synchronized void remove(JobEntry je)
141 {
142 queue.removeElement(je);
143 sortQueue();
144 }
145
146 /***
147 * Modify a job on the queue.
148 *
149 * @param je A JobEntry with the job to modify
150 */
151 public synchronized void modify(JobEntry je)
152 {
153 sortQueue();
154 }
155
156 /***
157 * Update the job for its next run time.
158 *
159 * @param je A JobEntry to be updated.
160 * @exception Exception, a generic exception.
161 */
162 public synchronized void updateQueue(JobEntry je)
163 throws Exception
164 {
165 je.calcRunTime();
166 sortQueue();
167 }
168
169 /***
170 * Re-sort the existing queue. Consumers of this method should be
171 * <code>synchronized</code>.
172 */
173 private void sortQueue()
174 {
175 Comparator aComparator = new Comparator ()
176 {
177 public int compare(Object o1, Object o2)
178 {
179 Long time1 = new Long (((JobEntry)o1).getNextRuntime());
180 Long time2 = new Long (((JobEntry)o2).getNextRuntime());
181 return (time1.compareTo(time2));
182 }
183 };
184
185 Collections.sort(queue,aComparator);
186 }
187 }