1 |
|
package org.apache.stratum.scheduler; |
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
import java.io.FileInputStream; |
21 |
|
import java.util.List; |
22 |
|
import java.util.Properties; |
23 |
|
|
24 |
|
import org.apache.commons.betwixt.XMLIntrospector; |
25 |
|
import org.apache.commons.betwixt.io.BeanReader; |
26 |
|
import org.apache.commons.betwixt.strategy.DecapitalizeNameMapper; |
27 |
|
import org.apache.commons.betwixt.strategy.DefaultPluralStemmer; |
28 |
|
import org.apache.commons.configuration.Configuration; |
29 |
|
import org.apache.commons.configuration.PropertiesConfiguration; |
30 |
|
import org.apache.commons.lang.exception.NestableException; |
31 |
|
import org.apache.log4j.Category; |
32 |
|
import org.apache.stratum.lifecycle.Configurable; |
33 |
|
import org.apache.stratum.lifecycle.Startable; |
34 |
|
|
35 |
|
import org.quartz.CronTrigger; |
36 |
|
import org.quartz.JobDetail; |
37 |
|
import org.quartz.SchedulerException; |
38 |
|
import org.quartz.impl.StdScheduler; |
39 |
|
import org.quartz.impl.StdSchedulerFactory; |
40 |
|
|
41 |
|
|
42 |
|
|
43 |
|
|
44 |
|
|
45 |
|
|
46 |
|
|
47 |
10 |
public class Scheduler |
48 |
|
implements Configurable, Startable |
49 |
|
{ |
50 |
|
|
51 |
30 |
private static Category log = Category.getInstance(Scheduler.class); |
52 |
2 |
|
53 |
|
|
54 |
10 |
private static String confLocation = "/projects/jakarta-turbine-stratum/src/test-conf/Scheduler.properties"; |
55 |
|
|
56 |
|
|
57 |
|
private StdSchedulerFactory schedulerFactory; |
58 |
|
|
59 |
6 |
|
60 |
|
protected StdScheduler scheduler; |
61 |
|
|
62 |
2 |
|
63 |
|
|
64 |
|
|
65 |
|
|
66 |
|
|
67 |
|
|
68 |
|
|
69 |
|
|
70 |
|
public static void main(String [] args) |
71 |
|
throws Exception |
72 |
|
{ |
73 |
0 |
Scheduler sched = new Scheduler(); |
74 |
0 |
PropertiesConfiguration conf = new PropertiesConfiguration(confLocation); |
75 |
0 |
sched.configure(conf); |
76 |
0 |
sched.start(); |
77 |
0 |
} |
78 |
|
|
79 |
|
|
80 |
|
|
81 |
|
|
82 |
|
|
83 |
|
|
84 |
|
public void start() |
85 |
|
throws Exception |
86 |
|
{ |
87 |
10 |
scheduler.start(); |
88 |
10 |
} |
89 |
|
|
90 |
|
|
91 |
|
|
92 |
2 |
|
93 |
2 |
|
94 |
|
|
95 |
|
public void stop() |
96 |
|
throws Exception |
97 |
|
{ |
98 |
10 |
scheduler.shutdown(); |
99 |
10 |
} |
100 |
|
|
101 |
|
|
102 |
2 |
|
103 |
2 |
|
104 |
|
|
105 |
|
|
106 |
|
|
107 |
|
|
108 |
|
public void configure(Configuration configuration) |
109 |
|
throws NestableException |
110 |
|
{ |
111 |
10 |
String xmlLocation = configuration.getString("scheduler.config.location"); |
112 |
|
|
113 |
2 |
try |
114 |
|
{ |
115 |
|
|
116 |
|
|
117 |
10 |
SchedulerConfig schedConfig = getSchedulerConfig(xmlLocation); |
118 |
|
Properties factoryProps; |
119 |
2 |
|
120 |
|
try |
121 |
|
{ |
122 |
10 |
factoryProps = getFactoryProps(schedConfig); |
123 |
|
} |
124 |
2 |
catch (NullPointerException ex) |
125 |
|
{ |
126 |
0 |
log.error("Error loading properties to initialize" |
127 |
|
+ " Scheduler Factory. Please make sure the following" |
128 |
|
+ " elements are properly filled out in the" |
129 |
|
+ " Scheduler .xml config file:\n" |
130 |
|
+ " <instanceName></instanceName>\n" |
131 |
|
+ " <threadPoolConfig>\n" |
132 |
|
+ " <className></className>\n" |
133 |
|
+ " <threadCount></threadCount>\n" |
134 |
|
+ " <threadPriority></threadPriority>\n" |
135 |
|
+ " </threadPoolConfig>\n" |
136 |
|
+ " <jobStoreConfig>\n" |
137 |
|
+ " <className></className>\n" |
138 |
|
+ " </jobStoreConfig>"); |
139 |
|
|
140 |
0 |
return; |
141 |
10 |
} |
142 |
|
|
143 |
10 |
schedulerFactory = new StdSchedulerFactory(); |
144 |
10 |
schedulerFactory.initialize(factoryProps); |
145 |
2 |
|
146 |
10 |
scheduler = (StdScheduler) schedulerFactory.getScheduler(); |
147 |
2 |
|
148 |
12 |
List jobConfigs = schedConfig.getJobConfigs(); |
149 |
|
|
150 |
12 |
if (!jobConfigs.isEmpty()) |
151 |
|
{ |
152 |
32 |
for (int i = 0; i < jobConfigs.size(); i++) |
153 |
|
{ |
154 |
22 |
JobConfig jobConf = (JobConfig) jobConfigs.get(i); |
155 |
20 |
String jobName = (String) jobConf.getName(); |
156 |
6 |
|
157 |
20 |
String jobGroup = jobConf.getGroup(); |
158 |
24 |
String jobClassName = jobConf.getClassName(); |
159 |
4 |
|
160 |
20 |
JobDetail job = new JobDetail(jobName, jobGroup, Class.forName(jobClassName)); |
161 |
4 |
|
162 |
4 |
|
163 |
22 |
TriggerConfig triggerConf = getTriggerConfig(schedConfig, job.getName()); |
164 |
2 |
|
165 |
22 |
if (triggerConf != null) |
166 |
|
{ |
167 |
20 |
job.getName(); |
168 |
2 |
|
169 |
22 |
String triggerGroup = triggerConf.getGroup(); |
170 |
22 |
String triggerName = triggerConf.getName(); |
171 |
22 |
String cronExpression = |
172 |
2 |
triggerConf.getSeconds() + " " + triggerConf.getMinutes() + " " + triggerConf.getHours() + " " |
173 |
2 |
+ triggerConf.getDayOfMonth() + " " + triggerConf.getMonth() + " " + triggerConf.getDayOfWeek(); |
174 |
2 |
|
175 |
24 |
CronTrigger cronTrigger = new CronTrigger(triggerName, triggerGroup, jobName, jobGroup, cronExpression); |
176 |
4 |
|
177 |
22 |
scheduler.scheduleJob(job, cronTrigger); |
178 |
20 |
triggerConf = null; |
179 |
20 |
cronTrigger = null; |
180 |
|
} |
181 |
|
} |
182 |
|
} |
183 |
|
} |
184 |
2 |
catch (SchedulerException ex) |
185 |
2 |
{ |
186 |
0 |
log.error("Error Initializing Scheduler: " + ex.toString(), ex); |
187 |
|
} |
188 |
0 |
catch (ClassNotFoundException ex) |
189 |
|
{ |
190 |
2 |
log.error("Error Loading class: " + ex.toString(), ex); |
191 |
4 |
} |
192 |
4 |
catch (Exception ex) |
193 |
2 |
{ |
194 |
0 |
log.error("Error: " + ex.toString(), ex); |
195 |
10 |
} |
196 |
10 |
} |
197 |
|
|
198 |
|
|
199 |
|
|
200 |
|
|
201 |
|
|
202 |
|
|
203 |
|
|
204 |
|
|
205 |
|
private Properties getFactoryProps(SchedulerConfig schdConf) |
206 |
|
{ |
207 |
10 |
Properties props = new Properties(); |
208 |
1 |
|
209 |
12 |
props.put("org.quartz.scheduler.instanceName", schdConf.getInstanceName()); |
210 |
1 |
|
211 |
10 |
ThreadPoolConfig threadPool = schdConf.getThreadPoolConfig(); |
212 |
10 |
props.put("org.quartz.threadPool.class", threadPool.getClassName()); |
213 |
10 |
props.put("org.quartz.threadPool.threadCount", threadPool.getThreadCount()); |
214 |
10 |
props.put("org.quartz.threadPool.threadPriority", threadPool.getThreadPriority()); |
215 |
|
|
216 |
11 |
JobStoreConfig jobStore = schdConf.getJobStoreConfig(); |
217 |
11 |
props.put("org.quartz.jobStore.class", jobStore.getClassName()); |
218 |
1 |
|
219 |
11 |
return props; |
220 |
1 |
} |
221 |
1 |
|
222 |
|
|
223 |
1 |
|
224 |
3 |
|
225 |
2 |
|
226 |
1 |
|
227 |
2 |
|
228 |
2 |
|
229 |
2 |
|
230 |
2 |
|
231 |
1 |
private SchedulerConfig getSchedulerConfig(String xmlLocation) |
232 |
1 |
throws Exception |
233 |
1 |
{ |
234 |
10 |
FileInputStream in = new FileInputStream(xmlLocation); |
235 |
|
|
236 |
|
|
237 |
10 |
BeanReader reader = createBeanReader(); |
238 |
1 |
|
239 |
12 |
SchedulerConfig schedConf = (SchedulerConfig) reader.parse(in); |
240 |
1 |
|
241 |
11 |
return schedConf; |
242 |
1 |
} |
243 |
1 |
|
244 |
1 |
|
245 |
1 |
|
246 |
1 |
|
247 |
|
|
248 |
1 |
|
249 |
2 |
|
250 |
1 |
|
251 |
1 |
private BeanReader createBeanReader() |
252 |
1 |
throws Exception |
253 |
1 |
{ |
254 |
11 |
BeanReader reader = new BeanReader(); |
255 |
10 |
reader.setXMLIntrospector(createXMLIntrospector()); |
256 |
10 |
reader.registerBeanClass(SchedulerConfig.class); |
257 |
|
|
258 |
10 |
return reader; |
259 |
|
} |
260 |
|
|
261 |
|
|
262 |
|
|
263 |
|
|
264 |
|
|
265 |
1 |
|
266 |
1 |
private XMLIntrospector createXMLIntrospector() |
267 |
|
{ |
268 |
11 |
XMLIntrospector introspector = new XMLIntrospector(); |
269 |
1 |
|
270 |
1 |
|
271 |
12 |
introspector.getConfiguration().setAttributesForPrimitives(false); |
272 |
1 |
|
273 |
|
|
274 |
|
|
275 |
|
|
276 |
10 |
introspector.getConfiguration().setElementNameMapper(new DecapitalizeNameMapper()); |
277 |
9 |
introspector.getConfiguration().setAttributeNameMapper(new DecapitalizeNameMapper()); |
278 |
1 |
|
279 |
9 |
introspector.getConfiguration().setPluralStemmer(new DefaultPluralStemmer()); |
280 |
1 |
|
281 |
9 |
return introspector; |
282 |
1 |
} |
283 |
2 |
|
284 |
2 |
private TriggerConfig getTriggerConfig(SchedulerConfig schedConf, String jobName) |
285 |
4 |
{ |
286 |
21 |
TriggerConfig trig = null; |
287 |
18 |
List triggers = schedConf.getTriggerConfigs(); |
288 |
3 |
|
289 |
27 |
for (int i = 0; i < triggers.size(); i++) |
290 |
3 |
{ |
291 |
27 |
TriggerConfig tmpTrig = (TriggerConfig) triggers.get(i); |
292 |
3 |
|
293 |
27 |
if (tmpTrig.getJobName().equals(jobName)) |
294 |
2 |
{ |
295 |
19 |
trig = tmpTrig; |
296 |
3 |
|
297 |
18 |
return trig; |
298 |
1 |
} |
299 |
1 |
} |
300 |
|
|
301 |
0 |
return trig; |
302 |
|
} |
303 |
|
} |