spring定时任务之-quartz调度器

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

    查资料发现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触发 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值