zoukankan      html  css  js  c++  java
  • spring task定时器笔记

    定时器有两种方式

    1.延迟启动

       <bean id="timerTaskRunnerChain" class="bingo.uam.task.TimerTaskRunnerChain">
            <property name="timerTasks">
                <list>
                    <bean class="bingo.uam.task.XXXXTask"></bean>
                </list>
            </property>
        </bean>
    
        <bean id="btBuildTimerTask"    
            class="org.springframework.scheduling.timer.ScheduledTimerTask">    
            <!-- 设置启动延迟 -->    
            <property name="delay">    
                <value>3600000</value>   <!-- 1个小时后启动 --> 
            </property>    
            <!-- 后续延迟 -->    
            <property name="period">    
                <value>3600000</value>    
            </property>    
            <!-- 指定触发器信息 -->    
            <property name="timerTask">    
                <ref local="timerTaskRunnerChain" />    
            </property>    
        </bean>
              
        <!-- 使用TimerFactoryBean类,你可以让Spring使用配置创建触发器,并为一组指定的ScheduledTimerTask bean自动创建Timer实例。 -->    
        <bean id="timerFactory"    
            class="org.springframework.scheduling.timer.TimerFactoryBean">    
            <property name="scheduledTimerTasks">    
                <list> 
                    <ref local="btBuildTimerTask" />         
                </list>    
            </property>            
        </bean>

    2.某个设定时刻启动

       <!-- 配置Quartz, 用于凌晨2点启动 -->
        <bean id="blogTimerTask" class="bingo.uam.task.XXXXTask"/>
        <bean id="blogSchedulerJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
            <property name="targetObject" ref="blogTimerTask"/> 
            <property name="targetMethod" value="run"/> <!--启动的方法 -->
            <property name="concurrent" value="false"/>
        </bean>
        <bean id="blogCronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean" >
             <property name="jobDetail" ref="blogSchedulerJobDetail"/>
             <property name="cronExpression">
                  <!-- 秒 分 时 日 月 年  -->
                 <value>0 0 2 * * ?</value> <!-- 每天两点启动 -->
             </property>
         </bean>  
        <bean id="blogSchedulerFactory"  lazy-init="false" autowire="no"  class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
            <property name="triggers">
                <list>
                    <ref local="blogCronTrigger"/>
                </list>
            </property>
        </bean>
  • 相关阅读:
    sublime打开txt文件乱码的问题
    while循环小例
    mongoDB内置文档定义
    WebStorm 10.0.3注册码
    angularjs之ng-mode获取lobject类型里的键值
    前端打印console
    js去掉数组的空字符串
    js数组去重的三种方式的比较
    js数据类型之判断
    Bootstrap中的datetimepicker浅谈
  • 原文地址:https://www.cnblogs.com/lovesong/p/3574744.html
Copyright © 2011-2022 走看看