1 package org.apache.turbine.services.schedule;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 import java.util.List;
23 import java.util.Vector;
24 import java.util.concurrent.ConcurrentSkipListSet;
25
26 import org.apache.turbine.util.TurbineException;
27
28 /**
29 * Queue for the scheduler.
30 *
31 * @author <a href="mailto:mbryson@mont.mindspring.com">Dave Bryson</a>
32 * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
33 * @version $Id: JobQueue.java 615328 2008-01-25 20:25:05Z tv $
34 * @param <J> a specialized job entry type
35 */
36 public class JobQueue<J extends JobEntry>
37 {
38 /**
39 * The queue of <code>JobEntry</code> objects.
40 */
41 private ConcurrentSkipListSet<J> queue = null;
42
43 /**
44 * Creates a new instance.
45 */
46 public JobQueue()
47 {
48 queue = new ConcurrentSkipListSet<J>((o1, o2) -> Long.compare(o1.getNextRuntime(), o2.getNextRuntime()));
49 }
50
51 /**
52 * Return the next job off the top of the queue and remove it from the queue, or <code>null</code> if
53 * there are no jobs in the queue.
54 *
55 * @return The next job in the queue.
56 */
57 public J getNext()
58 {
59 return queue.pollFirst();
60 }
61
62 /**
63 * Return the next job of the top of the queue or <code>null</code> if
64 * there are no jobs in the queue.
65 *
66 * @return The next job in the queue.
67 */
68 public J getFirst()
69 {
70 return !queue.isEmpty()? queue.first(): null;
71 }
72
73 /**
74 * Return a specific job.
75 *
76 * @param je The JobEntry we are looking for. Falls back to check job id, if job was not found.
77 * @return A JobEntry.
78 */
79 public J getJob(J je)
80 {
81 if (je != null)
82 {
83 J job = queue.floor(je);
84 if (je.equals(job))
85 {
86 return job;
87 }
88 for (J jobEntry : list())
89 {
90 if (jobEntry.getJobId() == je.getJobId())
91 {
92 return jobEntry;
93 }
94 }
95 }
96 return null;
97 }
98
99 /**
100 * List jobs in the queue. This is used by the scheduler UI.
101 *
102 * @return A Vector of <code>JobEntry</code> objects.
103 */
104 public Vector<J> list()
105 {
106 if (!queue.isEmpty())
107 {
108 return new Vector<>(queue);
109 }
110 else
111 {
112 return null;
113 }
114 }
115
116 /**
117 * Add a job to the queue.
118 *
119 * @param je A JobEntry job.
120 */
121 public void add(J je)
122 {
123 queue.add(je);
124 }
125
126 /**
127 * Batch load jobs. Retains any already enqueued jobs. Called on
128 * <code>SchedulerService</code> start-up.
129 *
130 * @param jobEntries A list of the <code>JobEntry</code> objects to load.
131 */
132 public void batchLoad(List<J> jobEntries)
133 {
134 if (jobEntries != null)
135 {
136 queue.addAll(jobEntries);
137 }
138 }
139
140 /**
141 * Remove a job from the queue.
142 *
143 * @param je A JobEntry with the job to remove.
144 */
145 public void remove(J je)
146 {
147 queue.remove(je);
148 }
149
150 /**
151 * Modify a job on the queue.
152 *
153 * @param je A JobEntry with the job to modify
154 * @throws TurbineException if the runtime calculation fails
155 */
156 public void modify(J je) throws TurbineException
157 {
158 remove(je);
159 je.calcRunTime();
160 add(je);
161 }
162
163 /**
164 * Update the job for its next run time.
165 *
166 * @param je A JobEntry to be updated.
167 * @throws TurbineException a generic exception.
168 */
169 public void updateQueue(J je)
170 throws TurbineException
171 {
172 modify(je);
173 }
174 }