首页  

quartz 原理简介     所属分类 quartz 浏览量 112
Quartz  开源作业调度框架
org.quartz.Job 接口 
void execute(JobExecutionContext context) throws JobExecutionException;

类似 java.util.Timer ,Quartz 增强了功能:
作业持久性化   
作业管理 

Quartz 的核心类 :
任务 Job : 任务类,实现 execute() 方法 
触发器 Trigger :  SimpleTrigger 和 CronTrigger
调度器 Scheduler : 基于 Trigger触发器,执行 Job任务

org.quartz-scheduler:quartz:2.3.0

public class HelloJob implements Job {

    private static final AtomicLong ID = new AtomicLong(0);
    private final String name;
    public HelloJob(){
        name = "HelloJob_"+ID.incrementAndGet();
    }

    @Override
    public void execute(JobExecutionContext context) {
        log.error("{} run,now={}",name, DateUtil.now());
    }
}


import static org.quartz.SimpleScheduleBuilder.simpleSchedule;


// 1.创建调度器 Scheduler
SchedulerFactory factory = new StdSchedulerFactory();
Scheduler scheduler = factory.getScheduler();
 
// 2.创建JobDetail实例,并与HelloJob类绑定 
JobDetail job = JobBuilder.newJob(MyJob.class)
    .withIdentity("job1", "group1")
    .build();
 
// 3.构建Trigger实例,每隔5s执行一次
Trigger trigger = TriggerBuilder.newTrigger()
    .withIdentity("trigger1", "group1")
    .startNow()
    .withSchedule(simpleSchedule().withIntervalInSeconds(5).repeatForever())
    .build();
 
// 4.执行,开启调度器
scheduler.scheduleJob(job, trigger);
System.out.println(System.currentTimeMillis());
scheduler.start();
 
//主线程睡眠1分钟,然后关闭调度器
TimeUnit.MINUTES.sleep(1);
scheduler.shutdown();
System.out.println(System.currentTimeMillis());


org.quartz.JobKey
Uniquely identifies a JobDetail.
Keys are composed of both a name and group, and the name must be unique within the group. 
If only a name is specified then the default group name will be used.
org.quartz.utils.Key#DEFAULT_GROUP
// The default group for scheduling entities, with the value "DEFAULT".
public static final String DEFAULT_GROUP = "DEFAULT";


JobDetail name 任务名称 group 任务分组,默认分组 DEFAULT jobClass 任务类 jobDataMap 任务参数信息 Scheduler 调度执行Job,创建Job实例,执行Job中的execute()方法,执行结束后,释放Job对象实例 JobDetail + Job JobExecutionContext JobDetail job = JobBuilder.newJob(MyJob.class) .withIdentity("job1", "group1") // 定义任务添加自定义参数 .usingJobData("quartz", "quartz 自定义参数") .build(); 构建Trigger实例,每隔30s执行一次 Trigger trigger = TriggerBuilder.newTrigger() .withIdentity("trigger1", "group1") // Trigger 添加自定义参数 .usingJobData("trigger", "trigger 自定义参数") .startNow() .withSchedule(simpleSchedule() .withIntervalInSeconds(30) .repeatForever()) .build(); public class MyJob implements Job { @Override public void execute(JobExecutionContext context) throws JobExecutionException { Object triggerParams = context.getTrigger().getJobDataMap().get("trigger"); Object quartzParams = context.getJobDetail().getJobDataMap().get("quartz"); } } Job状态参数 JobDataMap Trigger 可以设置任务的开始结束时间, Scheduler 会根据参数进行触发。 Calendar instance = Calendar.getInstance(); Date startTime = instance.getTime(); instance.add(Calendar.MINUTE, 1); Date endTime = instance.getTime(); // 3.构建Trigger实例 Trigger trigger = TriggerBuilder.newTrigger() .withIdentity("trigger1", "group1") // 开始时间 .startAt(startTime) // 结束时间 .endAt(endTime) .build(); Trigger 可以设置任务的开始结束时间, Scheduler 会根据参数进行触发。 public void execute(JobExecutionContext context) throws JobExecutionException { System.out.println(context.getTrigger().getStartTime()); System.out.println(context.getTrigger().getEndTime()); } TriggerBuilder.newTrigger() .withSchedule(SimpleScheduleBuilder .simpleSchedule() .withIntervalInSeconds(30) .repeatForever()) TriggerBuilder.newTrigger() .withSchedule(SimpleScheduleBuilder .simpleSchedule() .withIntervalInSeconds(30) .withRepeatCount(5)) withRepeatCount(count) 重复次数,实际运行次数 count+1 TriggerBuilder.newTrigger().withSchedule(CronScheduleBuilder.cronSchedule("* * * * * ?"))
quartz job默认无状态 ,每次调用都会创建一个新的 JobDataMap job实现类加上注解 @PersistJobDataAfterExecution //多次调用 Job 的时候,将参数保留在 JobDataMap @PersistJobDataAfterExecution public class JobStatus implements Job { @Override public void execute(JobExecutionContext context) throws JobExecutionException { long count = (long) context.getJobDetail().getJobDataMap().get("count"); System.out.println("当前执行,第" + count + "次"); context.getJobDetail().getJobDataMap().put("count", ++count); } } JobDetail job = JobBuilder.newJob(JobStatus.class) .withIdentity("statusJob", "group1") .usingJobData("count", 1L) .build();
https://gitee.com/dyyx/work2024/tree/master/demo/springboot2demo/demo-task-quartz

上一篇     下一篇
java final 字段 反射修改说明

煎荷包蛋小技巧

mac brew 安装 openjdk17

Quartz简介

quartz countJob 实例

quartz 配置文件 quartz.properties