KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sape > carbon > services > scheduler > DefaultSchedulerServiceImpl


1 /*
2  * The contents of this file are subject to the Sapient Public License
3  * Version 1.0 (the "License"); you may not use this file except in compliance
4  * with the License. You may obtain a copy of the License at
5  * http://carbon.sf.net/License.html.
6  *
7  * Software distributed under the License is distributed on an "AS IS" basis,
8  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
9  * the specific language governing rights and limitations under the License.
10  *
11  * The Original Code is The Carbon Component Framework.
12  *
13  * The Initial Developer of the Original Code is Sapient Corporation
14  *
15  * Copyright (C) 2003 Sapient Corporation. All Rights Reserved.
16  */

17
18 package org.sape.carbon.services.scheduler;
19
20 import java.util.ArrayList JavaDoc;
21 import java.util.Collection JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.Timer JavaDoc;
24
25 import org.sape.carbon.core.component.ComponentConfiguration;
26 import org.sape.carbon.core.component.lifecycle.Configurable;
27 import org.sape.carbon.core.component.lifecycle.Startable;
28 import org.sape.carbon.core.component.lifecycle.Suspendable;
29
30 import org.sape.carbon.services.threadpool.ThreadPool;
31
32 import org.apache.commons.logging.Log;
33 import org.apache.commons.logging.LogFactory;
34
35
36 /**
37  * Default implementation for the SchedulerService. This implementation
38  * uses java.util.Timer to schedule tasks. When this service is stopped
39  * or suspended, all tasks are canceled. When this service is started or
40  * resumed, all configured tasks are scheduled.
41  *
42  * @see java.util.Timer
43  *
44  * Copyright 2002 Sapient
45  * @since carbon 1.0
46  * @author Douglas Voet, June 2002
47  * @version $Revision: 1.12 $($Author: dvoet $ / $Date: 2003/11/20 21:46:15 $)
48  */

49 public class DefaultSchedulerServiceImpl
50     implements SchedulerService, Configurable, Startable, Suspendable {
51
52     /**
53      * Provides a handle to Apache-commons logger
54      */

55     private Log log =
56         LogFactory.getLog(this.getClass());
57
58     /** Holds the timer object that will execute the tasks. */
59     private Timer JavaDoc internalTimer;
60
61     /** Holds the collection of scheduled tasks for this service. */
62     private Collection JavaDoc scheduledTasks = new ArrayList JavaDoc();
63     
64     /**
65      * @see Configurable#configure(ComponentConfiguration)
66      */

67     public void configure(ComponentConfiguration configuration) {
68         SchedulerServiceConfiguration schedulerConfiguration =
69             (SchedulerServiceConfiguration) configuration;
70             
71         ThreadPool threadPool = schedulerConfiguration.getThreadPool();
72
73         scheduledTasks.clear();
74
75         FixedRateTaskConfiguration[] fixedRateTasks =
76             schedulerConfiguration.getFixedRateTask();
77         for (int i = 0; i < fixedRateTasks.length; i++) {
78             scheduledTasks.add(new FixedRateTask(fixedRateTasks[i], threadPool));
79         }
80
81         FixedDelayTaskConfiguration[] fixedDelayTasks =
82             schedulerConfiguration.getFixedDelayTask();
83         for (int i = 0; i < fixedDelayTasks.length; i++) {
84             scheduledTasks.add(new FixedDelayTask(fixedDelayTasks[i], threadPool));
85         }
86     }
87
88     /**
89      * @see Startable#start()
90      */

91     public void start() {
92         scheduleAllTasks();
93     }
94
95     /**
96      * @see Startable#stop()
97      */

98     public void stop() {
99         if (log.isTraceEnabled()) {
100             log.trace("stopping scheduler");
101         }
102         internalTimer.cancel();
103     }
104
105     /**
106      * @see Suspendable#resume()
107      */

108     public void resume() {
109         scheduleAllTasks();
110     }
111
112     /**
113      * @see Suspendable#suspend()
114      */

115     public void suspend() {
116         internalTimer.cancel();
117     }
118
119     /**
120      * Iterates over tasks and schedules them.
121      */

122     private void scheduleAllTasks() {
123         // create a new timer and make it a daemon so it won't keep the
124
// system from shutting down
125
this.internalTimer = new Timer JavaDoc(true);
126
127         Iterator JavaDoc taskIterator = this.scheduledTasks.iterator();
128
129         while (taskIterator.hasNext()) {
130             AbstractTask task = (AbstractTask) taskIterator.next();
131             task.schedule(this.internalTimer);
132         }
133     }
134 }
135
Popular Tags