java 调度quartz,java quartz任务调度

1.quartz的结构

quartz包括了3个部分:任务、触发器和调度器。

(1)任务

Job:是一个接口。要定义自己的任务,必须继承该接口。该接口只用一个方法void execute(JobExecutionContext context),JobExecutionContext类提供了调度上下文的各种信息。Job运行时的信息保存在JobDataMap实例中。

JobDetail:该类的作用是给自定义的任务绑定组名以及Job名,用来在Scheduler中区分每个Job。该类的构造方法为JobDetail(java.lang.String name, java.lang.String group, java.lang.Class jobClass)。

(2)触发器

Trigger:该类的作用是描述Job的执行时间规则,有SimpleTrigger和CronTrigger两个子类。SimpleTrigger用来描述一次触发或者是固定的时间间隔,CronTrigger则是通过Cron表达式规定固定的时间点。

Calendar:该类表示是特定时间点的集合。一个Trigger可以对应多个Calendar,用来排除或者添加特定的时间点。其下有若干个实现类,如AnnualCalendar、MonthlyCalendar、WeeklyCalendar分别针对每年、每月和每周进行定义。

(3)调度器

Scheduler:该类是quartz的运行容器,Trigger和JobDetail可以注册到Scheduler中,两者在Scheduler中拥有各自的组及名称,组及名称是Scheduler查找定位容器中某一对象的依据,Trigger的组及名称必须唯一,JobDetail的组和名称也必须唯一(但可以和Trigger的组和名称相同,因为它们是不同类型的)。Scheduler可以将Trigger绑定到某一JobDetail中,这样当Trigger触发时,对应的Job就被执行。一个Job可以对应多个Trigger,但一个Trigger只能对应一个Job。

ThreadPool:Scheduler使用一个线程池作为任务运行的基础设施,任务通过共享线程池中的线程提高运行效率。

d4c0ae1187c60ae9c02474263ecf7811.gif

2.quartz使用实例

public class PrintJob implements Job {

@Override

public void execute(JobExecutionContext context) throws JobExecutionException {

System.out.println("executing this job");

}

}

(1)使用SimpleTrigger

public static void main(String[] args) throws SchedulerException {

JobDetail detail = new JobDetail("jName", "jGroup", PrintJob.class);

SimpleTrigger trigger = new SimpleTrigger("stName", "stGroup");

// 当前时间为开始时间

trigger.setStartTime(new Date());

// 每隔2秒执行一次

trigger.setRepeatInterval(2000);

// 执行10次

trigger.setRepeatCount(10);

SchedulerFactory factory = new StdSchedulerFactory();

Scheduler scheduler = factory.getScheduler();

// JobDetail与Trigger关联

scheduler.scheduleJob(detail, trigger);

scheduler.start();

}

在构造Trigger实例时,可以考虑使用org.quartz.TriggerUtils工具类,该工具类不但提供了众多获取特定时间的方法,还拥有众多获取常见Trigger的方法。

(2)使用CronTrigger

Cron表达式时间字段

位置

时间域名

允许值

允许的特殊字符

1

0-59

, – * /

2

分钟

0-59

, – * /

3

小时

0-23

, – * /

4

日期

1-31

, – * ? / L W C

5

月份

1-12

, – * /

6

星期

1-7

, – * ? / L C #

7

年(可选)

空值1970-2099

, – * /

●星号(*):可用在所有字段中,表示对应时间域的每一个时刻,例如,*在分钟字段时,表示“每分钟”;

●问号(?):该字符只在日期和星期字段中使用,它通常指定为“无意义的值”,相当于点位符(当日期或者星期存在一个时,另一个可以设置为?);

●减号(-):表达一个范围,如在小时字段中使用“10-12”,则表示从10到12点,即10,11,12;

●逗号(,):表达一个列表值,如在星期字段中使用“MON,WED,FRI”,则表示星期一,星期三和星期五;

●斜杠(/):x/y表达一个等步长序列,x为起始值,y为增量步长值。如在秒字段中使用0/15,则表示为0,15,30和45秒,而5/15在分钟字段中表示5,20,35,50分,你也可以使用*/y,它等同于0/y;

