- Scheduler 调度器。所有的调度都是由它控制,是Quartz的大脑,所有的任务都是由它来管理
- Job:任务,定时执行的类(定义业务逻辑)
- JobDetail:基于Job,进一步包装。其中关联一个Job,并为Job指定更详细的书香,比如标识等
- Trigger:触发器。可以指定给某个任务。指定任务的触发机制
创建Job4.0.0 com.cp quarz-demo0.0.1-SNAPSHOT org.quartz-scheduler quartz2.2.3
package com.quarz.demo;
import java.util.Date;
import org.quartz.Job;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.JobKey;
//Job 任务
public class HelloJob implements Job{
public void execute(JobExecutionContext context) throws JobExecutionException {
//可以获取到job的标识
JobDetail jobDetail = context.getJobDetail();
JobKey key = jobDetail.getKey();
String name = key.getName();
String group = key.getGroup();
System.out.println(name);
System.out.println(group);
System.out.println("hello join execute"+ new Date());
}
}
测试
package com.quarz.demo;
import java.util.GregorianCalendar;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SimpleScheduleBuilder;
import org.quartz.SimpleTrigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;
public class HelloQuartz {
public static void main(String[] args) throws SchedulerException {
//1.构建调度器Scheduler
Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
//2.触发器
SimpleTrigger trigger = TriggerBuilder.newTrigger().withIdentity("trigger1", "group1")
.startNow()
.withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(2).repeatForever())
.endAt(new GregorianCalendar(2021, 10, 13, 9, 23, 59).getTime()).build();
//3.JobDetail
JobDetail jobDetail = JobBuilder.newJob(HelloJob.class).withIdentity("job1", "group1").build();
//4.将JobDetail 和触发器 添加到调度器中
scheduler.scheduleJob(jobDetail, trigger);
//5.启动,调度器开始工作
scheduler.start();
}
}
quartz.properties配置文件(可以不配置)
quartz有配置文件,这里没有配置。
当我们没有配置quartz.properties文件时,quartz会有他自己的默认值。
当你有需要修改,就需要配置quartz.properties文件
## 指定调度器名称,非实现类 org.quartz.scheduler.instanceName = MyScheduler ## 指定线程池实现类 org.quartz.threadPool.threadCount = 3 ## 线程池线程数量 org.quartz.threadPoll.threadPriority = 5 ## 非持久化job org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore控制台输出
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder". SLF4J: Defaulting to no-operation (NOP) logger implementation SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details. job1 group1 hello join executeSat Nov 13 10:01:02 GMT+08:00 2021 job1 group1 hello join executeSat Nov 13 10:01:04 GMT+08:00 2021 job1 group1 hello join executeSat Nov 13 10:01:06 GMT+08:00 2021 job1 group1 hello join executeSat Nov 13 10:01:08 GMT+08:00 2021 job1 group1 hello join executeSat Nov 13 10:01:10 GMT+08:00 2021 job1 group1 hello join executeSat Nov 13 10:01:12 GMT+08:00 2021 job1 group1 hello join executeSat Nov 13 10:01:14 GMT+08:00 2021 job1 group1 hello join executeSat Nov 13 10:01:16 GMT+08:00 2021 job1 group1 hello join executeSat Nov 13 10:01:18 GMT+08:00 2021 job1 group1 hello join executeSat Nov 13 10:01:20 GMT+08:00 2021 job1 group1 hello join executeSat Nov 13 10:01:22 GMT+08:00 2021 job1 group1 hello join executeSat Nov 13 10:01:24 GMT+08:00 2021 job1 group1 hello join executeSat Nov 13 10:01:26 GMT+08:00 2021 job1 group1 hello join executeSat Nov 13 10:01:28 GMT+08:00 2021 job1 group1 hello join executeSat Nov 13 10:01:30 GMT+08:00 2021 job1 group1 hello join executeSat Nov 13 10:01:32 GMT+08:00 2021 job1 group1 hello join executeSat Nov 13 10:01:34 GMT+08:00 2021 job1 group1 hello join executeSat Nov 13 10:01:36 GMT+08:00 2021



