定时任务

Naims_task.xml

<?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:p="http://www.springframework.org/schema/p"
  xmlns:task="http://www.springframework.org/schema/task"
  xmlns:context="http://www.springframework.org/schema/context"
  xmlns:aop="http://www.springframework.org/schema/aop" 
  xsi:schemaLocation="http://www.springframework.org/schema/beans  
  http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 
  http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd  
  http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd  
  http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd  
  http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd  
  http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd">
<task:annotation-driven /> <!-- 定时器开关-->
 
  <bean id="myTask" class="com.wisoft.jazwfw.convenienceServices.controller.ConvenienceServicesController"></bean> 
 
  <task:scheduled-tasks> 
    <!-- 这里表示的是每天23点59分执行一次 -->
    <task:scheduled ref="myTask" method="getPubService" cron="0 59 23 * * ?" /> 
    <!-- 这里表示的是每隔十秒执行一次 -->
    <!-- <task:scheduled ref="myTask" method="print" cron="*/10 * * * * ?"/>  -->
  </task:scheduled-tasks> 
 
  <!-- 自动扫描的包名 -->
  <context:component-scan base-package="com.wisoft.jazwfw.convenienceServices.controller" /> 
  
</beans>

然后在Naims_main.xml引入

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
    <import resource="spring/Naims_ws.xml" />
    <import resource="spring/Naims_bo.xml" />
    <import resource="spring/Naims_dao.xml"/>
    <import resource="spring/Naims_params.xml"/>
    <import resource="spring/Naims_task.xml"/>
</beans>

 ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

spring的定时任务配置分为三个步骤: 
1、定义任务
2、任务执行策略配置
3、启动任务

1、定义任务
<!--要定时执行的方法-->
<bean id="testTaskJob"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject">
<!--指定要定时执行的方法所在类,将定时任务定义成bean-->
<ref bean="testTask" />
</property>
<property name="targetMethod">
<!--指定定时执行的方法-->
<value>execute</value>
</property>
<property name="concurrent">
<!--指定目标封装为有状态的任务,有状态的任务不能并发执行,无状态的任务可并发执行-->
<value>false</value>
</property>
</bean>

2、任务执行策略配置
(1)指定重复间隔的定时任务
<!-- 调度时间设置-->
<bean id="testTaskJobTrigger"
class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<property name="jobDetail">
<ref bean="testTaskJob" />
</property>
<!-- 延时启动时间,单位ms -->
<property name="startDelay" value="60000"></property>
<!-- 重复间隔时间,单位ms -->
<property name="repeatInterval" value="60000">
</property>
</bean>

(2)按周期执行的任务
<!-- 定义触发时间 -->
<bean id="doTime"
class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail">
<ref bean="testTaskJob" />
</property>
<!-- cron表达式,此处是每天10点42执行 -->
<property name="cronExpression">
<value>0 42 10 * * ?</value>
</property>
</bean>

3、启动任务
<!--启动工作-->
<bean lazy-init="false"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<!-- 这里添加多个定时任务触发器,对应第2步的bean id -->
<ref local="testTaskJobTrigger" />
</list>
</property>
</bean>

至此,系统启动的时候,就能加载定时任务,并按照指定的定时策略执行。

对于一次性定时任务,一般有如下几种形式:(1)使用spring的init方法;(2)在使用时判断是否需要加载,确保只执行一次。

cron表达式的基本使用:
 
字段 允许值 允许的特殊字符
秒 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 *

-----------------------------------------------------------------------------------------------------------------------------------------

<!-- 配置定时任务 -->
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
            <list>
                <ref local="cronTrigger" />
            </list>
        </property>
    </bean>
     
    <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
        <property name="jobDetail">
            <ref bean="YOUR_JOB_BEAN_ID" />
        </property>
        <property name="cronExpression">
            <!-- 每隔两分钟运行
            <value>0 0/2 * * * ?</value> -->
            <!-- 每天2:30运行-->
            <value>0 30 2 * * ?</value>
        </property>
    </bean>
     
    <bean id="YOUR_JOB_BEAN_ID" class="org.springframework.scheduling.quartz.JobDetailBean">
        <property name="jobClass">
            <value>你的任务类,完整的包名+类名</value>
        </property>
        <property name="jobDataAsMap">
            <map>
                    <!-- 任务类中的引用的外部变量配置 -->
                <entry key="service" value-ref="xxxService" />
            </map>
        </property>
 
    </bean>

 

posted @ 2017-07-13 12:51  jassy  阅读(346)  评论(0编辑  收藏  举报