首页 > 编程语言 > 详细

spring定时任务之-quartz调度器

时间:2017-04-26 12:39:32      阅读:252      评论:0      收藏:0      [点我收藏+]

    公司项目里面有个定时任务,一直想弄清楚其流程,以为很难,今天有空研究了一下,发现其实实现很简单。

    查资料发现spring的定时任务有很多种,quartz调度器也有两种一种是作业类需继承自特定的基类:org.springframework.scheduling.quartz.QuartzJobBean。

另一种则是作业类不需继承特定基类。我觉得第二种更加简单简洁,公司项目里面也是用到的这种方法。接下来就讲一下具体编写步骤:

第一步:编写任务类

Java代码  

    package com.grx.pay.quartz;

    /**
    * 定时任务测试类
    * <功能详细描述>
    *
    * @author sun
    * @version [版本号, 2017年4月26日]
    */
    public class quarts_test
    {
    public void run()
    {
    System.out.println("---------------------------------定时任务测试------------------------------------");
    }
    }

第二步:配置作业类

        <bean id="test_payOrderTask" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <property name="targetObject" ref="test_payOrderManager"></property><!-- 关联的bean -->
        <property name="targetMethod" value="run"></property><!-- 执行的方法 -->
        <property name="concurrent" value="false"/>
        </bean>

        //对应第一步中的方法地址

        <bean id="test_payOrderManager" class="com.grx.pay.quartz.quarts_test" />

 说明:这一步是关键步骤,声明一个MethodInvokingJobDetailFactoryBean,有两个关键属性:targetObject指定任务类,targetMethod指定运行的方法。

第三步:配置作业调度的触发方式(触发器)

<!-- 1分钟执行一次 -->
<bean id="test_payOrderTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="test_payOrderTask" />
<property name="cronExpression" value="0 0/1 * * * ?" />
</bean>

第四步:配置调度工厂 

<!-- 每个定义的任务都要在这里进行引用才能运行 -->
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="test_payOrderTrigger" />
</list>
</property>
</bean>

这样一个定时任务就完成了

下面贴出完整的spring-quartz-test.xml内容,注spring-quartz-test.xml需在springmvc文件中引入(常用引入方法);<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-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/context
http://www.springframework.org/schema/context/spring-context-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/util
http://www.springframework.org/schema/util/spring-util-3.0.xsd">

<!-- 每个定义的任务都要在这里进行引用才能运行 -->
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="test_payOrderTrigger" />
</list>
</property>
</bean>

<bean id="test_payOrderManager" class="com.grx.pay.quartz.quarts_test" />

<bean id="test_payOrderTask" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="test_payOrderManager"></property><!-- 关联的bean -->
<property name="targetMethod" value="run"></property><!-- 执行的方法 -->
<property name="concurrent" value="false"/>
</bean>

<!-- 1分钟执行一次 -->
<bean id="test_payOrderTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="test_payOrderTask" />
<property name="cronExpression" value="0 0/1 * * * ?" />
</bean>

</beans>

 

附录:

cronExpression的配置说明,具体使用以及参数请百度google

字段   允许值   允许的特殊字符

秒    0-59    , - * /

分    0-59    , - * /

小时    0-23    , - * /

日期    1-31    , - * ? / L W C

月份    1-12 或者 JAN-DEC    , - * /

星期    1-7 或者 SUN-SAT    , - * ? / L C #

年(可选)    留空, 1970-2099    , - * / 

- 区间  

* 通配符  

? 你不想设置那个字段

下面只例出几个式子

 

CRON表达式    含义 

"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触发 

spring定时任务之-quartz调度器

原文:http://www.cnblogs.com/ajax-li/p/6767757.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!