Introduction to Trigger
Quartz has some different trigger types, but the most used ones are SimpleTrigger and CronTrigger
jobKey
Indicates the ID of the job instance. When the trigger is triggered, the specified job instance will be executed
statrTime
Represents the trigger's schedule, the first time it was triggered, and its data type is java.util.Date
endTime
Specifies the trigger termination triggered time, whose data type is java.uti.Date
Two maven dependencies of quartz need to be imported for environment preparation and task scheduling
<!--quzrtz Task scheduling core package--> <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>2.3.2</version> </dependency> <!--Kit is optional--> <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz-jobs</artifactId> <version>2.3.2</version> </dependency>
Log framework used
<dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.2</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency>
First, create a class to implement the Job interface, which is the completed task
package com.quartz; import org.quartz.*; import java.text.SimpleDateFormat; import java.util.Date; /** * package_name:com.quartz * Author:Xu Ya Yuan * Date:2020/1/16 19:59 * Project Name: Quartz one * Desription: **/ public class HelloJobTrigger implements Job { @Override public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException { Date date = new Date(); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String dateString = simpleDateFormat.format(date); System.out.println("Database backup in progress:Database backup time:"+dateString); Trigger trigger = jobExecutionContext.getTrigger(); System.out.println("jobKey Name:"+trigger.getKey().getName()+"; jobKey Name of group: "+trigger.getKey().getGroup()); //startTime start task time System.out.println("Time when the task starts executing:"+simpleDateFormat.format(trigger.getStartTime())); //endTime task end time System.out.println("Task end time:"+simpleDateFormat.format(trigger.getEndTime())); } }
In the implementation of a main method, there are Scheduler, task instance JobDetail and Trigger trigger
package com.scheduler; import com.quartz.HelloJob; import com.quartz.HelloJobTrigger; import org.quartz.*; import org.quartz.impl.StdSchedulerFactory; import java.util.Date; /** * package_name:com.scheduler * Author:Xu Ya Yuan * Date:2020/1/16 20:03 * Project Name: Quartz one * Desription: **/ public class HelloSchedulerTrigger { public static void main(String args[]) throws Exception { //Scheduler, get the scheduling instance from the factory, Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler(); //Start time Date startDate = new Date(); //Delay the start of the task by five seconds startDate.setTime(startDate.getTime() + 5000); //End time Date endDate = new Date(); //Task end time delayed by 10 seconds (stop after 10 seconds) endDate.setTime(endDate.getTime() + 10000); //Task instance (JobDetail) JobDetail jobDetail = JobBuilder.newJob(HelloJobTrigger.class) //Loading the task class to complete the binding with HelloJob requires that HelloJob must implement the Job interface .withIdentity("job", "group1") //Name of parameter 1 task name of parameter 2 task group .usingJobData("message", "Print log jobDetail") //Transfer parameters .build(); //Trigger Trigger trigger = TriggerBuilder.newTrigger().withIdentity("trigger1", "group1") //Name of parameter 1 trigger name of parameter 2 trigger group //. startNow() / / start the trigger now .withSchedule(SimpleScheduleBuilder.simpleSchedule().repeatSecondlyForever(5)) .startAt(startDate) //Set task start time .endAt(endDate) //Set the end time of the task .build(); //Let the scheduler associate tasks and triggers to ensure that tasks are executed according to the conditions of triggers scheduler.scheduleJob(jobDetail, trigger); //start-up scheduler.start(); } }
Output result