KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > components > varscheduler > Scheduler


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.servicemix.components.varscheduler;
18
19 import java.util.Date JavaDoc;
20 import java.util.Timer JavaDoc;
21 import java.util.TimerTask JavaDoc;
22
23 /**
24  * Class to handle scheduling tasks.
25  * <p>
26  * This class is thread-safe
27  * <p>
28  *
29  * @author George Gastaldi (gastaldi)
30  */

31 public class Scheduler {
32
33     private Timer JavaDoc timer;
34
35     /**
36      * Creates a new Scheduler.
37      */

38     public Scheduler() {
39         this.timer = new Timer JavaDoc();
40     }
41
42     /**
43      * Creates a new Daemon Scheduler
44      * @param daemon Thread must be executed as "daemon".
45      */

46     public Scheduler(boolean daemon) {
47         this.timer = new Timer JavaDoc(daemon);
48     }
49
50     /**
51      * Cancels the scheduler task
52      */

53     public void cancel() {
54         timer.cancel();
55     }
56
57     /**
58      * Schedules a task
59      *
60      * @param task scheduled tasl
61      * @param iterator iterator for schedulingque descreve o agendamento
62      * @throws IllegalStateException if task scheduled or canceled
63      */

64     public void schedule(SchedulerTask task, ScheduleIterator iterator) {
65         Date JavaDoc time = iterator.nextExecution();
66         if (time == null) {
67             task.cancel();
68         } else {
69             synchronized (task.lock) {
70                 if (task.state != SchedulerTask.VIRGIN) {
71                     throw new IllegalStateException JavaDoc(
72                         "Task already scheduled or cancelled");
73                 }
74                 task.state = SchedulerTask.SCHEDULED;
75                 task.timerTask = new SchedulerTimerTask(task, iterator);
76                 timer.schedule(task.timerTask, time);
77             }
78         }
79     }
80
81     private void reschedule(SchedulerTask task, ScheduleIterator iterator) {
82         Date JavaDoc time = iterator.nextExecution();
83         if (time == null) {
84             task.cancel();
85         } else {
86             synchronized (task.lock) {
87                 if (task.state != SchedulerTask.CANCELLED) {
88                     task.timerTask = new SchedulerTimerTask(task, iterator);
89                     timer.schedule(task.timerTask, time);
90                 }
91             }
92         }
93     }
94
95     /**
96      * Internal TimerTask instance
97      */

98     class SchedulerTimerTask extends TimerTask JavaDoc {
99         private SchedulerTask task;
100         private ScheduleIterator iterator;
101         
102         public SchedulerTimerTask(SchedulerTask task,ScheduleIterator iterator){
103             this.task = task;
104             this.iterator = iterator;
105         }
106         
107         public void run() {
108             task.run();
109             reschedule(task, iterator);
110         }
111     }
112
113 }
114
115
Popular Tags