001: /*
002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: * $Id: Scheduler.java 3634 2007-01-08 21:42:24Z gbevin $
007: */
008: package com.uwyn.rife.scheduler;
009:
010: import com.uwyn.rife.scheduler.exceptions.*;
011:
012: import java.util.Collection;
013: import java.util.HashMap;
014:
015: public class Scheduler extends Thread {
016: private TaskManager mTaskManager = null;
017: private TaskoptionManager mTaskoptionManager = null;
018: private int mSleepTime = 500;
019: private HashMap<Object, Executor> mExecutors = null;
020:
021: public Scheduler(TaskManager taskManager,
022: TaskoptionManager taskoptionManager) {
023: super ("SCHEDULER_DEAMON");
024:
025: setDaemon(true);
026: setTaskManager(taskManager);
027: setTaskoptionManager(taskoptionManager);
028: mExecutors = new HashMap<Object, Executor>();
029: }
030:
031: public void setTaskManager(TaskManager taskManager) {
032: if (null == taskManager)
033: throw new IllegalArgumentException(
034: "taskManager can't be null.");
035:
036: mTaskManager = taskManager;
037: taskManager.setScheduler(this );
038: }
039:
040: public TaskManager getTaskManager() {
041: return mTaskManager;
042: }
043:
044: public void setTaskoptionManager(TaskoptionManager taskoptionManager) {
045: if (null == taskoptionManager)
046: throw new IllegalArgumentException(
047: "taskoptionManager can't be null.");
048:
049: mTaskoptionManager = taskoptionManager;
050: taskoptionManager.setScheduler(this );
051: }
052:
053: public TaskoptionManager getTaskoptionManager() {
054: return mTaskoptionManager;
055: }
056:
057: public boolean addExecutor(Executor executor)
058: throws SchedulerException {
059: if (null == executor)
060: throw new IllegalArgumentException(
061: "executor can't be null.");
062:
063: if (null == executor.getScheduler()) {
064: mExecutors.put(executor.getHandledTasktype(), executor);
065: executor.setScheduler(this );
066: } else if (this == executor.getScheduler()) {
067: return false;
068: } else {
069: throw new ExecutorAlreadyRegisteredException(executor);
070: }
071:
072: assert mExecutors.containsKey(executor.getHandledTasktype());
073: assert executor == mExecutors
074: .get(executor.getHandledTasktype());
075: assert this == executor.getScheduler();
076:
077: return true;
078: }
079:
080: public boolean removeExecutor(Executor executor) {
081: if (null == executor)
082: throw new IllegalArgumentException(
083: "executor can't be null.");
084:
085: if (null == mExecutors.remove(executor.getHandledTasktype())) {
086: return false;
087: }
088:
089: executor.setScheduler(null);
090:
091: assert !mExecutors.containsKey(executor.getHandledTasktype());
092: assert null == executor.getScheduler();
093:
094: return true;
095: }
096:
097: public Executor getExecutor(String tasktype) {
098: if (null == tasktype)
099: throw new IllegalArgumentException(
100: "tasktype can't be null.");
101:
102: return mExecutors.get(tasktype);
103: }
104:
105: public Collection<Executor> getExecutors() {
106: return mExecutors.values();
107: }
108:
109: public void setSleepTime(int sleeptime) {
110: if (sleeptime <= 0)
111: throw new IllegalArgumentException(
112: "sleeptime has to be bigger than 0.");
113:
114: mSleepTime = sleeptime;
115: }
116:
117: public void run() {
118: while (true) {
119: try {
120: if (!isInterrupted()) {
121: scheduleStep();
122: // Ensure that the wake up is always on an even multiplier of the
123: // sleep time, this to ensure that no drift occurs.
124: long now = System.currentTimeMillis();
125: long projected = ((System.currentTimeMillis() + mSleepTime) / mSleepTime)
126: * mSleepTime;
127: long difference = projected - now;
128:
129: Thread.sleep(difference);
130: } else {
131: break;
132: }
133: } catch (InterruptedException e) {
134: break;
135: }
136: }
137:
138: synchronized (this ) {
139: notifyAll();
140: }
141: }
142:
143: private void scheduleStep() throws SchedulerExecutionException {
144: assert mTaskManager != null;
145:
146: try {
147: Executor executor = null;
148: for (Task task : mTaskManager.getTasksToProcess()) {
149: executor = mExecutors.get(task.getType());
150: if (null != executor) {
151: executor.startTaskExecution(task);
152: } else {
153: throw new NoExecutorForTasktypeException(task
154: .getType());
155: }
156: }
157: } catch (TaskManagerException e) {
158: throw new UnableToRetrieveTasksToProcessException(e);
159: }
160: }
161: }
|