Quartz开源的作业调度框架 博客分类: JAVA Quartz开源作业调度框架
程序员文章站
2024-03-11 18:29:37
...
package com.demo.web.quartz; import java.text.SimpleDateFormat; import java.util.Date; import org.quartz.Job; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; /** * 具体执行的任务 * @author lxzqz * */ public class QuartzJob implements Job{ /** * 要执行的任务在此定义 */ @Override public void execute(JobExecutionContext arg0) throws JobExecutionException { System.out.println("执行具体的Job:" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())); } }
package com.demo.web.quartz; import org.quartz.JobBuilder; import org.quartz.JobDetail; import org.quartz.Scheduler; import org.quartz.SchedulerException; import org.quartz.SchedulerFactory; import org.quartz.SimpleScheduleBuilder; import org.quartz.Trigger; import org.quartz.TriggerBuilder; import org.quartz.impl.StdSchedulerFactory; /** * 普通的Trigger任务调度 * * @author lxzqz * */ public class QuartzSchedulerSimple { public static void main(String[] args) { System.out.println("初始化"); SchedulerFactory schedulerfactory = new StdSchedulerFactory(); Scheduler scheduler = null; try { scheduler = schedulerfactory.getScheduler(); JobDetail jobDetail = JobBuilder.newJob(QuartzJob.class) .withIdentity("jobDe", "schedulerG").build(); Trigger trigger = TriggerBuilder.newTrigger() .withIdentity("simpleTrigger", "schedulerG").withSchedule( // 每15分钟执行一次 SimpleScheduleBuilder.repeatMinutelyForever(15)) .startNow().build(); scheduler.scheduleJob(jobDetail, trigger); scheduler.start(); System.out.println("启动"); } catch (SchedulerException e) { e.printStackTrace(); } } }
package com.demo.web.quartz; import org.quartz.CronScheduleBuilder; import org.quartz.JobBuilder; import org.quartz.JobDetail; import org.quartz.Scheduler; import org.quartz.SchedulerException; import org.quartz.SchedulerFactory; import org.quartz.Trigger; import org.quartz.TriggerBuilder; import org.quartz.impl.StdSchedulerFactory; /** * 带Cron Like 表达式的Trigger任务调度 * * @author lxzqz * */ public class QuartzScheduler { public static void main(String[] args) { System.out.println("初始化"); SchedulerFactory schedulerfactory = new StdSchedulerFactory(); Scheduler scheduler = null; try { scheduler = schedulerfactory.getScheduler(); JobDetail jobDetail = JobBuilder.newJob(QuartzJob.class) .withIdentity("jobDe", "schedulerG").build(); Trigger trigger = TriggerBuilder .newTrigger() .withIdentity("cronTrigger", "schedulerG") .withSchedule( // 每个小时的0,15,30,45分钟执行 CronScheduleBuilder .cronSchedule("0 0,15,30,45 * * * ?")) .startNow().build(); scheduler.scheduleJob(jobDetail, trigger); scheduler.start(); System.out.println("启动"); } catch (SchedulerException e) { e.printStackTrace(); } } }
框架核心分析
-
SchedulerFactory -- 调度程序工厂
- StdSchedulerFactory -- Quartz默认的SchedulerFactory
- DirectSchedulerFactory -- DirectSchedulerFactory是对SchedulerFactory的直接实现,通过它可以直接构建Scheduler、threadpool 等
- ThreadExecutor / DefaultThreadExecutor -- 内部线程操作对象
-
JobExecutionContext -- JOB上下文,保存着Trigger、 JobDeaitl 等信息,JOB的execute方法传递的参数就是对象的实例
- JobExecutionContextImpl
-
Scheduler -- 调度器
- StdScheduler -- Quartz默认的Scheduler
- RemoteScheduler -- 带有RMI功能的Scheduler
-
JOB --任务对象
- JobDetail -- 他是实现轮询的一个的回调类,可将参数封装成JobDataMap对象,Quartz将任务的作业状态保存在JobDetail中.
- JobDataMap -- JobDataMap用来报错由JobDetail传递过来的任务实例对象
-
Trigger
- SimpleTrigger <普通的Trigger> -- SimpleScheduleBuilder
- CronTrigger <带Cron Like 表达式的Trigger> -- CronScheduleBuilder
- CalendarIntervalTrigger <带日期触发的Trigger> -- CalendarIntervalScheduleBuilder
- DailyTimeIntervalTrigger <按天触发的Trigger> -- DailyTimeIntervalScheduleBuilder
-
ThreadPool -- 为Quartz运行任务时提供了一些线程
- SimpleThreadPool --一个Quartz默认实现的简单线程池,它足够健壮,能够应对大部分常用场景