要学习Quartz框架,首先大概了解了Quartz的基本知识后,在通过简单的例子入门,一步一个脚印的走下去。
下面介绍Quartz入门的示例,由于Quartz的存储方式分为RAM和JDBC,分别对这两种进行简单的说明。并附上代码! 首先需要添加Quartz的Jar包 ,我使用的是quartz.2.2.1版本! Demo代码下载地址:org.quartz-scheduler quartz 2.2.1 org.quartz-scheduler quartz-jobs 2.2.1
1:RAM方式
要测试RAMdemo的代码,请先删除demo中这个quartz.properties文件,或者重命名!否则会测试不成功! (1):Jobpackage com.dufy.learn;import java.util.Date;import org.quartz.Job;import org.quartz.JobExecutionContext;import org.quartz.JobExecutionException;import org.slf4j.Logger;import org.slf4j.LoggerFactory;public class RAMJob implements Job{ private static Logger _log = LoggerFactory.getLogger(RAMJob.class); @Override public void execute(JobExecutionContext arg0) throws JobExecutionException { _log.info("Say hello to Quartz" + new Date()); } }
(2):QuartzTest
package com.dufy.learn;import java.util.Date;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.SimpleScheduleBuilder;import org.quartz.Trigger;import org.quartz.TriggerBuilder;import org.quartz.impl.StdSchedulerFactory;import org.slf4j.Logger;import org.slf4j.LoggerFactory;/** * This is a RAM Store Quartz! * @author dufy * @date 2017.02.04 * */public class RAMQuartz { private static Logger _log = LoggerFactory.getLogger(RAMQuartz.class); public static void main(String[] args) throws SchedulerException { //1.创建Scheduler的工厂 SchedulerFactory sf = new StdSchedulerFactory(); //2.从工厂中获取调度器实例 Scheduler scheduler = sf.getScheduler(); //3.创建JobDetail JobDetail jb = JobBuilder.newJob(RAMJob.class) .withDescription("this is a ram job") //job的描述 .withIdentity("ramJob", "ramGroup") //job 的name和group .build(); //任务运行的时间,SimpleSchedle类型触发器有效 long time= System.currentTimeMillis() + 3*1000L; //3秒后启动任务 Date statTime = new Date(time); //4.创建Trigger //使用SimpleScheduleBuilder或者CronScheduleBuilder Trigger t = TriggerBuilder.newTrigger() .withDescription("") .withIdentity("ramTrigger", "ramTriggerGroup") //.withSchedule(SimpleScheduleBuilder.simpleSchedule()) .startAt(statTime) //默认当前时间启动 .withSchedule(CronScheduleBuilder.cronSchedule("0/2 * * * * ?")) //两秒执行一次 .build(); //5.注册任务和定时器 scheduler.scheduleJob(jb, t); //6.启动 调度器 scheduler.start(); _log.info("启动时间 : " + new Date()); }}
运行结果:
SimpleScheduleBuilder:CronScheduleBuilder:
2.JDBC方式
使用jdbc方式,就要配置quartz.properties文件,并且在开始的时候在数据库中新增表!
我使用的数据库是mysql,数据库中表在quartz_demo里面有,需要的请在里面下载! 运行 tables_db2_v8.sql 这个文件。#JDBC驱动org.quartz.dataSource.qzDS.driver:com.mysql.jdbc.Driverorg.quartz.dataSource.qzDS.URL:jdbc:mysql://localhost:3306/quartz_testorg.quartz.dataSource.qzDS.user:rootorg.quartz.dataSource.qzDS.password:rootorg.quartz.dataSource.qzDS.maxConnection:10
(1)MyJob
package com.dufy.jdbctest;import java.text.SimpleDateFormat;import java.util.Date;import org.quartz.Job;import org.quartz.JobExecutionContext;import org.quartz.JobExecutionException;import org.slf4j.Logger;import org.slf4j.LoggerFactory;public class MyJob implements Job{ private static final Logger log = LoggerFactory.getLogger(MyJob.class); @Override public void execute(JobExecutionContext context)throws JobExecutionException { log.info("MyJob is start .................."); log.info("Hello quzrtz "+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss ").format(new Date())); log.info("MyJob is end ....................."); } }
(2)QuartzJdbcTest
package com.dufy.jdbctest;import java.text.ParseException;import java.util.List;import org.quartz.CronScheduleBuilder;import org.quartz.CronTrigger;import org.quartz.JobBuilder;import org.quartz.JobDetail;import org.quartz.JobKey;import org.quartz.Scheduler;import org.quartz.SchedulerException;import org.quartz.SchedulerFactory;import org.quartz.SimpleScheduleBuilder;import org.quartz.SimpleTrigger;import org.quartz.Trigger;import org.quartz.TriggerBuilder;import org.quartz.TriggerKey;import org.quartz.impl.StdSchedulerFactory;public class QuartzJdbcTest { public static void main(String[] args) throws SchedulerException, ParseException { startSchedule(); //resumeJob(); } /** * 开始一个simpleSchedule()调度 */ public static void startSchedule() { try { // 1、创建一个JobDetail实例,指定Quartz JobDetail jobDetail = JobBuilder.newJob(MyJob.class) // 任务执行类 .withIdentity("job1_1", "jGroup1") // 任务名,任务组 .build(); //触发器类型 SimpleScheduleBuilder builder = SimpleScheduleBuilder // 设置执行次数 .repeatSecondlyForTotalCount(5); //CronScheduleBuilder builder = CronScheduleBuilder.cronSchedule("0/2 * * * * ?"); // 2、创建Trigger Trigger trigger = TriggerBuilder.newTrigger() .withIdentity("trigger1_1", "tGroup1").startNow() .withSchedule(builder) .build(); // 3、创建Scheduler Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler(); scheduler.start(); // 4、调度执行 scheduler.scheduleJob(jobDetail, trigger); try { Thread.sleep(60000); } catch (InterruptedException e) { e.printStackTrace(); } //关闭调度器 scheduler.shutdown(); } catch (SchedulerException e) { e.printStackTrace(); } } /** * 从数据库中找到已经存在的job,并重新开户调度 */ public static void resumeJob() { try { SchedulerFactory schedulerFactory = new StdSchedulerFactory(); Scheduler scheduler = schedulerFactory.getScheduler(); JobKey jobKey = new JobKey("job1_1", "jGroup1"); List triggers = scheduler.getTriggersOfJob(jobKey); //SELECT TRIGGER_NAME, TRIGGER_GROUP FROM {0}TRIGGERS WHERE SCHED_NAME = {1} AND JOB_NAME = ? AND JOB_GROUP = ? // 重新恢复在jGroup1组中,名为job1_1的 job的触发器运行 if(triggers.size() > 0){ for (Trigger tg : triggers) { // 根据类型判断 if ((tg instanceof CronTrigger) || (tg instanceof SimpleTrigger)) { // 恢复job运行 scheduler.resumeJob(jobKey); } } scheduler.start(); } } catch (Exception e) { e.printStackTrace(); } }}
运行结果:
控制台打印:
Cron和Simple类型,Simple类型的如果JobDetail没有设置.storeDurably(true),则job在运行完成之后会在数据库中删除!
欢迎访问我的csdn博客,我们一同成长!
"不管做什么,只要坚持下去就会看到不一样!在路上,不卑不亢!"
博客首页: