2 * Copyright (C) 2001, 2002 The Mir-coders group
4 * This file is part of Mir.
6 * Mir is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * Mir is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with Mir; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * In addition, as a special exception, The Mir-coders gives permission to link
21 * the code of this program with any library licensed under the Apache Software License,
22 * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library
23 * (or with modified versions of the above that use the same license as the above),
24 * and distribute linked combinations including the two. You must obey the
25 * GNU General Public License in all respects for all of the code used other than
26 * the above mentioned libraries. If you modify this file, you may extend this
27 * exception to your version of the file, but you are not obligated to do so.
28 * If you do not wish to do so, delete this exception statement from your version.
31 package mircoders.global;
34 import java.util.ArrayList;
35 import java.util.Calendar;
36 import java.util.Date;
37 import java.util.GregorianCalendar;
38 import java.util.HashMap;
39 import java.util.Iterator;
40 import java.util.List;
43 import mir.log.LoggerWrapper;
45 // important: objects passed as data must not be altered once put into a job
47 public class JobQueue {
48 private List jobHandlers;
49 private Map identifierToJobHandler;
51 private int jobCleanupTreshold;
52 private JobQueueRunner queueRunner;
53 private Thread thread;
54 private LoggerWrapper logger;
55 private long lastCleanup;
57 public static final int STATUS_CREATED = -1;
58 public static final int STATUS_PENDING = 0;
59 public static final int STATUS_PROCESSING = 1;
60 public static final int STATUS_PROCESSED = 2;
61 public static final int STATUS_CANCELLED = 3;
62 public static final int STATUS_ABORTED = 4;
64 public static final int PRIORITY_NORMAL = 100;
65 public static final int PRIORITY_LOW = 10;
66 public static final int PRIORITY_HIGH = 1000;
68 public static final int FINISHEDJOBS_LOGSIZE = 10;
70 public JobQueue(LoggerWrapper aLogger) {
72 jobHandlers = new ArrayList();
73 identifierToJobHandler = new HashMap();
76 jobCleanupTreshold = 900; // seconds
77 queueRunner = new JobQueueRunner(logger);
78 thread = new Thread(queueRunner, "JobQueue");
79 thread.setDaemon(true);
83 public String appendJob(Job aJob, String aDescription) {
85 if (System.currentTimeMillis() - lastCleanup > 60000)
89 logger.error("error while cleaning up joblist: " + t.toString());
92 synchronized (jobHandlers) {
93 JobHandler jobHandler = new JobHandler(aJob, Integer.toString(nrJobs), aDescription);
95 jobHandlers.add(jobHandler);
96 identifierToJobHandler.put(jobHandler.getIdentifier(), jobHandler);
97 jobHandler.setPending();
101 return jobHandler.getIdentifier();
105 public List getJobsInfo() {
106 List result = new ArrayList();
108 synchronized (jobHandlers) {
109 Iterator i = jobHandlers.iterator();
111 while (i.hasNext()) {
112 result.add(0, ((JobHandler) i.next()).getJobInfo());
119 private void cleanupJobList() {
120 List toRemove = new ArrayList();
122 synchronized (jobHandlers) {
123 Iterator i = jobHandlers.iterator();
125 Calendar tresholdCalendar = new GregorianCalendar();
126 tresholdCalendar.add(Calendar.SECOND, -jobCleanupTreshold);
127 Date treshold = tresholdCalendar.getTime();
129 while (i.hasNext()) {
130 JobHandler jobHandler = (JobHandler) i.next();
132 synchronized (jobHandler) {
133 if (jobHandler.isFinished() && jobHandler.getLastChange().before(treshold)) {
134 toRemove.add(jobHandler);
139 jobHandlers.removeAll(toRemove);
142 lastCleanup = System.currentTimeMillis();
146 * Returns when a new producer job has become available
148 private JobHandler acquirePendingJob() throws InterruptedException {
149 int priorityFound= 0;
152 synchronized (jobHandlers) {
155 Iterator i = jobHandlers.iterator();
156 while (i.hasNext()) {
157 JobHandler job = (JobHandler) i.next();
159 if (job.isPending() && (jobFound==null || priorityFound<job.getPriority())) {
161 priorityFound = job.getPriority();
165 if (jobFound==null) {
169 } while (jobFound==null);
175 public void cancelJobs(List aJobs) {
176 synchronized (jobHandlers) {
177 Iterator i = aJobs.iterator();
179 while (i.hasNext()) {
180 ((JobHandler) identifierToJobHandler.get(i.next())).cancelOrAbortJob();
185 public void cancelAllJobs() {
186 synchronized (jobHandlers) {
187 Iterator i = jobHandlers.iterator();
189 while (i.hasNext()) {
190 ((JobHandler) i.next()).cancelOrAbortJob();
195 public interface Job {
197 * This method should cause the run() method to terminate as soon as
203 * This method should perform the actions associated with the job.
204 * @return <code>true</code> if terminated normally, <code>false</code> if aborted
209 public static class JobInfo {
210 private String identifier;
211 private Date lastChange;
213 private long runningTime;
214 private int priority;
215 private String description;
217 private JobInfo(String aDescription, int aStatus, Date aLastChange, String anIdentifier, long aRunningTime, int aPriority) {
218 description = aDescription;
219 lastChange = aLastChange;
221 identifier = anIdentifier;
222 priority = aPriority;
223 runningTime = aRunningTime;
226 public String getDescription() {
230 public int getStatus() {
234 public int getPriority() {
238 public Date getLastChange() {
242 public String getIdentifier() {
246 public long getRunningTime() {
251 public class JobHandler {
253 private String identifier;
254 private String description;
256 private Date lastChange;
257 private long starttime;
258 private long endtime;
260 private int priority;
261 private boolean hasRun;
263 public JobHandler(Job aJob, String anIdentifier, String aDescription, int aPriority) {
265 description = aDescription;
266 identifier = anIdentifier;
267 priority = aPriority;
268 status = STATUS_CREATED;
271 public JobHandler(Job aJob, String anIdentifier, String aDescription) {
272 this(aJob, anIdentifier, aDescription, PRIORITY_NORMAL);
275 public JobInfo getJobInfo() {
276 return new JobInfo(getDescription(), getStatus(), getLastChange(), getIdentifier(), getRunningTime(), priority);
279 private void runJob() {
280 if (setProcessing()) {
288 private void cancelOrAbortJob() {
289 synchronized (this) {
297 public int getStatus() {
303 public String getIdentifier() {
307 public String getDescription() {
311 public long getRunningTime() {
319 result = System.currentTimeMillis();
321 result = result - starttime;
328 public int getPriority() {
332 private boolean setProcessing() {
333 return setStatus(STATUS_PENDING, STATUS_PROCESSING);
336 private void setProcessed() {
337 setStatus(STATUS_PROCESSING, STATUS_PROCESSED);
340 private void setAborted() {
341 setStatus(STATUS_PROCESSING, STATUS_ABORTED);
344 private void setPending() {
345 setStatus(STATUS_CREATED, STATUS_PENDING);
348 private boolean setCancelled() {
349 return setStatus(STATUS_PENDING, STATUS_CANCELLED);
352 public boolean hasBeenProcessed() {
353 return getStatus() == STATUS_PROCESSED;
356 public boolean hasBeenAborted() {
357 return getStatus() == STATUS_ABORTED;
360 public boolean isCancelled() {
361 return getStatus() == STATUS_CANCELLED;
364 public boolean isFinished() {
365 return hasBeenProcessed() || hasBeenAborted() || isCancelled();
368 public boolean isProcessing() {
369 return getStatus() == STATUS_PROCESSING;
372 public boolean isPending() {
373 return getStatus() == STATUS_PENDING;
376 public Date getLastChange() {
377 synchronized (this) {
382 private boolean setStatus(int anOldStatus, int aNewStatus) {
384 if (status == anOldStatus) {
386 lastChange = (new GregorianCalendar()).getTime();
387 if (isProcessing()) {
388 starttime = System.currentTimeMillis();
393 endtime = System.currentTimeMillis();
402 private class JobQueueRunner implements Runnable {
404 public JobQueueRunner(LoggerWrapper aLogger) {
409 logger.debug("starting JobQueueRunner");
414 JobHandler job = acquirePendingJob();
416 logger.debug(" starting job ("+job.getIdentifier()+"): " +job.getDescription());
418 logger.debug(" finished job ("+job.getIdentifier()+"): " +job.getDescription());
422 catch (InterruptedException e) {
427 logger.warn("JobQueueRunner terminated");