什么是Quartz
Quartz是OpenSymphony开源组织在Job scheduling领域的开源项目,它可以与J2EE与J2SE应用程序相结合也可以单独使用。Quartz是一个任务日程管理系统,一个在预先确定(被纳入日程)的时间到达时,负责执行(或者通知)其他软件组件的系统。
Quartz的依赖引入
quartz一般结合spring框架整合使用,引入如下依赖
<dependency>
<groupId>org.quartz-scheduler</groupId> // quartz依赖
<artifactId>quartz</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId> // 为quartz和spring整合的依赖
<artifactId>spring-context-support</artifactId>
<version>4.1.3.RELEASE</version>
</dependency>
Quartz框架结构
- Scheduler 核心调度器
- Job 任务
- JobDetail 任务描述
- Trigger 触发器
Cron表达式规范
- Seconds
- Minutes
- Hours
- Day-of-Month
- Month
- Day-of-Week
- Year (可选字段)
每一个字段都有一套可以指定有效值,如
- Seconds (秒) :可以用数字0-59 表示
- Minutes(分) :可以用数字0-59 表示
- Hours(时) :可以用数字0-23表示
- Day-of-Month(天) :可以用数字1-31 中的任意一个值
- Month(月) :可以用0-11 或用字符串 “JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV and DEC” 表示
- Day-of-Week(每周):可以用数字1-7表示(1 = 星期日)或用字符口串“SUN, MON, TUE, WED, THU, FRI and SAT”表示
- “/”:为特别单位,表示为“每”如“0/15”表示每隔15分钟执行一次,“0”表示为从“0”分开始, “3/20”表示表示每隔20分钟执行一次,“3”表示从第3分钟开始
- “?”:表示每月的某一天,或第周的某一天
- “L”:用于每月,或每周,表示为每月的最后一天,或每个月的最后星期几如“6L”表示“每月的最后一个星期五”
- “W”:表示为最近工作日,如“15W”放在每月(day-of-month)字段上表示为“到本月15日最近的工作日”
- ““#”:是用来指定“的”每月第n个工作日,例 在每周(day-of-week)这个字段中内容为”6#3” or “FRI#3” 则表示“每月第三个星期五
Cron表达式范例
每隔5秒执行一次:*/5 * * * * ?
每隔1分钟执行一次:0 */1 * * * ?
每天23点执行一次:0 0 23 * * ?
每天凌晨1点执行一次:0 0 1 * * ?
每月1号凌晨1点执行一次:0 0 1 1 * ?
每月最后一天23点执行一次:0 0 23 L * ?
每周星期天凌晨1点实行一次:0 0 1 ? * L
在26分、29分、33分执行一次:0 26,29,33 * * * ?
每天的0点、13点、18点、21点都执行一次:0 0 0,13,18,21 * * ?
使用Quartz定时任务
1 编写任务(Job)类,创建一个任务处理方法
package com.mrfy.service.impl;
/**
* 任务类
*/
public class MongoTask {
public void execute() {
System.out.println("定时任务执行了~~");
}
}
2 配置job类到spring容器中,本例在applicationContext-quartz.xml中配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
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-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">
<!-- 配置job类 -->
<bean id="mongoTask" class="com.mrfy.service.impl.MongoTask"></bean>
<!-- 配置jobDetail -->
<bean id="mongoTaskDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject"> <!-- 要包装的任务类 -->
<ref bean="mongoTask" />
</property>
<property name="targetMethod"> <!-- 要执行的方法名称 -->
<value>execute</value>
</property>
</bean>
<!-- 配置调度触发器 -->
<bean id="mongoTaskTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="mongoTaskDetail"></property>
<property name="cronExpression" value="0/5 * * * * ?"></property>
</bean>
<!-- 配置调度工厂 -->
<bean id="mongoTaskScheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="mongoTaskTrigger"/>
</list>
</property>
</bean>
</beans>
3 启动项目,发现已经每隔5秒在打印输出
三月 21, 2018 12:03:53 上午 org.apache.coyote.AbstractProtocol start
信息: Starting ProtocolHandler ["http-bio-8080"]
定时任务执行了~~
定时任务执行了~~
定时任务执行了~~
定时任务执行了~~
定时任务执行了~~
使用springTask定时任务
1 在任务类上添加注解
package com.mrfy.service.impl;
import java.util.Date;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
/**
* 任务类
*/
@Component
public class MongoTask {
@Scheduled(cron="0/7 * * * * ?")
public void execute() {
System.out.println("定时任务执行了~~" + new Date());
}
}
2 修改spring配置文件,添加如下头信息
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd "
3 配置任务注解驱动以及扫描任务类所在包,因为我在项目中其他地方已经扫描了该包,因此本次不再设置扫描,完整配置文件如下
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd">
<!-- 配置加载定时任务注解驱动 -->
<task:annotation-driven/>
</beans>
4 执行结果如下
信息: Starting ProtocolHandler ["http-bio-8080"]
定时任务执行了~~Wed Mar 21 00:19:07 CST 2018
定时任务执行了~~Wed Mar 21 00:19:14 CST 2018
定时任务执行了~~Wed Mar 21 00:19:21 CST 2018
定时任务执行了~~Wed Mar 21 00:19:28 CST 2018