spring相关定时器

1、基于spring的配置文件


[html]  view plain  copy
  1. <beans xmlns="http://www.springframework.org/schema/beans"  
  2.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  3.     xmlns:p="http://www.springframework.org/schema/p"  
  4.     xmlns:task="http://www.springframework.org/schema/task"  
  5.     xmlns:context="http://www.springframework.org/schema/context"  
  6.     xmlns:aop="http://www.springframework.org/schema/aop"   
  7.     xsi:schemaLocation="http://www.springframework.org/schema/beans   
  8.     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
  9.     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd    
  10.     http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd    
  11.     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd    
  12.     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd    
  13.     http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">  
  14.   
  15.     <task:annotation-driven /> <!-- 定时器开关-->  
  16.   
  17.     <bean id="myTaskXml" class="com.spring.task.MyTaskXml"></bean>  
  18.   
  19.     <task:scheduled-tasks>  
  20.         <!--  
  21.             这里表示的是每隔五秒执行一次  
  22.         -->  
  23.         <task:scheduled ref="myTaskXml" method="show" cron="*/5 * * * * ?" />  
  24.         <task:scheduled ref="myTaskXml" method="print" cron="*/10 * * * * ?"/>  
  25.     </task:scheduled-tasks>  
  26.       
  27.     <!-- 自动扫描的包名 -->    
  28.     <context:component-scan base-package="com.spring.task" />  
  29.       
  30. </beans>  

2、基于xml的定时器任务

[java]  view plain  copy
  1. package com.spring.task;  
  2.   
  3. /** 
  4.  * 基于xml的定时器 
  5.  * @author hj 
  6.  */  
  7. public class MyTaskXml {  
  8.       
  9.       
  10.     public void show(){  
  11.         System.out.println("XMl:is show run");  
  12.     }  
  13.       
  14.     public void print(){  
  15.         System.out.println("XMl:print run");  
  16.     }  
  17. }  

3、基于注解的定时器任务

[java]  view plain  copy
  1. package com.spring.task;  
  2.   
  3. import org.springframework.scheduling.annotation.Scheduled;  
  4. import org.springframework.stereotype.Component;  
  5.   
  6. /** 
  7.  * 基于注解的定时器 
  8.  * @author hj 
  9.  */  
  10. @Component  
  11. public class MyTaskAnnotation {  
  12.       
  13.     /**  
  14.      * 定时计算。每天凌晨 01:00 执行一次  
  15.      */    
  16.     @Scheduled(cron = "0 0 1 * * *")   
  17.     public void show(){  
  18.         System.out.println("Annotation:is show run");  
  19.     }  
  20.       
  21.     /**  
  22.      * 心跳更新。启动时执行一次,之后每隔2秒执行一次  
  23.      */    
  24.     @Scheduled(fixedRate = 1000*2)   
  25.     public void print(){  
  26.         System.out.println("Annotation:print run");  
  27.     }  
  28. }  

4、测试

[java]  view plain  copy
  1. package com.spring.test;  
  2.   
  3. import org.springframework.context.ApplicationContext;  
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  5.   
  6.   
  7. public class Main {  
  8.     public static void main(String[] args) {  
  9.         ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-mvc.xml");  
  10.     }  
  11. }  
  1. 若上面的定时器没法应,可加上@Lazy(false)

    1. 二:基于注解方式

    使用注解方式不需要再每写一个任务类还要在xml文件中配置下,方便了很多。使用Spring的@Scheduled,下面先看一注解@Scheduled在源文件中的定义:

    @Target({java.lang.annotation.ElementType.METHOD, java.lang.annotation.ElementType.ANNOTATION_TYPE})  
    @Retention(RetentionPolicy.RUNTIME)  
    @Documented  
    public @interface Scheduled  
    {  
      public abstract String cron();  
    
      public abstract long fixedDelay();  
    
      public abstract long fixedRate();  
    }  
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    cron:表示指定cron表达式。(cron类型表示 是指定时间触发器触发任务执行!)

    • fixedDelay:表示从上一个任务完成开始到下一个任务开始的间隔,单位是毫秒。
    • fixedRate:表示从上一个任务开始到下一个任务开始的间隔,单位是毫秒。

    下面进行一下具体的配置过程:

    1:编写pojo类

    package com.tclshop.cms.center.web.task;
    
    import org.springframework.scheduling.annotation.Scheduled;
    import org.springframework.stereotype.Component;
    
    @Component
    public class WebTask {
    
        // 每五秒执行一次
        @Scheduled(cron = "0/5 * * * * ?")
        public void TaskJob() {
            System.out.println("test second annotation style ...");
        }
    }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    2:配置xml文件

    下面贴出相关的配置文件内容:

    <!-- 开启这个配置,spring才能识别@Scheduled注解   -->  
        <task:annotation-driven scheduler="qbScheduler" mode="proxy"/>  
        <task:scheduler id="qbScheduler" pool-size="10"/>
    • 1
    • 2
    • 3

    注:理论上只需要加上这句配置就可以了,其他参数都不是必须的。 
    配置完成,运行就能看到效果!

    总结:这种定时器的使用,不需要集成其他父类定时器,使用简单方便!功能也很强大!


    附:cronExpression的配置说明

    字段      允许值     允许的特殊字符
    秒       0-59        , - * /
    分       0-59        , - * /
    小时      0-23        , - * /
    日期      1-31        , - * ? / L W C
    月份      1-12 或者 JAN-DEC     , - * /
    星期      1-7 或者 SUN-SAT      , - * ? / L C #
    年(可选)       留空, 1970-2099       , - * /
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    例子:

    CRON表达式         含义 
    "0 0 12 * * ?"    每天中午十二点触发 
    "0 15 10 ? * *"    每天早上1015触发 
    "0 15 10 * * ?"    每天早上1015触发 
    "0 15 10 * * ? *"    每天早上1015触发 
    "0 15 10 * * ? 2005"    2005年的每天早上1015触发 
    "0 * 14 * * ?"    每天从下午2点开始到259分每分钟一次触发 
    "0 0/5 14 * * ?"    每天从下午2点开始到255分结束每5分钟一次触发 
    "0 0/5 14,18 * * ?"    每天的下午2点至2556点至655分两个时间段内每5分钟一次触发 
    "0 0-5 14 * * ?"    每天14:0014:05每分钟一次触发 
    "0 10,44 14 ? 3 WED"    三月的每周三的14101444触发 
    "0 15 10 ? * MON-FRI"    每个周一、周二、周三、周四、周五的1015触发 
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12


  1. 二:基于注解方式

猜你喜欢

转载自blog.csdn.net/Angle_wing_wh/article/details/78684344