introduce
Quartz is an open source job scheduling framework written entirely in Java. It has many features, such as database support, clustering, plug-ins, EJB job pre-construction, JavaMail and others, support for cron-like expressions, and so on. The project has been acquired by Terracotta.
Introduction to Quartz API
The key interfaces of the Quartz API are as follows:
Interface class object:
- Scheduler: The most important API interface related to task scheduling.
- Job: The component definition (what the scheduler executes) that you expect the task to be scheduled to execute must all implement the interface.
- Trigger: Defines when a specified Job is executed, also known as a trigger.
Objects used to create examples:
- JobDetail: An instance used to define a Job.
- JobBuilder: An instance used to define or create a JobDetail, which limits instances that can only be Jobs.
- TriggerBuilder: An instance used to define or create triggers.
Environmental Science
- idea
- maven
- springboot
case
Introduction Case-1
It's not as good as running the code directly to get the result, so do it.
The pom.xml configuration is as follows:
<!-- quartz --> <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> </dependency> <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz-jobs</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>5.0.7.RELEASE</version> </dependency>
Class job
@Data public class HelloJob implements Job { public void execute(JobExecutionContext context) throws JobExecutionException { JobDataMap jobDataMap = context.getJobDetail().getJobDataMap(); String name = jobDataMap.get("name").toString(); String characteristic = jobDataMap.get("characteristic").toString(); System.out.println("name : " + name); System.out.println("characteristic : " + characteristic); } }
QuartzTest class
public class QuartzTest { public static void main(String[] args) throws SchedulerException, ParseException { SchedulerFactory schedulerFactory = new StdSchedulerFactory(); Scheduler scheduler = schedulerFactory.getScheduler(); scheduler.start(); // Define job s, bind our scheduled tasks JobDetail job2 = newJob(HelloJob.class) .usingJobData("name", "Zhang San") .usingJobData("characteristic", "Handsome 3") .build(); // Execute tasks, using defined triggers and tasks scheduler.scheduleJob(job2, getTrigger6()); } /** * The trigger is triggered immediately, then every 2 seconds, 22:55:00: */ private static Trigger getTrigger1() { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); //Define a task trigger return newTrigger() .withIdentity("job2", "group2") //Fixed point triggering //.startAt(sdf.parse("2018-09-27 10:27:00")) // Five seconds later trigger .startAt(DateBuilder.futureDate(5, DateBuilder.IntervalUnit.SECOND)) .withSchedule(simpleSchedule() .withIntervalInSeconds(2) .repeatForever()) //.withRepeatCount(0)) .endAt(DateBuilder.dateOf(22, 55, 0)) .build(); }
Running the test class, the results are as follows: (Congratulations, success, then, in-depth study)
10:29:11.012 [MyOneQuartzScheduler_Worker-1] DEBUG org.quartz.core.JobRunShell - Calling execute on job DEFAULT.6da64b5bd2ee-f1562ba3-b8fa-436d-848e-d6e8ea39c26d name : Zhang San characteristic : Handsome 3
Source address
github address<br/>