【JAVA秒会技术之搞定Quartz定时任务】Quartz在Spring中的集成与应用

  Quartz在Spring中的集成与应用

  一、Quqrtz简介

    Quartz是一个完全由Java编写的开源任务调度的框架,通过触发器设置作业定时运行规则,控制作业的运行时间。主要用来执行定时任务,如:定时发送信息、定时生成报表等等。

简而言之,Quartz是一个定时器组件,是可以整合Spring使用的一个定时器。

    二、Quqrtz的配置文件

    1.maven中引入:

<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context-support</artifactId>
    <version>4.1.3.RELEASE</version>
</dependency>

   2.Spring-quqrtz.xml(以每天00:01定时执行一个任务为例):

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc  
    http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd  
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
    http://www.springframework.org/schema/jdbc
    http://www.springframework.org/schema/jdbc/spring-jdbc-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/aop
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
  <!-- Quartz配置文件-->
 
<!-- 定义执行任务:class为要执行任务的类的全限定名 -->    
    <bean id="Job" class="com.**.XXXController"/> 
    
<!-- 定义JobDetail(任务详情) -->
<bean id="JobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">   
  <!-- 执行任务的Bean类-->
        <property name="targetObject" ref="Job"/>
        <!-- 执行任务的方法 -->  
        <property name="targetMethod" value="具体执行任务的方法名称"/>  
        <!-- 是否允许多个任务并发执行。当值为false时,表示必须等到前一个线程处理完毕后才再启一个新的线程-->  
        <property name="concurrent" value="false"/>
    </bean>  
    
  <!-- 定义Scheduler调度器-->
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="Trigger" />
</list>
</property>
<!-- 是否自动启动 -->
<property name="autoStartup" value="true" />
</bean>
<!-- 定义Trigger(触发的时间) -->
<bean id="Trigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="JobDetail" />
<!-- 每天00:01开始执行-->
<property name="cronExpression" value="0 1 0 * * ?" />
</bean>
</beans>



   
1Cron表达式的格式
(可选)    三、cron表达式

字段名

允许的值

允许的特殊字符

0-59 

, - * /

0-59 

, - * /

小时

0-23

, - * /

1-31

, - * ? / L W C 

1-12 or JAN-DEC 

 , - * / 

周几 

1-7 or SUN-SAT

 , - * ? / L C #

(可选字段)

empty, 1970-2099

 , - * / 

特殊字符说明:

?”字符

表示不确定的值

,”字符

指定数个值

-”字符

指定一个值的范围

/”字符

指定一个值的增加幅度。n/m表示从n开始,每次增加m

L”字符

用在日表示一个月中的最后一天,用在周表示该月最后一个星期X

W”字符

指定离给定日期最近的工作日(周一到周五)

#”字符

表示该月第几个周X6#3表示该月第3个周五

  

   2Cron表达式范例。

表达式

含义

0 0 12 * * ?

每天中午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期间的每1分钟触发

0 0/5 14 * * ?

在每天下午2点到下午2:55期间的每5分钟触发

0 0/5 14,18 * * ?

在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发

0 0-5 14 * * ?

在每天下午2点到下午2:05期间的每1分钟触发

0 10,44 14 ? 3 WED

每年三月的星期三的下午2:102:44触发

0 15 10 ? * MON-FRI

周一至周五的上午10:15触发

0 15 10 15 * ?

每月15日上午10:15触发

0 15 10 L * ?

每月最后一日的上午10:15触发

0 15 10 ? * 6L

每月的最后一个星期五上午10:15触发

0 15 10 ? * 6L 2002-2005

2002年至2005年的每月的最后一个星期五上午10:15触发

0 15 10 ? * 6#3

每月的第三个星期五上午10:15触发

    四、触发器表达式生成器

    当然,如果你嫌Cron表达式手写费事,没关系,博主给大家提供一个CronExpBuilder-1.0(触发器表达式生成器),纯傻瓜式操作。

    下载地址:http://download.csdn.net/detail/qq296398300/9743045

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值