·运行 ScanDirectoryJob 的多个实例
多次调用 schedulerJob() 方法,在设置上比如 Job 名称和扫描间隔名称通过参数传。因此从 createScheduler() 方法获取到 Scheduler 实例后,两个 Job(同一个类) 用不同的参数就被安排到了 Scheduler 上了。
public class MulJobScheduler {
static Log logger = LogFactory.getLog(MulJobScheduler.class);
public static void main(String[] args) {
MulJobScheduler example = new MulJobScheduler();
try {
// Create a Scheduler and schedule the Job
Scheduler scheduler = example.createScheduler();
// Jobs can be scheduled after Scheduler is running
scheduler.start();
logger.info("Scheduler started at " + new Date());
// Schedule the first Job
example.scheduleJob(scheduler, "ScanDirectory1", ScanDirectoryJob.class, "d:\\test", 10);
// Schedule the second Job
example.scheduleJob(scheduler, "ScanDirectory2", ScanDirectoryJob.class, "d:\\test", 15);
} catch (SchedulerException ex) {
logger.error(ex);
}
}
/*
* return an instance of the Scheduler from the factory
*/
public Scheduler createScheduler() throws SchedulerException {
return StdSchedulerFactory.getDefaultScheduler();
}
// Create and Schedule a ScanDirectoryJob with the Scheduler
private void scheduleJob(Scheduler scheduler, String jobName,
Class jobClass, String scanDir, int scanInterval)
throws SchedulerException {
// Create a JobDetail for the Job
JobDetail jobDetail = new JobDetail(jobName, Scheduler.DEFAULT_GROUP, jobClass);
// Configure the directory to scan
jobDetail.getJobDataMap().put("SCAN_DIR", scanDir);
// Trigger that repeats every "scanInterval" secs forever
Trigger trigger = TriggerUtils.makeSecondlyTrigger(scanInterval);
trigger.setName(jobName + "-Trigger");
// Start the trigger firing from now
trigger.setStartTime(new Date());
// Associate the trigger with the job in the scheduler
scheduler.scheduleJob(jobDetail, trigger);
}
}