`
vicky_luo
  • 浏览: 18923 次
  • 性别: Icon_minigender_2
  • 来自: 上海
社区版块
存档分类
最新评论

使用spring @Scheduled注解-执行定时任务

 
阅读更多

 

  1.   
  2. 第一步:配置spring.xml
  3. <beans
  4.   xmlns="http://www.springframework.org/schema/beans" 

        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

        xmlns:p="http://www.springframework.org/schema/p" 

        xmlns:context="http://www.springframework.org/schema/context"

        xmlns:mvc="http://www.springframework.org/schema/mvc"

        xmlns:task="http://www.springframework.org/schema/task"  

        xsi:schemaLocation="http://www.springframework.org/schema/beans

        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

        http://www.springframework.org/schema/aop        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd

        http://www.springframework.org/schema/tx  http://www.springframework.org/schema/tx/spring-tx-3.0.xsd

        http://www.springframework.org/schema/context 

        http://www.springframework.org/schema/context/spring-context-3.0.xsd

        http://www.springframework.org/schema/mvc 

        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd

        http://www.springframework.org/schema/task

        http://www.springframework.org/schema/task/spring-task-3.2.xsd">

  5. <context:annotation-config/>
  6. <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
  7. <task:annotation-driven/>
  8. <!--扫描的是com.crm.web这样的包下的内容-->
  9. <context:component-scan base-package="com.crm.web" />
  10. 第二步:接口和实现
  11. package com.crm.web;
  12. import org.springframework.beans.factory.annotation.Autowired;
  13. import org.springframework.scheduling.annotation.Scheduled;
  14. import org.springframework.stereotype.Component;
  15. @Component
  16. public class MyTask {
  17. @Scheduled(cron="0 0 1 * * ?") //每天1点触发
  18. //  @Scheduled(cron="0/60 * * * * ? ") //间隔60秒执行  
  19.  public void taskCycle(){ 
  20.  System.out.println("呵呵");
  21.  }
  22. cron的取值参考:
  23. /**
  24. 秒 0-59 , - * /
  25. 分 0-59 , - * /
  26. 小时 0-23 , - * /
  27. 日期 1-31 , - * ? / L W C
  28. 月份 1-12 或者 JAN-DEC , - * /
  29. 星期 1-7 或者 SUN-SAT , - * ? / L C #
  30. 年(可选)留空, 1970-2099 , - * /
  31. 表达式意义
  32. "0 0 12 * * ?" 每天中午12点触发
  33. "0 15 10 ? * *" 每天上午10:15触发
  34. "0 15 10 * * ?" 每天上午10:15触发
  35. "0 15 10 * * ? *" 每天上午10:15触发
  36. "0 15 10 * * ? 2005" 2005年的每天上午10:15触发
  37. "0 * 14 * * ?" 在每天下午2点到下午2:59期间的每1分钟触发
  38. "0 0/5 14 * * ?" 在每天下午2点到下午2:55期间的每5分钟触发
  39. "0 0/5 14,18 * * ?" 在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发
  40. "0 0-5 14 * * ?" 在每天下午2点到下午2:05期间的每1分钟触发
  41. "0 10,44 14 ? 3 WED" 每年三月的星期三的下午2:10和2:44触发
  42. "0 15 10 ? * MON-FRI" 周一至周五的上午10:15触发
  43. "0 15 10 15 * ?" 每月15日上午10:15触发
  44. "0 15 10 L * ?" 每月最后一日的上午10:15触发
  45. "0 15 10 ? * 6L" 每月的最后一个星期五上午10:15触发
  46. "0 15 10 ? * 6L 2002-2005" 2002年至2005年的每月的最后一个星期五上午10:15触发
  47. "0 15 10 ? * 6#3" 每月的第三个星期五上午10:15触发
  48. * **/
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics