日期:2014-05-20  浏览次数:21002 次

Quartz如何设置多任务?
现在想实现这样一个问题:
  有多个作业,每个作业的调度时间都不同,我已用Quartz实现了单作业调度,但多作业调度的问题,就不行了,麻烦哪位朋友指点指点。下面是TestDemo:
 
Java code

     public class Test_1 {
    public Scheduler createScheduler() throws Exception {
    
    SchedulerFactory sf = new StdSchedulerFactory("Quartz.properties");
    Scheduler scheduler = sf.getScheduler();
    scheduler.scheduleJob(createJobDetail_1(), createCronTrigger_1());

[color=#FF0000]// 下面这一行,是我添加的另外一个作用的调度计划,问题就出在这[/color]
    scheduler.scheduleJob(createJobDetail_2(), createCronTrigger_2());
    return scheduler;
}

private JobDetail createJobDetail_1() {
    JobDetail job = new JobDetail(DemoJob_1.class.getName(),
            Scheduler.DEFAULT_GROUP, DemoJob_1.class);
    return job;
}

private CronTrigger createCronTrigger_1() throws ParseException {
    CronTrigger trigger = new org.quartz.CronTrigger("DEFAULT", null);
    trigger.setJobName(DemoJob_1.class.getName());
    trigger.setCronExpression(" 0/5 * * * * ? ");
    return trigger;
}


private JobDetail createJobDetail_2() {
    JobDetail job = new JobDetail(DemoJob_2.class.getName(),
            Scheduler.DEFAULT_GROUP, DemoJob_2.class);
    return job;
}


private CronTrigger createCronTrigger_2() throws ParseException {
    CronTrigger trigger = new org.quartz.CronTrigger("DEFAULT", null);
    trigger.setJobName(DemoJob_2.class.getName());
    trigger.setCronExpression(" 0/15 * * * * ? ");
    return trigger;
}
}


   

public class DemoJob_1 implements Job {
    
    @Override
    public void execute(JobExecutionContext arg0) throws JobExecutionException {
        System.out.println("DemoJob_1 demo output :" + new Date());
    }

}




public class DemoJob_2 implements Job {
    @Override
    public void execute(JobExecutionContext arg0) throws JobExecutionException {
        System.out.println("DemoJob_2 demo output :" + new Date());
    }

}



public class DemoTest {
    public static void main(String [] args) throws SchedulerException, Exception{
        Test_1 test_1 = new Test_1();
         test_1.createScheduler().start();
    }

}

    


执行的时候,抛出异常:
Exception in thread "main" org.quartz.ObjectAlreadyExistsException: Unable to store Trigger with name: 'DEFAULT' and group: 'DEFAULT', because one already exists with this identification.

有一个定时功能需要用到Quartz,刚学Quartz,忘大家帮忙!

------解决方案--------------------
哈哈!
分给我吧!
我脸皮比较厚!!!
------解决方案--------------------
http://www.java2000.net/p1643

Spring管理的定时器