2019年6月22日重新整理
这里我们选择的项目框架是spring3.2.9 + quartz1.6.6 来完成相关的功能
首先说明一下,由于是Web项目,我们一般使用配置文件进行配置相关的类,然后在类文件中编写自己的逻辑即可。
注意:quartz类库中的关于定时器的操作可以分两种 简单定时器 和 基于表达式的定时器(复杂)
简单定时器:比如系统启动后多久执行以及执行多少次(1次或多次),这种是以固定的时间为单位周期性的执行。比如:每10秒、每2小时执行一次,时间周期总是以ms为单位)
基于表达式的定时器:这种更适合复杂的场景,比如:每天早上8点、每个星期五晚上10点、每月1日几点、每个周末执行、或者其他复杂的时间点执行任务。(每天执行定时器,像图书馆借书到期提醒,3天后到期的用户,系统通过微信、短信、邮件或其他方式通知用户)
后面我们将分别进行配置及说明。由于本人能力有限,本文描述可能有些错误,如发现请指出。
1、spring相关的jar文件和quartz的jar文件添加我们的项目中来(非Maven项目,需要提前下载好这些jar文件)
2、(基础)配置项目中的web.xml及spring核心配置文件(新建applicationContext.xml)
web.xml,加载spring配置文件
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
3、编写定时任务处理类 QuartzJob1.java
public class QuartzJob1
public void work(){
task1();
}
public void task1(){
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
System.out.println(df.format(new Date().getTime())+" 调度任务1 每隔5秒钟执行一次");
}
}
4、applicationContext.xml配置quartz及任务处理类
简单定时器(配置quartz中的SimpleTriggerBean类)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">
<!-- 实例化任务处理类 -->
<bean id="quartzJob1" class="com.timer.job.QuartzJob1" ></bean>
<!-- 配置quartz中关联任务处理类及执行的方法 -->
<!-- 定义生成工作对象的工厂,并为工厂设定目标对象targetObject属性、目标对象的工作方法targetMethod属性 -->
<bean id="jobtask1" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject">
<ref bean="quartzJob1" />
</property>
<!-- 执行哪个方法 -->
<property name="targetMethod"><value>work</value></property>
<!-- 是否允许任务并发执行,默认为true。当值为false时,表示必须等到前一个线程处理完毕后才再启一个新的线程 -->
<property name="concurrent" value="false"/>
</bean>
<!-- quartz实例化5秒后执行一次job的方式 -->
<bean id="cronTrigger1" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean">
<property name="jobDetail" ref="jobtask1" />
<!-- 配置5秒执行一次任务(执行一次或多次) 开始 -->
<!-- 启动延迟(服务启动后多少秒之后启动定时器) 单位/毫秒-->
<property name="startDelay" value="5000" />
<!-- 重复次数:0表示不重复,即系统启动成功5秒后只执行一次任务 -->
<property name="repeatCount" value="0" />
<!-- 配置5秒执行一次任务(执行一次或多次) 结束 -->
<!-- 另一种配置(系统启动一次后,按固定的时间(5秒)周期性执行)开始 -->
<!--
启动延迟(服务启动后多少秒之后启动定时器) 单位/毫秒
<property name="startDelay" value="5000" />
-->
<!-- 这里定义的是循环的周期,单位是毫秒
<property name="repeatInterval" value="5000" />
-->
<!-- 另一种配置(系统启动一次后,按固定的时间(5秒)周期性执行)结束 -->
</bean>
<!-- 触发器(可以配置多个) 如果将lazy-init='false'那么容器启动就会执行调度程序 -->
<bean id="startQuertz" lazy-init="false" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="cronTrigger1" />
</list>
</property>
</bean>
</beans>
基于表达式的定时器(配置quartz中的CronTriggerFactoryBean类),
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">
<!-- 实例化任务处理类 -->
<bean id="quartzJob1" class="com.timer.job.QuartzJob1" ></bean>
<!-- 配置quartz中关联任务处理类及执行的方法 -->
<!-- 定义生成工作对象的工厂,并为工厂设定目标对象targetObject属性、目标对象的工作方法targetMethod属性 -->
<bean id="jobtask1" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject">
<ref bean="quartzJob1" />
</property>
<!-- 执行哪个方法 -->
<property name="targetMethod"><value>work</value></property>
<!-- 是否允许任务并发执行,默认为true。当值为false时,表示必须等到前一个线程处理完毕后才再启一个新的线程 -->
<property name="concurrent" value="false"/>
</bean>
<!-- quartz实例化5秒后执行一次job的方式
cronExpression可以配置复杂的时间,这里为了演示配置一个简单的时间
-->
<bean id="cronTrigger1" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="jobtask1" />
<!-- 配置5秒执行一次任务(周期性执行) 开始 -->
<!-- cron表达式 -->
<property name="cronExpression">
<!-- 每隔5秒执行一次 <value>0/5 * * * * ?</value> -->
<!-- 每隔10分钟执行一次 <value>0 0/10 * * * ?</value> -->
<value>0/5 * * * * ?</value>
<!-- 配置5秒执行一次任务(周期性执行) 结束 -->
</property>
</bean>
<!-- 触发器(可以配置多个) 如果将lazy-init='false'那么容器启动就会执行调度程序 -->
<bean id="startQuertz" lazy-init="false" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="cronTrigger1" />
</list>
</property>
</bean>
</beans>
到此简单定时器或复杂定时器配置到此结束,可以结合自己的业务场景来选择使用哪种定时器。注意配置文件中唯一的配置区别就是 SimpleTriggerBean类与CronTriggerFactoryBean类以及相关的属性。
好了,我们可以启动web服务器,观察后台定时任务程序是否正常执行。
关于CronTriggerFactoryBean类中的cronExpression属性参数,详细内容请往下看看
spring集成了quartz,它允许开发人员根据时间间隔(或天)来调度作业,只需要简单的配置,就可以实现定时任务,代码耦合低。
Quartz的cron表达式
一个cron表达式有至少6个(也可能是7个)由空格分隔的时间元素.从左到右,这些元素的定义如下:
1.秒(0-59)
2.分钟(0-59)
3.小时(0-23)
4.月份中的是期(1-31)
5.月份(1-12或SUN-DEC)
6.星期中的日期(1-7或SUN-SAT)
7.年份(1970-2099)
例子:
0 0 10,14,16 * * ? 每天上午10点,下午2点和下午4点
0 0,15,30,45 * 1-10 * ? 每月前10天每隔15分钟
30 0 0 1 1 ? 2012 在2012年1月1日午夜过30秒时
0 0 8-5 ? * MON-FRI 每个工作日的工作时间
- 区间
* 通配符
? 你不想设置那个字段
关于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 | , - * / |
Cron表达式的时间字段除允许设置数值外,还可使用一些特殊的字符,提供列表、范围、通配符等功能:
●星号(*):可用在所有字段中,表示对应时间域的每一个时刻,例如,*在分钟字段时,表示“每分钟”;
●问号(?):该字符只在日期和星期字段中使用,它通常指定为“无意义的值”,相当于点位符;
●减号(-):表达一个范围,如在小时字段中使用“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在星期字段中相当于星期日后的第一天。
Cron表达式对特殊字符的大小写不敏感,对代表星期的缩写英文大小写也不敏感。
eg:
"0 0 12 * * ?" 每天中午十二点触发
"0 15 10 ? * *" 每天早上10:15触发
"0 15 10 * * ?" 每天早上10:15触发
"0 15 10 * * ? *" 每天早上10:15触发
"0 15 10 * * ? 2005" 2005年的每天早上10:15触发
"0 * 14 * * ?" 每天从下午2点开始到2点59分每分钟一次触发
"0 0/5 14 * * ?" 每天从下午2点开始到2:55分结束每5分钟一次触发
"0 0/5 14,18 * * ?" 每天的下午2点至2:55和6点至6点55分两个时间段内每5分钟一次触发
"0 0-5 14 * * ?" 每天14:00至14:05每分钟一次触发
"0 10,44 14 ? 3 WED" 三月的每周三的14:10和14:44触发
"0 15 10 ? * MON-FRI" 每个周一、周二、周三、周四、周五的10:15触发
其他参考资料
Quartz快速入门(w3cschool)
spring Quartz多个定时任务的配置
Quartz的基本使用之入门(2.3.0版本)
Spring+Quartz结合使用(Maven版)
1. Maven导包
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
2. 定义一个任务类MyJob.java
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.context.ApplicationContext;
import org.springframework.scheduling.quartz.QuartzJobBean;
public class Myjob extends QuartzJobBean{
private static int count=1;
@Override
protected void executeInternal(JobExecutionContext arg0) throws JobExecutionException {
System.out.println("Myjob开始执行了。。。。"+arg0.getTrigger().getKey().getName());
ApplicationContext applicationContext=(ApplicationContext)arg0.getJobDetail().getJobDataMap()
.get("applicationContext");
System.out.println("获取到的容器是:"+(count++)+"|"+applicationContext);
}
}
3. 增加一个spring的配置文件,applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- 定义任务Bean -->
<bean name="myJobDetail" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
<!-- 指定具体的job类 -->
<property name="jobClass" value="com.quartz.Myjob"></property>
<!-- 指定job的名称 -->
<property name="name" value="myJob"></property>
<!-- 指定job的分组 -->
<property name="group" value="jobs"></property>
<!-- 必须设置为true,如果为false,当没有活动的触发器与之关联的时候回在调度器中删除该任务 -->
<property name="durability" value="true"></property>
<!-- 指定spring容器的可以,如果不设定在job中的jobmap中是获取不到spring容器的 -->
<property name="applicationContextJobDataKey" value="applicationContext"></property>
</bean>
<!-- 定义触发器 -->
<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="myJobDetail"></property>
<!-- 定义定时表达式 -->
<property name="cronExpression" value="/5 * * * * ?"></property>
</bean>
<!-- 定义调度器 -->
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="cronTrigger"/>
</list>
</property>
</bean>
</beans>
4. 测试类
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
}
}
5. 测试结果
八月 09, 2017 7:11:40 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@49c2faae: startup date [Wed Aug 09 19:11:40 GMT+08:00 2017]; root of context hierarchy
八月 09, 2017 7:11:40 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [applicationContext.xml]
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
八月 09, 2017 7:11:40 下午 org.springframework.context.support.DefaultLifecycleProcessor start
信息: Starting beans in phase 2147483647
八月 09, 2017 7:11:40 下午 org.springframework.scheduling.quartz.SchedulerFactoryBean startScheduler
信息: Starting Quartz Scheduler now
Myjob开始执行了。。。。cronTrigger
获取到的容器是:1|org.springframework.context.support.ClassPathXmlApplicationContext@49c2faae: startup date [Wed Aug 09 19:11:40 GMT+08:00 2017]; root of context hierarchy
Myjob开始执行了。。。。cronTrigger
获取到的容器是:2|org.springframework.context.support.ClassPathXmlApplicationContext@49c2faae: startup date [Wed Aug 09 19:11:40 GMT+08:00 2017]; root of context hierarchy
Myjob开始执行了。。。。cronTrigger
获取到的容器是:3|org.springframework.context.support.ClassPathXmlApplicationContext@49c2faae: startup date [Wed Aug 09 19:11:40 GMT+08:00 2017]; root of context hierarchy
https://blog.csdn.net/qq_23134139/article/details/77010868