quartz定时任务实现

适用于spring框架

1.在web.xml文件中引入spring-quartz.xml文件。

<!-- 读取spring配置文件 -->
 <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:config/spring.xml;
            classpath:config/spring-mybatis.xml;
             classpath:config/spring-quartz.xml;
        </param-value>
 </context-param>

2.spring-quartz.xml文件如下

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:cache="http://www.springframework.org/schema/cache"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/cache
        http://www.springframework.org/schema/cache/spring-cache.xsd">
    <!-- 定时器工作调度的bean -->
     <bean id="trainPassengerTraffic" class="springmvc.wx.controller.train.trainPassengerTraffic" />
    <bean id="trainCode" class="springmvc.wx.controller.train.trainCode" />
    <bean id="trainIntoTimeShare" class="springmvc.wx.controller.train.trainIntoTimeShare" />
    <bean id="trainLateInfo" class="springmvc.wx.controller.train.trainLateInfo" />
    <bean id="trainleaveTimeShare" class="springmvc.wx.controller.train.trainleaveTimeShare" />
    <bean id="trainTwoInto" class="springmvc.wx.controller.train.trainTwoInto" />
    <bean id="trainTwoLeave" class="springmvc.wx.controller.train.trainTwoLeave" />
    <!-- job的配置开始 --> 
    <bean id="job1"
        class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
          <property name="targetObject" ref="trainPassengerTraffic"/> 
       <property name="targetMethod" value="getValue"/> 
       <property name="concurrent" value="true"/> 
    </bean>
     <bean id="job2"
        class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
          <property name="targetObject" ref="trainCode"/> 
       <property name="targetMethod" value="getValue"/> 
       <property name="concurrent" value="true"/> 
    </bean>
     <bean id="job3"
        class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
          <property name="targetObject" ref="trainIntoTimeShare"/> 
       <property name="targetMethod" value="getValue"/> 
       <property name="concurrent" value="true"/> 
    </bean>
     <bean id="job4"
        class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
          <property name="targetObject" ref="trainLateInfo"/> 
       <property name="targetMethod" value="getValue"/> 
       <property name="concurrent" value="true"/> 
    </bean>
     <bean id="job5"
        class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
          <property name="targetObject" ref="trainleaveTimeShare"/> 
       <property name="targetMethod" value="getValue"/> 
       <property name="concurrent" value="true"/> 
    </bean>
     <bean id="job6"
        class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
          <property name="targetObject" ref="trainTwoInto"/> 
       <property name="targetMethod" value="getValue"/> 
       <property name="concurrent" value="true"/> 
    </bean>
     <bean id="job7"
        class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
          <property name="targetObject" ref="trainTwoLeave"/> 
       <property name="targetMethod" value="getValue"/> 
       <property name="concurrent" value="true"/> 
    </bean>
    <!-- 调度的配置开始 -->
    <!-- 客运量统计数据:数据更新频率:每天更新一次,大约每天早上9点更新完成。 -->
    <bean id="crontestJobTrigger1" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
        <property name="jobDetail">
            <ref bean="job1" />
        </property>
        <property name="cronExpression">
             <value>0 15 9 * * ? </value> 
        </property>
    </bean>
    <!-- 车次统计数据:数据更新频率:每天更新一次,大约每天早上6点更新完成。 -->
     <bean id="crontestJobTrigger2" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
        <property name="jobDetail">
            <ref bean="job2" />
        </property>
        <property name="cronExpression">
             <value>0 15 6 * * ? </value> 
        </property>
    </bean>
 
    <!-- 到站列车分时统计数据:  数据更新频率:2小时更新一次,1、3、5…23点更新 -->
     <bean id="crontestJobTrigger3" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
        <property name="jobDetail">
            <ref bean="job3" />
        </property>
        <property name="cronExpression">
             <value>0 0 1,3,5,7,9,11,13,15,17,19,21,23 * * ? </value> 
        </property>
    </bean>
     <!-- 列车晚点统计数据:数据更新频率:每小时更新一次,每小时的15分左右更新数据。-->
     <bean id="crontestJobTrigger4" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
        <property name="jobDetail">
            <ref bean="job4" />
        </property>
        <property name="cronExpression">
             <value>0 20 0/1 * * ? </value> 
        </property>
    </bean>
     <!-- 离站列车分时统计数据:  数据更新频率:2小时更新一次,1、3、5…23点更新 -->
     <bean id="crontestJobTrigger5" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
        <property name="jobDetail">
            <ref bean="job5" />
        </property>
        <property name="cronExpression">
             <value>0 0 1,3,5,7,9,11,13,15,17,19,21,23 * * ?</value> 
        </property>
    </bean>
    <!-- 2小时进站统计数据:  数据更新频率:2小时更新一次,1、3、5…23点更新 -->
     <bean id="crontestJobTrigger6" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
        <property name="jobDetail">
            <ref bean="job6" />
        </property>
        <property name="cronExpression">
             <value>0 0 1,3,5,7,9,11,13,15,17,19,21,23 * * ?</value> 
        </property>
    </bean>
    <!-- 2小时离站统计数据:  数据更新频率:2小时更新一次,1、3、5…23点更新 -->
     <bean id="crontestJobTrigger7" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
        <property name="jobDetail">
            <ref bean="job7" />
        </property>
        <property name="cronExpression">
             <value>0 0 1,3,5,7,9,11,13,15,17,19,21,23 * * ?</value> 
        </property>
    </bean>
    <!-- 启动触发器的配置开始 -->
    <bean name="startQuertz1" lazy-init="false" autowire="no"
        class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
            <list>
                <ref bean="crontestJobTrigger1" />
            </list>
        </property>
    </bean>
     <bean name="startQuertz2" lazy-init="false" autowire="no"
        class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
            <list>
                <ref bean="crontestJobTrigger2" />
            </list>
        </property>
    </bean>
     <bean name="startQuertz3" lazy-init="false" autowire="no"
        class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
            <list>
                <ref bean="crontestJobTrigger3" />
            </list>
        </property>
    </bean>
     <bean name="startQuertz4" lazy-init="false" autowire="no"
        class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
            <list>
                <ref bean="crontestJobTrigger4" />
            </list>
        </property>
    </bean>
     <bean name="startQuertz5" lazy-init="false" autowire="no"
        class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
            <list>
                <ref bean="crontestJobTrigger5" />
            </list>
        </property>
    </bean>
     <bean name="startQuertz6" lazy-init="false" autowire="no"
        class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
            <list>
                <ref bean="crontestJobTrigger6" />
            </list>
        </property>
    </bean>
     <bean name="startQuertz7" lazy-init="false" autowire="no"
        class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
            <list>
                <ref bean="crontestJobTrigger7" />
            </list>
        </property>
    </bean>
   
   
</beans>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值