public static void schedTest(SchedulerFactory sf) throws Exception {
final Log lg = LogFactory.getLog(InterruptableJobTest.class);
lg.info("------- Initializing -------------------");
final Scheduler sched = sf.getScheduler();
lg.info("------- Initializing 2 -------------------");
// remove job/trigger entries that may be lingering in a JDBCJobStore
// from
// previous run of this example... (obviously this doesn't matter for
// RAMJobStore -- but is necessary for JDBCJobStore because this
// example
// program is 'dumb' - it blindly inserts the same jobs every time it
// executes - even though they may still be in the JobStore)
String[] groups = sched.getTriggerGroupNames();
for (int i = 0; i < groups.length; i++) {
String[] names = sched.getTriggerNames(groups[i]);
for (int j = 0; j < names.length; j++)
sched.unscheduleJob(names[j], groups[i]);
}
groups = sched.getJobGroupNames();
for (int i = 0; i < groups.length; i++) {
String[] names = sched.getJobNames(groups[i]);
for (int j = 0; j < names.length; j++)
sched.deleteJob(names[j], groups[i]);
}
lg.info("------- Initialization Complete -----------");
lg.info("------- Scheduling Jobs -----------");
// jobs can be scheduled before start() has been called
long ts = TriggerUtils.getNextGivenSecondDate(null, 5).getTime(); // get
// a
// 'nice
// round'
// time
// a
// few
// seconds
// in
// the
// future...
JobDetail job = new JobDetail("interruptableJob1", "group1",
DumbInterruptableJob.class);
job.getJobDataMap().put(StatefulDumbJob.EXECUTION_DELAY, 10000l);
SimpleTrigger trigger = new SimpleTrigger("trigg1", "group1", new Date(
ts), null, SimpleTrigger.REPEAT_INDEFINITELY, 5000l);
Date ft = sched.scheduleJob(job, trigger);
lg.info(job.getFullName() + " will run at: " + ft + " & repeat: "
+ trigger.getRepeatCount() + "/" + trigger.getRepeatInterval());
lg.info("------- Starting Scheduler ----------------");
// jobs don't start firing until start() has been called...
sched.start();
lg.info("------- Started Scheduler -----------------");
lg.info("------- Starting loop to interrupt job every 7 seconds ----------");
for(int i=0; i < 50; i++) {
try {
Thread.sleep(7000l);
sched.interrupt(job.getName(), job.getGroup());
} catch (Exception e) {
}
}
lg.info("------- Shutting Down ---------------------");
sched.shutdown(true);
lg.info("------- Shutdown Complete -----------------");
SchedulerMetaData metaData = sched.getMetaData();
lg.info("Executed " + metaData.numJobsExecuted() + " jobs.");
}
|