• Spring-task-timer定时器


     · spring定时控制器配置文件实现方式

        一. 编写一个正常的业务类

    public class SyncDataTaskTimer {
        
        
        private final static Logger log = Logger.getLogger(SyncDataTaskTimer.class); 
        
        /**
         * 同步组织
         */
        public void syncOrg(){
            log.info("同步组织:"+System.currentTimeMillis());
        }
        /**
         * 同步用户
         */
        public void syncUser(){
            log.info("同步用户:"+System.currentTimeMillis());
        }
        
    }

      二. applicationContext-task.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: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:jee="http://www.springframework.org/schema/jee"
        xmlns:task="http://www.springframework.org/schema/task"
        xsi:schemaLocation="
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
        http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context-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/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
        http://www.springframework.org/schema/task
        http://www.springframework.org/schema/task/spring-task-3.0.xsd">
            
        <!-- spring定时任务配置项 -->
        <bean id="scheduledTask" class="org.springframework.scheduling.timer.ScheduledTimerTask"/>
    
        <!-- 同步ipm数据-定时任务配置 -->
        <bean id="syncDataTaskTimer" class="cn.chinaunicom.pmis.interfaces.ipm.server.task.SyncDataTaskTimer"/>
    
        <!-- 任务时间项配置 -->
        <task:scheduled-tasks>
            <task:scheduled ref="syncDataTaskTimer" method="syncOrg" cron="0 30 23 * * ?"/>
            <task:scheduled ref="syncDataTaskTimer" method="syncUser" cron="0 40 23 * * ?"/>
        </task:scheduled-tasks>
        

      三. applicationContext.xml 引用

    <!-- 支持自带定时任务配置 -->
    <import resource="applicationContext-task.xml" />

      注:这里把spring的配置文件进行了拆分。

    ----------------------------------------分割线-----------------------------------------

      · 难点解析

      一. cron语法

        <task:scheduled ref="syncDataTaskTimer" method="syncOrg" cron="0 30 23 * * ?"/>  

         cron="0 30 23 * * ?"意思为每天的23点30分执行一次。

        cron语法使用和字符解释

          cron的使用语法:

            

     (Seconds)  (Minutes)  (Hours)  (DayofMonth)  (Month)  (DayofWeek) [Year]
    [年]

      

           cron的字符意义:

            ① * : 表示在当前区域内匹配任意值,列如如果在秒上,则表示每秒都出发该事件

            ② ? : 表示任意值,使用在dayofmonth和dayofweek上

            ③ / : 使用方式5/15,假如写在秒所在区域上,左边是开始时间,右边是在隔15秒后执行,所以执行频率是, 5秒执行一次,20秒执行一次,35,50

            ④ - : 表示一个区间,如果在分钟区域1-5,其时间内每分钟执行一次

            ⑤ , : 枚举值,在分钟位置上5,15,31,代表这5分,15分,31分各执行一次

            ⑥ # : 2#3,每个月第三个周的星期一(1代表日,2代表星期一)

            ⑦ L : 最后的意思,使用在dayofmonth和dayofweek上,如放在dayofweek=6L,意思是星期五触发。

            ⑧ W :正常工作日周一到周五

            ⑨ LW:每个月最后的一个周五

          

           在网上摘抄一些例子,基本上都包含了,

          

    “0 0 12 * * ?” 每天12:00触发事件
    “0 15 10 ? * *” 每天10:15触发事件
    “0 15 10 * * ?” 每天10:15触发事件
    “0 15 10 * * ? *” 每天10:15触发事件
    “0 15 10 * * ? 2005″ 2005年的每天10:15触发事件
    “0 * 14 * * ?” 每天14点开始触发,每分钟触发一次,14:59分结束
    “0 0/5 14 * * ?” 每天14点开始触发到14:59分结束的每5分钟触发一次事件
    “0 0/5 14,18 * * ?” 每天14点开始到14:59期间和18点到18:59期间的每5分钟触发一次事件
    “0 0-5 14 * * ?” 每天14点到14:05期间的每1分钟触发一次事件
    “0 10,44 14 ? 3 WED” 每年3月的星期三的14:10和14: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大步骤
    [转] js 实现table每列可左右拖动改变列宽度
    [转]在Excel中使用SQL语句实现精确查询
    [书目20150727]有效沟通-余世维
    [书目20150727]选择重于一切(扎克伯格给年轻人的37个人生忠告)
    [转]把汉字写“绝”了
    [转]《间架结构92法》
    深圳社保转入
    广州社保转出
    [转]利用telnet进行SMTP的验证
  • 原文地址:https://www.cnblogs.com/duwenlei/p/5069021.html
Copyright © 2020-2023  润新知