Chinaunix首页 | 论坛 | 博客
  • 博客访问: 704088
  • 博文数量: 178
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1507
  • 用 户 组: 普通用户
  • 注册时间: 2014-04-27 23:20
文章分类

全部博文(178)

文章存档

2015年(58)

2014年(121)

我的朋友

分类: Java

2014-10-23 10:40:29

 

spring自带的定时任务功能,基于注解和xml配置

分类: spring 344人阅读 评论(3) 收藏 举报

1、spring的配置文件


[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <beans xmlns=""  
  2.     xmlns:xsi=""   
  3.     xmlns:p=""  
  4.     xmlns:task=""  
  5.     xmlns:context=""  
  6.     xmlns:aop=""   
  7.     xsi:schemaLocation="   
  8.     /spring-beans-3.0.xsd  
  9.     
  10.     
  11.      /spring-context-3.0.xsd    
  12.      /spring-aop-3.0.xsd    
  13.      /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.         <task:scheduled ref="myTaskXml" method="show" cron="*/5 * * * * ?" />  
  22.         <task:scheduled ref="myTaskXml" method="print" cron="*/10 * * * * ?"/>  
  23.     task:scheduled-tasks>  
  24.       
  25.         
  26.     <context:component-scan base-package="com.spring.task" />  
  27.       
  28. beans>  

2、基于xml的定时器任务



[java] view plaincopy在CODE上查看代码片派生到我的代码片
  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 plaincopy在CODE上查看代码片派生到我的代码片
  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 plaincopy在CODE上查看代码片派生到我的代码片
  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. }  
运行结果:
Annotation:print run
Annotation:print run
Annotation:print run
XMl:print run
XMl:is show run
Annotation:print run
Annotation:print run



工程下载地址:

阅读(1782) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~