创建Scheduler 不相互影响
用默认工厂类获取scheduler 实例 code 如下:
StdSchedulerFactory schf = newStdSchedulerFactory();
scheduler = schf.getScheduler();
但多次初始化:
例如
List list = new ArrayList();
for(int i = 0;i<10;i++)
StdSchedulerFactory schf =newStdSchedulerFactory();
scheduler= schf.getScheduler();
list .add(scheduler);
}
但是获取都是一个默认的Scheduler.即调用getDefaultScheduler
若需要获得不同的scheduler for中代码如下
Properties props = new Properties();
props.put("org.quartz.scheduler.instanceName", i+"");// 不同name
props.put("org.quartz.threadPool.class","org.quartz.simpl.SimpleThreadPool");
props.put("org.quartz.threadPool.threadCount","10");
StdSchedulerFactory schf = new StdSchedulerFactory();
schf.initialize(props);
scheduler = schf.getScheduler();
Scheduler 可以经过默认的配置文件 设置Scheduler name.由此不同实例里的Scheduler
是不同的,一个Scheduler.shutdown 不再会相互影响.
第二. 灵活配置time
例子如下:
private Scheduler scheduler; // public SchedulerUtil() throws SchedulerException // { // StdSchedulerFactory schf = new StdSchedulerFactory(); // scheduler = schf.getScheduler(); // } public SchedulerUtil(String name) throws SchedulerException { Properties props = new Properties(); props.put("org.quartz.scheduler.instanceName", name); props.put("org.quartz.threadPool.class", "org.quartz.simpl.SimpleThreadPool"); props.put("org.quartz.threadPool.threadCount", "10"); StdSchedulerFactory schf = new StdSchedulerFactory(); schf.initialize(props); scheduler = schf.getScheduler(); } JobDetail jb1 = new JobDetail("job_1", "group_1", SimpleJob.class); public void taskRun()throws Exception { CronTrigger ct = new CronTrigger("trigger", "group"); ct.setCronExpression("0/1 * * * * ?"); scheduler.scheduleJob(jb1, ct); scheduler.start(); } public void modifyTaskTime()throws Exception { CronTrigger ct = (CronTrigger) scheduler.getTrigger("trigger", "group"); ct.setCronExpression("0/3 * * * * ?"); scheduler.deleteJob("job_1", "group_1"); scheduler.scheduleJob(jb1, ct); }</span>