zoukankan      html  css  js  c++  java
  • Spring整合quartz框架实现任务定时调度

      1.  首先需要引入需要的jar包,如上图所示。

           2. 编写需要定时调度的测试类:   

    package com.jp.task;
    import java.util.Date;
    public class TestTask{    
        private static int counter = 0;   
        protected void execute()  {   
              long ms = System.currentTimeMillis();   
              System.out.println("		" + "========================================"    +     new Date(ms));  
              System.out.println("(" + counter++ + ")");   
          }   
    }

      3.编写配置文件classpath:quartz.xml,放在工程的src目录下:

    <?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: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/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">
      
            <!-- 定义目标bean和bean中的方法 -->
        <bean id="SpringQtzJob" class="com.jp.task.TestTask"/>
        <bean id="SpringQtzJobMethod" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
            <property name="targetObject">
                <ref bean="SpringQtzJob"/>
            </property>
            <property name="targetMethod">  <!-- 要执行的方法名称 -->
                <value>execute</value>
            </property>
        </bean>
    
        <!-- ======================== 调度触发器 ======================== -->
        <bean id="CronTriggerBean" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
            <property name="jobDetail" ref="SpringQtzJobMethod"></property>
            <property name="cronExpression" value="0/5 * * * * ?"></property>
        </bean>
    
        <!-- ======================== 调度工厂 ======================== -->
        <bean id="SpringJobSchedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
            <property name="triggers">
                <list>
                    <ref bean="CronTriggerBean"/>
                </list>
            </property>
        </bean>  
    </beans>

      4.设置Spring启动时就加载quartz的配置文件,需要在web.xml中加上下面的代码:

    <listener>
        <description>spring监听器</description>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      </listener>
      <context-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>classpath:bean.xml;classpath:quartz.xml</param-value>
      </context-param>

      5.关于cronExpression表达式,这里讲解一下,就是第3步配置文件中的value="0/5 * * * * ?"

    秒 0-59 , - * /
    分 0-59 , - * /
    小时 0-23 , - * /
    日期 1-31 , - * ? / L W C
    月份 1-12 或者 JAN-DEC , - * /
    星期 1-7 或者 SUN-SAT , - * ? / L C #
    年(可选) 留空, 1970-2099 , - * /
    表达式意义
    "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:10和2: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触发
    每天早上6点
    0 6 * * *
    每两个小时
    0 */2 * * *
    晚上11点到早上8点之间每两个小时,早上八点
    0 23-7/2,8 * * *
    每个月的4号和每个礼拜的礼拜一到礼拜三的早上11点
    0 11 4 * 1-3
    1月1日早上4点
    0 4 1 1 *

       6.还要需要多个任务时的情况,参考:http://nesuk.iteye.com/blog/1582557

      关于Spring集成Quartz的concurrent属性
      以前经常在任务调度程序中使用Spring集成的Quartz,这种方式可以用简单的声明式配置即可实现定时任务,并结合了Spring自身的Bean的管理功能,非常方便。配置样本如下:

    <bean id="transmitTask" 
    class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <property name="targetObject">
            <ref bean="transTaskBusiness" />
        </property>
        <property name="targetMethod">
            <value>execute</value>
        </property>
        <property name="concurrent">
            <value>false</value>
        </property>
    </bean>

      其中concurrent属性标识的解释为:对于相同的JobDetail,当指定多个Trigger时,
    很可能第一个job完成之前,第二个job就开始了。指定concurrent设为false,多个job不会并发运行,第二个job将不会在第一个job完成之前开始。

      那么此处的第二个job到底是被推迟执行了,还是被取消了呢?
      今天带着这个疑问做了一个小实验,具体做法如下:将任务的Trigger设置为每10秒钟执行一次,然后在执行的任务体内让Thread暂停15秒钟,这样不同的job之间肯定会有时间上的重叠。当启动程序之后,发现前一个job执行结束的时刻的秒数为15,而且第二个本来在10s就执行的job马上就启动了。

  • 相关阅读:
    Ext JS 6开发实例(三) :主界面设计
    Ext JS 6开发实例(二) :使用CMD创建应用程序
    文件夹或者文件比对工具 Beyond Compare
    LIS问题(DP解法)---poj1631(模板)
    hdoj Max Sum Plus Plus(DP)
    A* 算法详解
    hdoj1043 Eight(逆向BFS+打表+康拓展开)
    hdoj2612 Find a way (bfs)
    luoguP3366 [模板] 最小生成树
    luoguP1196(带权并查集)
  • 原文地址:https://www.cnblogs.com/lnlvinso/p/4194725.html
Copyright © 2011-2022 走看看