欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

quartz 一个JobDetail 如何设置多个触发器  

程序员文章站 2024-01-22 09:23:52
...
http://www.iteye.com/problems/47143
     	
                  JobDetail jobDetail = new JobDetail(jobName, jobGroupName, class1.getClass());
                	Trigger trigger3 =TriggerUtils.makeMinutelyTrigger("trigger2", 10, SimpleTrigger.REPEAT_INDEFINITELY);
                	Trigger trigger4 =TriggerUtils.makeSecondlyTrigger("trigger1",5000, SimpleTrigger.REPEAT_INDEFINITELY);
                	scheduler.scheduleJob(jobDetail, trigger3);
                	[color=red]scheduler.scheduleJob(jobDetail, trigger4);[/color]                

红色处报异常了。。。
Unable to store Job with name: '1000000000' and group: 'jobDetailGroup2', because one already exists with this identification. 
org.quartz.ObjectAlreadyExistsException: Unable to store Job with name: '1000000000' and group: 'jobDetailGroup2', because one already exists with this identification. 
at org.quartz.impl.jdbcjobstore.JobStoreSupport.storeJob(JobStoreSupport.java:1098) 
at org.quartz.impl.jdbcjobstore.JobStoreSupport$3.execute(JobStoreSupport.java:1047) 
at org.quartz.impl.jdbcjobstore.JobStoreSupport$40.execute(JobStoreSupport.java:3675) 
at org.quartz.impl.jdbcjobstore.JobStoreSupport.executeInNonManagedTXLock(JobStoreSupport.java:3747)
at org.quartz.impl.jdbcjobstore.JobStoreTX.executeInLock(JobStoreTX.java:93) 
at org.quartz.impl.jdbcjobstore.JobStoreSupport.executeInLock(JobStoreSupport.java:3671) 
at org.quartz.impl.jdbcjobstore.JobStoreSupport.storeJobAndTrigger(JobStoreSupport.java:1035) 
at org.quartz.core.QuartzScheduler.scheduleJob(QuartzScheduler.java:732) 
at org.quartz.impl.StdScheduler.scheduleJob(StdScheduler.java:265) 
at com.congxing.bi.modules.alarmwarning.quartz.SimpleScheduler.registDataJobToScheduler(SimpleScheduler.java:126)
at Test.main(Test.java:17) 

A1
    JobDetail jobDetail = new JobDetail(jobName, jobGroup, class1.getClass());

    // durable, 指明任务就算没有绑定Trigger仍保留在Quartz的JobStore中,
    jobDetail.setDurable(true);

    // 加入一个任务到Quartz框架中, 等待后面再绑定Trigger,
    // 此接口中的JobDetail的durable必须为true
    scheduler.addJob(jobDetail, false);

    Trigger trigger3 = TriggerUtils.makeMinutelyTrigger("trigger2", 10, SimpleTrigger.REPEAT_INDEFINITELY);

    // 使用scheduleJob(Trigger)加入的Trigger必须指明JobName与JobGroup
    trigger3.setJobName(jobName);
    trigger3.setJobGroup(jobGroup);
    scheduler.scheduleJob(trigger3); // 看这里

    Trigger trigger4 = TriggerUtils.makeSecondlyTrigger("trigger1", 5000, SimpleTrigger.REPEAT_INDEFINITELY);
    trigger4.setJobName(jobName);
    trigger4.setJobGroup(jobGroup);
    scheduler.scheduleJob(trigger4); // 看这里

A2
    JobDetail jobDetail = new JobDetail(jobName, jobGroup, class1.getClass());
    Trigger trigger3 = TriggerUtils.makeMinutelyTrigger("trigger2", 10, SimpleTrigger.REPEAT_INDEFINITELY);
    Trigger trigger4 = TriggerUtils.makeSecondlyTrigger("trigger1", 5000, SimpleTrigger.REPEAT_INDEFINITELY);

    // 下面这个接口其实可以理解为先addJob(JobDetail, false)
    // 再调用scheduleJob(Trigger), 此时Quartz会自动校正与设置trigger3的JobName与JobGroup属性
    scheduler.scheduleJob(jobDetail, trigger3);

    // 因为任务已在上一条语句中已加入, 所以不能再使用scheduleJob(JobDetail, Trigger)
    trigger4.setJobName(jobName);
    trigger4.setJobGroup(jobGroup);
    scheduler.scheduleJob(trigger4); // 看这里

总结:
    每个任务JobDetail可以绑定多个Trigger,但一个Trigger只能绑定一个任务,这种绑定关系由四种接口来执行:
  • 1,
  • Java代码  收藏代码
    Scheduler#scheduleJob(JobDetail, Trigger) 

        该接口的作用是在将任务加入Quartz的同时绑定一个Trigger,Quartz会在加入该任务后自动设置Trigger的JobName与JobGroup为该JobDetail的name与group;
  • 2,
  • Java代码  收藏代码
    Scheduler#scheduleJob(Trigger) 

        该接口的作用是将该Trigger绑定到其JobName与JobGroup指向的任务JobDetail。这时的name与group需要在Trigger绑定前由Quartz的使用者来设置与调用
  • 3,
  • Java代码  收藏代码
    Scheduler#rescheduleJob(String, String, Trigger) 

        替换一个指定的Trigger, 即解除指定Trigger与任务的绑定,并将新的Trigger与任务绑定,Quartz会自动调整新Trigger的JobName与JobGroup,而旧的Trigger将被移除
  • 4,
  • Java代码  收藏代码
    Scheduler#triggerJob(String, String) 
    系列
        创建一个立即触发的Trigger,并将其与name与group指定的任务绑定


    JobDetail有个属性叫durable,表明该任务没有任何trigger绑定时仍保存在Quartz的JobStore中,默认为false。
    若JobDetail的durable属性为false,则任务将会从Quartz移除。