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

Spring Quartz2 动态任务的实例详解

程序员文章站 2023-11-07 11:18:34
spring quartz2 动态任务的实例详解 此处使用的是quartz中simpleschedulebuilder类,非cronschedulebuilder,cro...

spring quartz2 动态任务的实例详解

此处使用的是quartz中simpleschedulebuilder类,非cronschedulebuilder,cronschedulebuilder是cron表达式的。具体请自行百度。

实现代码: 

/** 
   * 新增任务 
   * @param schedulejob 
   * @throws exception 
   */ 
  @override 
  @suppresswarnings("unchecked") 
  public void addjobsimple(schedulejob schedulejob) throws exception{ 
    triggerkey triggerkey = triggerkey.triggerkey(schedulejob.getjobname(), schedulejob.getjobgroup()); 
    //任务触发 
    trigger trigger = scheduler.gettrigger(triggerkey); 
    if (null == trigger) { 
      jobdetail jobdetail = jobbuilder.newjob((class<? extends job>) class.forname(schedulejob.getclazz())) 
       .withidentity(schedulejob.getjobname(), schedulejob.getjobgroup()).build(); 
      jobdetail.getjobdatamap().put("schedulejob", schedulejob); 
       
      simpleschedulebuilder simpleschedulebuilder = simpleschedulebuilder.simpleschedule(); 
      if("秒".equals(schedulejob.gettimetype()) ||  
          "second".equalsignorecase(schedulejob.gettimetype())){ 
        simpleschedulebuilder.withintervalinseconds(schedulejob.gettimevalue()).repeatforever(); 
      }else if("分".equals(schedulejob.gettimetype()) || "分钟".equals(schedulejob.gettimetype()) ||  
          "minute".equalsignorecase(schedulejob.gettimetype())){ 
        simpleschedulebuilder.withintervalinminutes(schedulejob.gettimevalue()).repeatforever(); 
      }else if("时".equals(schedulejob.gettimetype()) || "小时".equals(schedulejob.gettimetype()) ||  
          "hour".equalsignorecase(schedulejob.gettimetype())){ 
        simpleschedulebuilder.withintervalinhours(schedulejob.gettimevalue()).repeatforever(); 
      }else if("天".equals(schedulejob.gettimetype()) ||  
          "date".equalsignorecase(schedulejob.gettimetype())){ 
        simpleschedulebuilder.withintervalinhours(schedulejob.gettimevalue()).repeatforever(); 
      } 
      schedulebuilder<simpletrigger> schedbuilder = simpleschedulebuilder.withmisfirehandlinginstructionnextwithremainingcount(); 
       
      trigger =triggerbuilder.newtrigger().withidentity(schedulejob.getjobname(), schedulejob.getjobgroup()) 
          .withschedule(schedbuilder).build(); 
      trigger = triggerbuilder.newtrigger().withidentity(schedulejob.getjobname(), schedulejob.getjobgroup()).withschedule(schedbuilder).build(); 
      scheduler.schedulejob(jobdetail, trigger); 
      log.info(cc.log_prefix + "新增简单任务:"+jasonutils.object2string(schedulejob)); 
    }else { 
      this.updatejobcron(schedulejob); 
    } 
  } 

需要注意是的,当定时任务只执行一次时,就要仔细看代码,如上面的简单定时任务,刚开始是我这样的:

simpleschedulebuilder.withintervalinseconds(schedulejob.gettimevalue()); 

结果只执行一次,而我需要的是不断循环执行,最后改成:

simpleschedulebuilder.withintervalinseconds(schedulejob.gettimevalue()).repeatforever(); 

可以看出,后面多了个:repeatforever(),表示永远重复。 

如果是希望执行多少次后不执行,就使用:.withrepeatcount(triggerrepeatcount),如下:

simpleschedulebuilder.withintervalinhours(schedulejob.gettimevalue()).withrepeatcount(triggerrepeatcount); 

如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!