SoFunction
Updated on 2025-03-06

Detailed explanation of Spring Quartz2 dynamic task instance

Detailed explanation of Spring Quartz2 dynamic task instance

Here is the SimpleScheduleBuilder class in Quartz, which is not CronScheduleBuilder, and CronScheduleBuilder is a Cron expression. Please Baidu for details.

Implementation code:

/**
    * Added tasks
    * @param scheduleJob
    * @throws Exception
    */ 
  @Override 
  @SuppressWarnings("unchecked") 
  public void addJobSimple(ScheduleJob scheduleJob) throws Exception{ 
    TriggerKey triggerKey = ((), ()); 
    //Task trigger    Trigger trigger = (triggerKey); 
    if (null == trigger) { 
      JobDetail jobDetail = ((Class<? extends Job>) (())) 
       .withIdentity((), ()).build(); 
      ().put("scheduleJob", scheduleJob); 
       
      SimpleScheduleBuilder simpleScheduleBuilder = (); 
      if("Second".equals(()) ||  
          "second".equalsIgnoreCase(())){ 
        (()).repeatForever(); 
      }else if("point".equals(()) || "minute".equals(()) ||  
          "minute".equalsIgnoreCase(())){ 
        (()).repeatForever(); 
      }else if("hour".equals(()) || "Hour".equals(()) ||  
          "hour".equalsIgnoreCase(())){ 
        (()).repeatForever(); 
      }else if("sky".equals(()) ||  
          "date".equalsIgnoreCase(())){ 
        (()).repeatForever(); 
      } 
      ScheduleBuilder<SimpleTrigger> schedBuilder = (); 
       
      trigger =().withIdentity((), ()) 
          .withSchedule(schedBuilder).build(); 
      trigger = ().withIdentity((), ()).withSchedule(schedBuilder).build(); 
      (jobDetail, trigger); 
      (CC.LOG_PREFIX + "Added simple tasks:"+JasonUtils.Object2String(scheduleJob)); 
    }else { 
      (scheduleJob); 
    } 
  } 

It should be noted that when the timing task is only executed once, you need to read the code carefully, such as the simple timing task above, I started out like this:

(()); 

The result is only executed once, and what I need is to execute continuously and finally change it to:

(()).repeatForever(); 

It can be seen that there is another one behind: repeatForever(), which means repeat forever.

If you want to execute it many times and then do not execute it, use: .withRepeatCount(triggerRepeatCount), as follows:

(()).withRepeatCount(triggerRepeatCount); 

If you have any questions, please leave a message or go to the community of this site to exchange and discuss. Thank you for reading. I hope it can help you. Thank you for your support for this site!