KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis2 > deployment > scheduler > Scheduler


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

16
17 package org.apache.axis2.deployment.scheduler;
18
19 import java.util.Date JavaDoc;
20 import java.util.Timer JavaDoc;
21 import java.util.TimerTask JavaDoc;
22
23 public class Scheduler {
24
25     private final Timer JavaDoc timer = new Timer JavaDoc();
26
27     public class SchedulerTimerTask extends TimerTask JavaDoc {
28
29         private SchedulerTask schedulerTask;
30         private DeploymentIterator iterator;
31
32
33         public SchedulerTimerTask(SchedulerTask schedulerTask, DeploymentIterator iterator) {
34             this.schedulerTask = schedulerTask;
35             this.iterator = iterator;
36         }
37
38         public void run() {
39             schedulerTask.run();
40             reschedule(schedulerTask, iterator);
41         }
42     }
43
44     /**
45      * Schedules the specified task for execution according to the specified schedule.
46      * If times specified by the <code>ScheduleIterator</code> are in the past they are
47      * scheduled for immediate execution.
48      *
49      * @param schedulerTask task to be scheduled
50      * @param iterator iterator that describes the schedule
51      * @throws IllegalStateException if task was already scheduled or cancelled,
52      * scheduler was cancelled, or scheduler thread terminated.
53      */

54
55     public void schedule(SchedulerTask schedulerTask, DeploymentIterator iterator) {
56
57         Date JavaDoc time = iterator.next();
58         if (time == null) {
59             schedulerTask.cancel();
60         } else {
61             synchronized (schedulerTask.lock) {
62                 schedulerTask.state = SchedulerTask.SCHEDULED;
63                 schedulerTask.timerTask =
64                         new SchedulerTimerTask(schedulerTask, iterator);
65                 timer.schedule(schedulerTask.timerTask, time);
66             }
67         }
68     }
69
70     private void reschedule(SchedulerTask schedulerTask, DeploymentIterator iterator) {
71         Date JavaDoc time = iterator.next();
72         if (time == null) {
73             schedulerTask.cancel();
74         } else {
75             synchronized (schedulerTask.lock) {
76                 if (schedulerTask.state != SchedulerTask.CANCELLED) {
77                     schedulerTask.timerTask =
78                             new SchedulerTimerTask(schedulerTask, iterator);
79                     timer.schedule(schedulerTask.timerTask, time);
80                 }
81             }
82         }
83     }
84
85 }
86
Popular Tags