Quartz has four core concepts:
Job: is an interface that only defines a method execute (JobExecutionContext context). Write a job (task) that needs to be executed regularly in the execute method that implements the interface.
Double slongitude = (().getJobDataMap().get("slongitude").toString());
JobDetail: Quartz recreates a Job instance every time he schedules a job, so it does not accept a Job instance. Instead, it receives a Job implementation class (JobDetail, which describes the Job implementation class and other related static information, such as Job name, description, association listener, etc.), so that the runtime instantiates the job through the reflection mechanism of newInstance().
rigger: is a class that describes the time triggering rules that trigger job execution, mainly two subclasses: SimpleTrigger and CronTrigger. SimpleTrigger is the most suitable choice if and only if scheduling is required or if scheduling is performed at a fixed time interval; while CronTrigger can define scheduling schemes for various complex time rules through Cron expressions: such as scheduling from 15:00 to 16:00 from Monday to Friday on weekdays.
Scheduler: The scheduler is equivalent to a container, loaded with tasks and triggers. This class is an interface that represents a Quartz independent running container. Trigger and JobDetail can be registered in Scheduler. Both have their own groups and names in Scheduler. Groups and names are the basis for Scheduler to find and locate an object in the container. Trigger's group and name must be unique, and JobDetail's group and name must also be unique (but they can be the same as Trigger's group and name because they are of different types). Scheduler defines multiple interface methods, allowing external access and control of Trigger and JobDetail in containers through groups and names.
1. Import pom dependencies
<dependency> <groupId></groupId> <artifactId>spring-boot-starter-quartz</artifactId> </dependency>
2. Turn on timing tasks
@EnableScheduling annotation
//java @EnableScheduling @SpringBootApplication public class DemoApplication { public static void main(String[] args) { (, args); } }
3. Create a new job
@Slf4j public class MyJob implements Job { @Override public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException { try { executeTask(jobExecutionContext); } catch (Exception e) { (); } } //java private static void executeTask(JobExecutionContext jobExecutionContext) throws SchedulerException { //The JobExecutionContext class provides some information about scheduling applications;//The information of the Job runtime is saved in the JobDataMap instance. JobKey key = ().getKey(); (new Date()+"->"+()+"Timed task is being executed"); } }
4. Build JobDetailrigger
Register Trigger and JobDetail into Scheduler
//fhrom @Configuration public class QuartzManager { // public static final String JOB1="job1"; // public static final String GROUP1="group1"; /**Default execution at 1 a.m. every week*/ //public static final String DEFAULT_CRON="0 0 1 ? * L"; /**Default execution once in 5 seconds*/ // public static final String DEFAULT_CRON="*/5 * * * * ?"; /** * Task Scheduling */ private Scheduler scheduler; @Bean public Scheduler scheduler() throws SchedulerException { SchedulerFactory schedulerFactoryBean = new StdSchedulerFactory(); = (); return (); } /** * Start executing timing tasks */ public void startJob(String name,String group,String time) throws SchedulerException { startJobTask(scheduler,name,group,time); (); } /** * Start the timing task * @param scheduler */ private void startJobTask(Scheduler scheduler,String name ,String group,String time) throws SchedulerException { //For identity JobDetail jobDetail= ().withIdentity(name,group).build(); ("jobDetail:-------------"+jobDetail); String DEFAULT_CRON="*/"+time+" * * * * ?"; // SimpleScheduleBuilder CronScheduleBuilder is used to build Scheduler and define the time rules for task scheduling CronScheduleBuilder cronScheduleBuilder=(DEFAULT_CRON); //trigger CronTrigger cronTrigger=().withIdentity(name,group) .withSchedule(cronScheduleBuilder).build(); (jobDetail,cronTrigger); } /** * Get Job information * @param name * @param group */ public String getjobInfo(String name,String group) throws SchedulerException { TriggerKey triggerKey=new TriggerKey(name,group); ("triggerKey:"+triggerKey); CronTrigger cronTrigger= (CronTrigger) (triggerKey); return ("time:%s,state:%s",(), (triggerKey).name()); } /** * Modify the execution time of the task * @param name * @param group * @param cron cron expression * @return * @throws SchedulerException */ public boolean modifyJob(String name,String group,String cron) throws SchedulerException{ Date date=null; TriggerKey triggerKey=new TriggerKey(name, group); CronTrigger cronTrigger= (CronTrigger) (triggerKey); String oldTime=(); if (!(cron)){ CronScheduleBuilder cronScheduleBuilder=(cron); CronTrigger trigger=().withIdentity(name,group) .withSchedule(cronScheduleBuilder).build(); date=(triggerKey,trigger); } return date !=null; } /** * Pause all tasks * @throws SchedulerException */ public void pauseAllJob()throws SchedulerException{ (); } /** * Pause a task * @param name * @param group * @throws SchedulerException */ public void pauseJob(String name,String group)throws SchedulerException{ JobKey jobKey=new JobKey(name,group); JobDetail jobDetail=(jobKey); if (jobDetail==null) { return; } (jobKey); } /** * Recover all tasks * @throws SchedulerException */ public void resumeAllJob()throws SchedulerException{ (); } /** * Recover a task */ public void resumeJob(String name,String group)throws SchedulerException { JobKey jobKey=new JobKey(name,group); JobDetail jobDetail=(jobKey); if (jobDetail==null) { return; } (jobKey); } /** * Delete a task * @param name * @param group * @throws SchedulerException */ public void deleteJob(String name,String group)throws SchedulerException{ JobKey jobKey=new JobKey(name, group); JobDetail jobDetail=(jobKey); if (jobDetail==null) { return; } (jobKey); } }
5. Write Controller
By calling the interface, you can start a timed task and end a timed task to obtain task information
// @RestController @RequestMapping("/quartz") public class ModifyCronController { @Autowired private QuartzManager quartzManager; @GetMapping("/modify") public String modify(@RequestParam("job")String job, //The name of the job @RequestParam("group")String group,//job's group @RequestParam("time")String time //Step length of timed tasks ) throws SchedulerException { // (QuartzManager.JOB1,QuartzManager.GROUP1); (job,group,time); String s = (job, group); ("job:"+s); return "ok"; } }
This is the end of this article about Spring Boot configuring Quartz timing tasks. For more related Spring Boot Quartz timing tasks, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!