spring 定时器配置

  1.  命名空间

    首先添加命名空间  在配置文件中applicationContext.xml



     
      
    [html]  view plain  copy
     print ? 在CODE上查看代码片 派生到我的代码片
    1. xmlns:task="http://www.springframework.org/schema/task"    
    2.  http://www.springframework.org/schema/task      
    3.   http://www.springframework.org/schema/task/spring-task-3.1.xsd     
  2.   task任务扫描注解
    [plain]  view plain  copy
     print ? 在CODE上查看代码片 派生到我的代码片
    1. <task:annotation-driven/>     
  3. <context:component-scan base-package="com.test"/> 

     

    [plain]  view plain  copy
     print ? 在CODE上查看代码片 派生到我的代码片
    1. 扫描位置  
    2.   
    3.  <context:component-scan base-package="com.core" />  

      
  4. 写自己的定时方法
    [java]  view plain  copy
     print ? 在CODE上查看代码片 派生到我的代码片
    1. <pre name="code" class="plain">@Service("taskService")  
    2. public class TaskServiceImpl implements TaskService{  
    3.     @Scheduled(cron="0/5 * *  * * ? ")   //每5秒执行一次  
    4.     public void myTest() {  
    5.          System.out.println("------"+System.currentTimeMillis());  
    6.     }  
    7. }  
     
      
    [java]  view plain  copy
     print ? 在CODE上查看代码片 派生到我的代码片
    1. <pre name="code" class="plain">public interface TaskService {  
    2.     public void myTest() ;  
    3. }  
     
    注意:定时器方法是不能有参数和返回值的Assert.isTrue(void.class.equals(method.getReturnType()),"Onlyvoid-returning methods may be annotated with @Scheduled.");Assert.isTrue(method.getParameterTypes().length == 0,"Only no-arg methods may be annotated with @Scheduled.");这是时间的设置规则 
    org.springframework.scheduling.quartz.CronTriggerBean允许你更精确地控制任务的运行时间,只需要设置其cronExpression属性。 
    一个cronExpression表达式有至少6个(也可能是7个)由空格分隔的时间元素。从左至右,这些元素的定义如下: 
    1.秒(0–59) 2.分钟(0–59) 3.小时(0–23) 
    4.月份中的日期(1–31) 5.月份(1–12或JAN–DEC) 
    6.星期中的日期(1–7或SUN–SAT) 7.年份(1970–2099) 0 0 10,14,16 * * ? 
    每天上午10点,下午2点和下午4点 
    0 0,15,30,45 * 1-10 * ? 
    每月前10天每隔15分钟 
    30 0 0 1 1 ? 2012 
    在2012年1月1日午夜过30秒时 
    0 0 8-5 ? * MON-FRI 
    每个工作日的工作时间 各个时间可用值如下:  
    秒0-59 , - * / 分0-59 , - * / 小时0-23 , - * / 
    日1-31 , - * ? / L W C 月1-12 or JAN-DEC , - * / 
    周几1-7 or SUN-SAT , - * ? / L C # 年(可选字段) empty, 1970-2099 , - * / 



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值