●L:该字符只在日期和星期字段中使用,代表“Last”的意思,但它在两个字段中意思不同。L在日期字段中,表示这个月份的最后一天,如一月的31号,非闰年二月的28号;如果L用在星期中,则表示星期六,等同于7。但是,如果L出现在星期字段里,而且在前面有一个数值X,则表示“这个月的最后X天”,例如,6L表示该月的最后星期五;

●W:该字符只能出现在日期字段里,是对前导日期的修饰,表示离该日期最近的工作日。例如15W表示离该月15号最近的工作日,如果该月15号是星期六,则匹配14号星期五;如果15日是星期日,则匹配16号星期一;如果15号是星期二,那结果就是15号星期二。但必须注意关联的匹配日期不能够跨月,如你指定1W,如果1号是星期六,结果匹配的是3号星期一,而非上个月最后的那天。W字符串只能指定单一日期,而不能指定日期范围;

●LW组合:在日期字段可以组合使用LW,它的意思是当月的最后一个工作日;

●井号(#):该字符只能在星期字段中使用,表示当月某个工作日。如6#3表示当月的第三个星期五(6表示星期五,#3表示当前的第三个),而4#5表示当月的第五个星期三,假设当月没有第五个星期三,忽略不触发;

●C:该字符只在日期和星期字段中使用,代表“Calendar”的意思。它的意思是计划所关联的日期,如果日期没有被关联,则相当于日历中所有日期。例如5C在日期字段中就相当于日历5日以后的第一天。1C在星期字段中相当于星期日后的第一天。

表示式

说明

0 15 10 ? * *

每天10:15运行

0 15 10 * * ?

每天10:15运行

0 15 10 * * ? 2008

在2008年的每天10:15运行

0 * 14 * * ?

每天14点到15点之间每分钟运行一次,开始于14:00,结束于14:59

0 0/5 14 * * ?

每天14点到15点每5分钟运行一次,开始于14:00,结束于14:55

0 0/5 14,18 * * ?

每天14点到15点每5分钟运行一次,此外每天18点到19点每5钟也运行一次

0 0-5 14 * * ?

每天14:00点到14:05,每分钟运行一次

0 15 10 ? * MON-FRI

每周一,二,三,四,五的10:15分运行

0 15 10 L * ?

每月最后一天10:15分运行

0 15 10 ? * 6L

每月最后一个星期五10:15分运行

0 15 10 ? * 6#3

每月第三个星期五的10:15分运行

public static void main(String[] args) throws SchedulerException {

JobDetail detail = new JobDetail("jName", "jGroup", PrintJob.class);

CronTrigger trigger = new CronTrigger("ctName", "ctGroup");

// 定义表达式

CronExpression cexp = new CronExpression("0/5 * * * * ?");

trigger.setCronExpression(cexp);

SchedulerFactory factory = new StdSchedulerFactory();

Scheduler scheduler = factory.getScheduler();

// JobDetail与Trigger关联

scheduler.scheduleJob(detail, trigger);

scheduler.start();

}

(3)使用Calendar

public static void main(String[] args) throws SchedulerException {

// 法定节日是以每年为周期的,所以使用AnnualCalendar

AnnualCalendar holidays = new AnnualCalendar();

// 五一劳动节

Calendar laborDay = new GregorianCalendar();

laborDay.add(Calendar.MONTH, 5);

laborDay.add(Calendar.DATE, 1);

// 排除的日期,如果设置为false则为包含

holidays.setDayExcluded(laborDay, true);

// 国庆节

Calendar nationalDay = new GregorianCalendar();

nationalDay.add(Calendar.MONTH, 10);

nationalDay.add(Calendar.DATE, 1);

holidays.setDayExcluded(nationalDay, true);

SchedulerFactory factory = new StdSchedulerFactory();

Scheduler scheduler = factory.getScheduler();

scheduler.addCalendar("holidays", holidays, false, true);

// 设置JobDetail与Trigger

...

scheduler.start();

}

3.spring配置quartz

使用spring,自定义任务不用实现Job接口:

public class PrintJob {

public void execute() {

System.out.println("executing this job");

}

}

spring-quartz.xml

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

加载配置文件便可以执行:

public static void main(String[] args) throws SchedulerException {

ApplicationContext context = new ClassPathXmlApplicationContext("com/ee/test/quartz/spring-quartz.xml");

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值