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.Calendar;
35 import java.util.Date;
36 import java.util.GregorianCalendar;
37 import java.util.HashMap;
38 import java.util.Iterator;
39 import java.util.List;
41 import java.util.Vector;
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 Vector 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 Vector();
73 identifierToJobHandler = new HashMap();
76 jobCleanupTreshold = 900; // seconds
77 queueRunner = new JobQueueRunner(logger);
78 thread = new Thread(queueRunner);
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();
99 return jobHandler.getIdentifier();
103 public List getJobsInfo() {
104 List result = new Vector();
106 synchronized (jobHandlers) {
107 Iterator i = jobHandlers.iterator();
109 while (i.hasNext()) {
110 result.add(0, ((JobHandler) i.next()).getJobInfo());
117 private void cleanupJobList() {
118 List toRemove = new Vector();
119 synchronized (jobHandlers) {
120 Iterator i = jobHandlers.iterator();
122 Calendar tresholdCalendar = new GregorianCalendar();
123 tresholdCalendar.add(Calendar.SECOND, -jobCleanupTreshold);
124 Date treshold = tresholdCalendar.getTime();
126 while (i.hasNext()) {
127 JobHandler jobHandler = (JobHandler) i.next();
129 synchronized (jobHandler) {
130 if (jobHandler.isFinished() && jobHandler.getLastChange().before(treshold)) {
131 toRemove.add(jobHandler);
136 jobHandlers.removeAll(toRemove);
139 lastCleanup = System.currentTimeMillis();
142 private JobHandler acquirePendingJob() {
143 synchronized (jobHandlers) {
144 int priorityFound= 0;
148 Iterator i = jobHandlers.iterator();
149 while (i.hasNext()) {
150 JobHandler job = (JobHandler) i.next();
152 if (job.isPending() && (jobFound==null || priorityFound<job.getPriority())) {
154 priorityFound = job.getPriority();
162 public void cancelJobs(List aJobs) {
163 synchronized (jobHandlers) {
164 Iterator i = aJobs.iterator();
166 while (i.hasNext()) {
167 ((JobHandler) identifierToJobHandler.get(i.next())).cancelOrAbortJob();
172 public void cancelAllJobs() {
173 synchronized (jobHandlers) {
174 Iterator i = jobHandlers.iterator();
176 while (i.hasNext()) {
177 ((JobHandler) i.next()).cancelOrAbortJob();
182 public interface Job {
188 * @return <code>true</code> if terminated normally, <code>false</code> if aborted
193 public static class JobInfo {
194 private String identifier;
195 private Date lastChange;
197 private long runningTime;
198 private int priority;
199 private String description;
201 private JobInfo(String aDescription, int aStatus, Date aLastChange, String anIdentifier, long aRunningTime, int aPriority) {
202 description = aDescription;
203 lastChange = aLastChange;
205 identifier = anIdentifier;
206 priority = aPriority;
207 runningTime = aRunningTime;
210 public String getDescription() {
214 public int getStatus() {
218 public int getPriority() {
222 public Date getLastChange() {
226 public String getIdentifier() {
230 public long getRunningTime() {
235 public class JobHandler {
237 private String identifier;
238 private String description;
240 private Date lastChange;
241 private long starttime;
242 private long endtime;
244 private int priority;
245 private boolean hasRun;
247 public JobHandler(Job aJob, String anIdentifier, String aDescription, int aPriority) {
249 description = aDescription;
250 identifier = anIdentifier;
251 priority = aPriority;
252 status = STATUS_CREATED;
255 public JobHandler(Job aJob, String anIdentifier, String aDescription) {
256 this(aJob, anIdentifier, aDescription, PRIORITY_NORMAL);
259 public JobInfo getJobInfo() {
260 return new JobInfo(getDescription(), getStatus(), getLastChange(), getIdentifier(), getRunningTime(), priority);
263 private void runJob() {
264 if (setProcessing()) {
272 private void cancelOrAbortJob() {
273 synchronized (this) {
281 public int getStatus() {
287 public String getIdentifier() {
291 public String getDescription() {
295 public long getRunningTime() {
303 result = System.currentTimeMillis();
305 result = result - starttime;
312 public int getPriority() {
316 private boolean setProcessing() {
317 return setStatus(STATUS_PENDING, STATUS_PROCESSING);
320 private void setProcessed() {
321 setStatus(STATUS_PROCESSING, STATUS_PROCESSED);
324 private void setAborted() {
325 setStatus(STATUS_PROCESSING, STATUS_ABORTED);
328 private void setPending() {
329 setStatus(STATUS_CREATED, STATUS_PENDING);
332 private boolean setCancelled() {
333 return setStatus(STATUS_PENDING, STATUS_CANCELLED);
336 public boolean hasBeenProcessed() {
337 return getStatus() == STATUS_PROCESSED;
340 public boolean hasBeenAborted() {
341 return getStatus() == STATUS_ABORTED;
344 public boolean isCancelled() {
345 return getStatus() == STATUS_CANCELLED;
348 public boolean isFinished() {
349 return hasBeenProcessed() || hasBeenAborted() || isCancelled();
352 public boolean isProcessing() {
353 return getStatus() == STATUS_PROCESSING;
356 public boolean isPending() {
357 return getStatus() == STATUS_PENDING;
360 public Date getLastChange() {
361 synchronized (this) {
366 private boolean setStatus(int anOldStatus, int aNewStatus) {
368 if (status == anOldStatus) {
370 lastChange = (new GregorianCalendar()).getTime();
371 if (isProcessing()) {
372 starttime = System.currentTimeMillis();
377 endtime = System.currentTimeMillis();
388 private class JobQueueRunner implements Runnable {
389 private LoggerWrapper logger;
391 public JobQueueRunner(LoggerWrapper aLogger) {
396 logger.debug("starting JobQueueRunner");
400 JobHandler job = acquirePendingJob();
402 logger.debug(" starting job ("+job.getIdentifier()+"): " +job.getDescription());
404 logger.debug(" finished job ("+job.getIdentifier()+"): " +job.getDescription());
411 catch (InterruptedException e) {
417 logger.warn("JobQueueRunner terminated");