zoukankan      html  css  js  c++  java
  • java定时器2-spring实现

    • spring定时器(基于xml)
    • spring定时器(基于注解)
    • quartz定时器
    1、使用基于xml配置的spring定时器
    首先编写定时任务类Mytask
    public class Mytask {  
      public void execute(){  
      System.out.println("基于xml配置的spring定时任务!");  
     }  
    }  
    接着在spring配置文件中加入xmlns:task="http://www.springframework.org/schema/task"命名空间
     
    <task:annotation-driven/>  
    <bean id="mytask" class="com.pptv.vipbackend.controller.Mytask"/>  
    <task:scheduled-tasks>  
    <task:scheduled ref="mytask" cron="*/5 * * * * ?" method="print"/><!--每隔5秒执行一次-->  
    </task:scheduled-tasks> 

    当然,定时任务类要加入spring的管理中

    <context:component-scan base-package="com.simonsfan.study.controller.Mytask">  
    执行便可看到效果
     
    2、使用基于注解配置的spring定时器
    基于注解会相对简单的多,直接编写任务类Mytask:
    @EnableScheduling  
    @Component  
    public class Mytask {  
      @Scheduled(cron = "*/5 * * * * ?")  
      public void execute(){  
        System.out.println("基于注解配置的spring定时任务!");  
      }  
    }  

    当然,任务类也要纳入spring管理

    <context:component-scan base-package="com.simonsfan.study.controller.Mytask">  
    <task:annotation-driven/> 
    启动可以看到相同效果
     
    3、quartz定时器,其性能和灵活性都优于jdk的TimerTask类
    pom文件中加入
    <dependency>  
    <groupId>org.quartz-scheduler</groupId>  
    <artifactId>quartz</artifactId>  
    <version>2.2.2</version>  
    </dependency> 

    定时任务类Mytask:

    @Component  
    public class Mytask {  
      public void execute() {  
        System.out.println("基于spring+quartz实现定时任务!");;  
      }  
    }  

    spring配置文件中加入:

    <bean id="jobBean" class="com.pptv.vipbackend.controller.Mytask"/>  
      
    <bean id="myJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">  
    <property name="targetObject">  
    <ref bean="jobBean"/>  
    </property>  
    <property name="targetMethod">  
    <value>execute</value><!--任务类中的方法名-->  
    </property>  
    <!--将并发设置为false-->  
    <property name="concurrent" value="false" />  
    </bean>  
    <bean id="simpleTrigger"  
    class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean">  
    <property name="jobDetail" ref="myJobDetail" />  
    <property name="startDelay" value="1000" />  
    <property name="repeatInterval" value="2000" />  
    </bean>  
      
    <!-- 总管理类如果将lazy-init='false'那么容器启动就会执行调度程序 -->  
    <bean id="startQuertz" class="org.springframework.scheduling.quartz.SchedulerFactoryBean" lazy-init="false" autowire="no">  
    <property name="triggers">  
    <list>  
    <!--作业调度器,list下可加入其他的调度器-->  
    <ref bean="simpleTrigger" />  
    </list>  
    </property>  
    </bean>  
    这样,基于spring + quartz便可实现定时任务的调度
     
    定时任务中的时间表达式:
    顺序: 分钟 小时 日期 月份 星期 年(可选)
    取值: 0-59 0-59 0-23 1-30(31) 1-12 1-7  
    允许特殊字符: , - * / , - * / , - * / , - * / ? L W C , - * / , - * / L # C 1970-2099 , - * /

    字段含义

    *:代表所有可能的值  
    -:指定范围  
    ,:列出枚举  例如在分钟里,"5,15"表示5分钟和20分钟触发  
    /:指定增量  例如在分钟里,"3/15"表示从3分钟开始,没隔15分钟执行一次  
    ?:表示没有具体的值,使用?要注意冲突  
    L:表示last,例如星期中表示7或SAT,月份中表示最后一天31或30,6L表示这个月倒数第6天,FRIL表示这个月的最后一个星期五  
    W:只能用在月份中,表示最接近指定天的工作日  
    #:只能用在星期中,表示这个月的第几个周几,例如6#3表示这个月的第3个周五  
      
    0 * * * * ? 每1分钟触发一次  
    0 0 * * * ? 每天每1小时触发一次  
    0 0 10 * * ? 每天10点触发一次  
    0 * 14 * * ? 在每天下午2点到下午2:59期间的每1分钟触发   
    0 30 9 1 * ? 每月1号上午9点半  
    0 15 10 15 * ? 每月15日上午10:15触发  
    */5 * * * * ? 每隔5秒执行一次  
    0 */1 * * * ? 每隔1分钟执行一次  
    0 0 5-15 * * ? 每天5-15点整点触发  
    0 0/3 * * * ? 每三分钟触发一次  
  • 相关阅读:
    leetcode Convert Sorted List to Binary Search Tree
    leetcode Convert Sorted Array to Binary Search Tree
    leetcode Binary Tree Level Order Traversal II
    leetcode Construct Binary Tree from Preorder and Inorder Traversal
    leetcode[105] Construct Binary Tree from Inorder and Postorder Traversal
    证明中序遍历O(n)
    leetcode Maximum Depth of Binary Tree
    限制 button 在 3 秒内不可重复点击
    HTML 和 CSS 画三角形和画多边行基本原理及实践
    在线前端 JS 或 HTML 或 CSS 编写 Demo 处 JSbin 与 jsFiddle 比较
  • 原文地址:https://www.cnblogs.com/chenglc/p/8539085.html
Copyright © 2011-2022 走看看