欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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();
		}
	}
}

 

框架核心分析

  1. SchedulerFactory    -- 调度程序工厂
    1. StdSchedulerFactory   -- Quartz默认的SchedulerFactory
    2. DirectSchedulerFactory  --   DirectSchedulerFactory是对SchedulerFactory的直接实现,通过它可以直接构建Scheduler、threadpool 
      1. ThreadExecutor / DefaultThreadExecutor   -- 内部线程操作对象
  2. JobExecutionContext -- JOB上下文,保存着Trigger、 JobDeaitl 等信息,JOB的execute方法传递的参数就是对象的实例
    1. JobExecutionContextImpl
  3. Scheduler    -- 调度器
    1. StdScheduler    -- Quartz默认的Scheduler
    2. RemoteScheduler  -- 带有RMI功能的Scheduler
  4. JOB --任务对象
    1. JobDetail  -- 他是实现轮询的一个的回调类,可将参数封装成JobDataMap对象,Quartz将任务的作业状态保存在JobDetail中.
    2. JobDataMap --  JobDataMap用来报错由JobDetail传递过来的任务实例对象
  5. Trigger
    1. SimpleTrigger <普通的Trigger> --  SimpleScheduleBuilder
    2. CronTrigger  <带Cron Like 表达式的Trigger> -- CronScheduleBuilder
    3. CalendarIntervalTrigger <带日期触发的Trigger> -- CalendarIntervalScheduleBuilder
    4. DailyTimeIntervalTrigger <按天触发的Trigger> -- DailyTimeIntervalScheduleBuilder
  6. ThreadPool  --  为Quartz运行任务时提供了一些线程
    1. SimpleThreadPool  --一个Quartz默认实现的简单线程池,它足够健壮,能够应对大部分常用场景