Summary of Quartz

*. When it comes to things that machines replace humans to complete in a certain cycle, there will always be such needs to be executed regularly. At this time, Quartz is a good choice, so how to use it? Please continue to look down, O(∩_∩)O haha~

ornate divider

 

*, First of all, you must know how to use the factory!

StdSchedulerFactory sdf = new StdSchedulerFactory();
Note: Even if there are N new factories in different places in the project, if the following operations are not taken seriously, there will be big problems!

 

*, the factory has, so how to correctly produce the product of the scheduler?

Properties props = new Properties();  
props.put("org.quartz.scheduler.instanceName", "QuartzConfig"+new Date().toString());  
props.put("org.quartz.threadPool.class", "org.quartz.simpl.SimpleThreadPool");  
props.put("org.quartz.threadPool.threadCount", "1");  
sdf.initialize(props);
sd.scheduleJob(1,2);
Among them, the most critical is the instanceName, which is the name of the scheduler. The name of the scheduler is different, and the scheduler obtained from the factory is also different, so the cycle of executing tasks between the schedulers, on and off, will not be affected. They affect each other. As for the performance problem between schedulers, it has not been in-depth yet, haha.

 

*. How does the scheduler schedule and set up tasks?

JobDetail jobDetail = new JobDetail(jobName, JOB_GROUP_NAME, class);// task name, task group, task execution class  
where class is the CLASS of a class
If you provide the complete class name of string type (including the path of the package), you can obtain CLASS through reflection
CronTrigger trigger = new CronTrigger(jobName, TRIGGER_GROUP_NAME);// trigger name, trigger group  
trigger.setCronExpression(time);// trigger time setting  
sched.scheduleJob(jobDetail, trigger);
Note that the same scheduler's jobname cannot appear the same.
Regarding the dynamic modification of parameters such as the execution period, the task can be updated by removing the current task first and then adding it.

 

*. What if I only want to modify the parameter of the execution cycle?

Of course this is also possible! ! !
CronTrigger trigger = (CronTrigger) sched.getTrigger(triggerName,triggerGroupName);  
 if (trigger == null) { //Get the trigger to be modified according to the trigger name
    return;  
 }  
String oldTime = trigger.getCronExpression();  
 if (!oldTime.equalsIgnoreCase(time)) {//Determine whether the execution time is consistent
        CronTrigger ct = (CronTrigger) trigger;  
       // Re-modify the execution time of the trigger  
       ct.setCronExpression(time);  
       // Restart trigger via scheduler  
        sched.resumeTrigger(triggerName, triggerGroupName);  
}

 

*, Well, maybe you just want to close one of the tasks!

It's also simple, just follow the process below
sched.pauseTrigger(trigger name, TRIGGER_GROUP_NAME);// stop the trigger  
sched.unscheduleJob(trigger name, TRIGGER_GROUP_NAME);// remove trigger  
sched.deleteJob(job name, JOB_GROUP_NAME);// delete job  

 

 

*, the shutdown and suspension of the current scheduler

Shutdown is: shutdown
Pause is: standby
I like to be a little more violent, haha, every time it is shutdown, and then re-adding tasks and then restarting, because standby seems to make up the suspended tasks for execution, which is something I don't want to see! ! !

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326081592&siteId=291194637
Recommended