Quartz定时任务+spring 实现
一.Quartz的学习简述
客官,不要急,请看完下面的内容...
代码可以直接拷贝使用,本文是编写2个定时方法来实现的,如果想要执行1个,删除另1个即可。但是想要知道执行原理请看最后的原理分析
二.执行任务类
1 package com.quartz; 2 3 import java.util.Date; 4 5 /** 6 *className : QuartzTest 7 *description : 定时任务test 8 *@date : 2017年12月5日 9 */ 10 public class QuartzTest { 11 //要执行的方法1 12 public void run1(){ 13 System.out.println("我是定时任务。。。111。。。run1。。。"+new Date()); 14 } 15 //要执行的方法2 16 public void run2(){ 17 System.out.println("我是定时任务。。。112。。。run2。。。"+new Date()); 18 } 19 }
三.quartz的配置文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" 4 xmlns:tx="http://www.springframework.org/schema/tx" xmlns:cache="http://www.springframework.org/schema/cache" 5 xmlns:task="http://www.springframework.org/schema/task" 6 xmlns:context="http://www.springframework.org/schema/context" 7 xsi:schemaLocation="http://www.springframework.org/schema/beans 8 http://www.springframework.org/schema/beans/spring-beans.xsd 9 http://www.springframework.org/schema/context 10 http://www.springframework.org/schema/context/spring-context.xsd 11 http://www.springframework.org/schema/aop 12 http://www.springframework.org/schema/aop/spring-aop.xsd 13 http://www.springframework.org/schema/tx 14 http://www.springframework.org/schema/tx/spring-tx.xsd 15 http://www.springframework.org/schema/cache 16 http://www.springframework.org/schema/cache/spring-cache.xsd 17 http://www.springframework.org/schema/task 18 http://www.springframework.org/schema/task/spring-task.xsd"> 19 20 <description>qutarz定时任务</description> 21 22 <!-- 定义一个任务 --> 23 <bean id="quartzTest" class="com.quartz.QuartzTest"></bean> 24 25 <!-- 定义第一个要执行方法 --> 26 <bean id="method1" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> 27 <property name="targetObject" ref="quartzTest"></property> 28 <property name="targetMethod" value="run1"></property> 29 </bean> 30 <!-- 定义第二个要执行方法 --> 31 <bean id="method2" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> 32 <property name="targetObject" ref="quartzTest"></property> 33 <property name="targetMethod" value="run2"></property> 34 </bean> 35 36 <!-- 定义第一个方法执行的指定的时间 --> 37 <bean id="cronTrigger1" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> 38 <property name="jobDetail" ref="method1"></property> 39 <!-- 0 0/1 * * * ? 每隔1分钟触发 --> 40 <property name="cronExpression" value="0 0/1 * * * ?"></property> 41 </bean> 42 43 <!-- 定义第二个方法执行的指定的时间 --> 44 <bean id="cronTrigger2" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> 45 <property name="jobDetail" ref="method2"></property> 46 <!-- 47 0 0/1 * * * ? 每隔1分钟触发 48 --> 49 <property name="cronExpression" value="0 0/1 * * * ?"></property> 50 </bean> 51 52 <!-- 指定一个总调度器 --> 53 <bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> 54 <property name="triggers"> 55 <list> 56 <ref bean="cronTrigger1"></ref> 57 <ref bean="cronTrigger2"></ref> 58 </list> 59 </property> 60 </bean> 61 </beans>
四.Quartz中时间表达式的设置-----corn表达式
各位看官在这个地方不要急,花个10分钟时间就可以把这个表达式学好,一定能写出你想要的时间
1. 表达式介绍
秒 分 时 日 月 周 年
必须 必须 必须 必须 必须 必须 非必
2.关键字说明
* 星号: 表示你想在这个域上包含所有合法的值。
例:在分钟域上使用星号意味着每个月都会触发这个定时任务。
0 * 17 * * ? // 每天下午17时的每分钟都会触发这个定时任务
? 问号: 只能用在日和周域上,但是不能在这两个域上同时使用。
例:假如你为这两域的其中一个指定了值,那就必须在另一个字值上放一个,如果不指定,日为*,周为?即可
0 10,44 14 ? 3 WED // 在三月中的每个星期三的下午 2:10 和 下午 2:44 被触发。
, 逗号:是用来在给某个域上的值指定一个可选值列表
例:例如,使用值 0,15,30,45 在秒域上意味着每15秒触发一个定时任务。
0 0,15,30,45 * * * ? // 每刻钟触发一次定时任务
/ 斜杠:用于时间表的递增的。
例:斜杠在左边的值是指开始的时间,斜杠右边是步长,也就是从哪个时间开始,多少的步长执行这个定时任务
0/15 0/30 * * * ? // 在整点和半点时每15秒触发
- 中划线:用于指定一个范围
例:在小时域上的 3-8 意味着 "3,4,5,6,7 和 8 点,但是不能反过来写!!!
0 45 3-8 ? * * // 在上午的3点至上午的8点的45分时触发
L 字母:说明了某域上允许的最后一个值,它仅被日和周域支持。(这个分为2种情况)
1.当用在日域上,表示的是在月域上指定的月份的最后一天。
例:
0 0 8 L * ? // 每个月最后一天的上午 8:00 触发
2.当用在周域上,指示着周的最后一天,就是星期六 (或者数字7)。
例:
0 59 23 ? * L。 // 在每个月的最后一个星期六下午的 11:59 触发
3.列举表达式
1.每分钟触发的
0 0/1 * * * ?
2.每天触发
"0 0 12 * *?" 每天中午12点触发
"0 15 10 ? **" 每天上午10:15触发
3.每月最后一天触发
"0 15 10 L *?" 每月最后一日的上午10:15触发
4.保留字符
# 井号:仅能用于周域中。
W 字母:代表着平日 (Mon-Fri),并且仅能用于日域中。它用来指定离指定日的最近的一个平日。