| 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 |
9 |
public class Scheduler |
| 48 |
|
implements Configurable, Startable |
| 49 |
|
{ |
| 50 |
|
|
| 51 |
27 |
private static Category log = Category.getInstance(Scheduler.class); |
| 52 |
2 |
|
| 53 |
|
|
| 54 |
9 |
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 |
9 |
scheduler.start(); |
| 88 |
9 |
} |
| 89 |
|
|
| 90 |
|
|
| 91 |
|
|
| 92 |
2 |
|
| 93 |
2 |
|
| 94 |
|
|
| 95 |
|
public void stop() |
| 96 |
|
throws Exception |
| 97 |
|
{ |
| 98 |
9 |
scheduler.shutdown(); |
| 99 |
9 |
} |
| 100 |
|
|
| 101 |
|
|
| 102 |
2 |
|
| 103 |
2 |
|
| 104 |
|
|
| 105 |
|
|
| 106 |
|
|
| 107 |
|
|
| 108 |
|
public void configure(Configuration configuration) |
| 109 |
|
throws NestableException |
| 110 |
|
{ |
| 111 |
9 |
String xmlLocation = configuration.getString("scheduler.config.location"); |
| 112 |
|
|
| 113 |
2 |
try |
| 114 |
|
{ |
| 115 |
|
|
| 116 |
|
|
| 117 |
9 |
SchedulerConfig schedConfig = getSchedulerConfig(xmlLocation); |
| 118 |
|
Properties factoryProps; |
| 119 |
2 |
|
| 120 |
|
try |
| 121 |
|
{ |
| 122 |
9 |
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 |
9 |
} |
| 142 |
|
|
| 143 |
9 |
schedulerFactory = new StdSchedulerFactory(); |
| 144 |
9 |
schedulerFactory.initialize(factoryProps); |
| 145 |
2 |
|
| 146 |
9 |
scheduler = (StdScheduler) schedulerFactory.getScheduler(); |
| 147 |
2 |
|
| 148 |
11 |
List jobConfigs = schedConfig.getJobConfigs(); |
| 149 |
|
|
| 150 |
11 |
if (!jobConfigs.isEmpty()) |
| 151 |
|
{ |
| 152 |
29 |
for (int i = 0; i < jobConfigs.size(); i++) |
| 153 |
|
{ |
| 154 |
20 |
JobConfig jobConf = (JobConfig) jobConfigs.get(i); |
| 155 |
18 |
String jobName = (String) jobConf.getName(); |
| 156 |
6 |
|
| 157 |
18 |
String jobGroup = jobConf.getGroup(); |
| 158 |
22 |
String jobClassName = jobConf.getClassName(); |
| 159 |
4 |
|
| 160 |
18 |
JobDetail job = new JobDetail(jobName, jobGroup, Class.forName(jobClassName)); |
| 161 |
4 |
|
| 162 |
4 |
|
| 163 |
20 |
TriggerConfig triggerConf = getTriggerConfig(schedConfig, job.getName()); |
| 164 |
2 |
|
| 165 |
20 |
if (triggerConf != null) |
| 166 |
|
{ |
| 167 |
18 |
job.getName(); |
| 168 |
2 |
|
| 169 |
20 |
String triggerGroup = triggerConf.getGroup(); |
| 170 |
20 |
String triggerName = triggerConf.getName(); |
| 171 |
20 |
String cronExpression = |
| 172 |
2 |
triggerConf.getSeconds() + " " + triggerConf.getMinutes() + " " + triggerConf.getHours() + " " |
| 173 |
2 |
+ triggerConf.getDayOfMonth() + " " + triggerConf.getMonth() + " " + triggerConf.getDayOfWeek(); |
| 174 |
2 |
|
| 175 |
22 |
CronTrigger cronTrigger = new CronTrigger(triggerName, triggerGroup, jobName, jobGroup, cronExpression); |
| 176 |
4 |
|
| 177 |
20 |
scheduler.scheduleJob(job, cronTrigger); |
| 178 |
18 |
triggerConf = null; |
| 179 |
18 |
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 |
9 |
} |
| 196 |
9 |
} |
| 197 |
|
|
| 198 |
|
|
| 199 |
|
|
| 200 |
|
|
| 201 |
|
|
| 202 |
|
|
| 203 |
|
|
| 204 |
|
|
| 205 |
|
private Properties getFactoryProps(SchedulerConfig schdConf) |
| 206 |
|
{ |
| 207 |
9 |
Properties props = new Properties(); |
| 208 |
1 |
|
| 209 |
11 |
props.put("org.quartz.scheduler.instanceName", schdConf.getInstanceName()); |
| 210 |
1 |
|
| 211 |
9 |
ThreadPoolConfig threadPool = schdConf.getThreadPoolConfig(); |
| 212 |
9 |
props.put("org.quartz.threadPool.class", threadPool.getClassName()); |
| 213 |
9 |
props.put("org.quartz.threadPool.threadCount", threadPool.getThreadCount()); |
| 214 |
9 |
props.put("org.quartz.threadPool.threadPriority", threadPool.getThreadPriority()); |
| 215 |
|
|
| 216 |
10 |
JobStoreConfig jobStore = schdConf.getJobStoreConfig(); |
| 217 |
10 |
props.put("org.quartz.jobStore.class", jobStore.getClassName()); |
| 218 |
1 |
|
| 219 |
10 |
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 |
9 |
FileInputStream in = new FileInputStream(xmlLocation); |
| 235 |
|
|
| 236 |
|
|
| 237 |
9 |
BeanReader reader = createBeanReader(); |
| 238 |
1 |
|
| 239 |
11 |
SchedulerConfig schedConf = (SchedulerConfig) reader.parse(in); |
| 240 |
1 |
|
| 241 |
10 |
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 |
10 |
BeanReader reader = new BeanReader(); |
| 255 |
9 |
reader.setXMLIntrospector(createXMLIntrospector()); |
| 256 |
9 |
reader.registerBeanClass(SchedulerConfig.class); |
| 257 |
|
|
| 258 |
9 |
return reader; |
| 259 |
|
} |
| 260 |
|
|
| 261 |
|
|
| 262 |
|
|
| 263 |
|
|
| 264 |
|
|
| 265 |
1 |
|
| 266 |
1 |
private XMLIntrospector createXMLIntrospector() |
| 267 |
|
{ |
| 268 |
10 |
XMLIntrospector introspector = new XMLIntrospector(); |
| 269 |
1 |
|
| 270 |
1 |
|
| 271 |
11 |
introspector.getConfiguration().setAttributesForPrimitives(false); |
| 272 |
1 |
|
| 273 |
|
|
| 274 |
|
|
| 275 |
|
|
| 276 |
9 |
introspector.getConfiguration().setElementNameMapper(new DecapitalizeNameMapper()); |
| 277 |
8 |
introspector.getConfiguration().setAttributeNameMapper(new DecapitalizeNameMapper()); |
| 278 |
1 |
|
| 279 |
8 |
introspector.getConfiguration().setPluralStemmer(new DefaultPluralStemmer()); |
| 280 |
1 |
|
| 281 |
8 |
return introspector; |
| 282 |
1 |
} |
| 283 |
2 |
|
| 284 |
2 |
private TriggerConfig getTriggerConfig(SchedulerConfig schedConf, String jobName) |
| 285 |
4 |
{ |
| 286 |
19 |
TriggerConfig trig = null; |
| 287 |
16 |
List triggers = schedConf.getTriggerConfigs(); |
| 288 |
3 |
|
| 289 |
24 |
for (int i = 0; i < triggers.size(); i++) |
| 290 |
3 |
{ |
| 291 |
24 |
TriggerConfig tmpTrig = (TriggerConfig) triggers.get(i); |
| 292 |
3 |
|
| 293 |
24 |
if (tmpTrig.getJobName().equals(jobName)) |
| 294 |
2 |
{ |
| 295 |
17 |
trig = tmpTrig; |
| 296 |
3 |
|
| 297 |
16 |
return trig; |
| 298 |
1 |
} |
| 299 |
1 |
} |
| 300 |
|
|
| 301 |
0 |
return trig; |
| 302 |
|
} |
| 303 |
|
} |