ITeye博客

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

vicky_luo · 2015-05-26 14:36 · java基础 · 企业架构 · Markdown

  1. 第一步:配置spring.xml

  2. <beans

  3. 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">

  4. <context:annotation-config/>

  5. <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />

  6. <task:annotation-driven/>

  7. <!--扫描的是com.crm.web这样的包下的内容-->

  8. <context:component-scan base-package="com.crm.web" />

  9. 第二步:接口和实现

  10. package com.crm.web;

  11. import org.springframework.beans.factory.annotation.Autowired;

  12. import org.springframework.scheduling.annotation.Scheduled;

  13. import org.springframework.stereotype.Component;

  14. @Component

  15. public class MyTask {

  16. @Scheduled(cron="0 0 1 * * ?") //每天1点触发

  17. //  @Scheduled(cron="0/60 * * * * ? ") //间隔60秒执行

  18. public void taskCycle(){

  19. System.out.println("呵呵");

  20. }

  21. cron的取值参考:

  22. /**

  23. 秒 0-59 , - * /

  24. 分 0-59 , - * /

  25. 小时 0-23 , - * /

  26. 日期 1-31 , - * ? / L W C

  27. 月份 1-12 或者 JAN-DEC , - * /

  28. 星期 1-7 或者 SUN-SAT , - * ? / L C #

  29. 年(可选)留空, 1970-2099 , - * /

  30. 表达式意义

  31. "0 0 12 * * ?" 每天中午12点触发

  32. "0 15 10 ? * *" 每天上午10:15触发

  33. "0 15 10 * * ?" 每天上午10:15触发

  34. "0 15 10 * * ? *" 每天上午10:15触发

  35. "0 15 10 * * ? 2005" 2005年的每天上午10:15触发

  36. "0 * 14 * * ?" 在每天下午2点到下午2:59期间的每1分钟触发

  37. "0 0/5 14 * * ?" 在每天下午2点到下午2:55期间的每5分钟触发

  38. "0 0/5 14,18 * * ?" 在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发

  39. "0 0-5 14 * * ?" 在每天下午2点到下午2:05期间的每1分钟触发

  40. "0 10,44 14 ? 3 WED" 每年三月的星期三的下午2:10和2:44触发

  41. "0 15 10 ? * MON-FRI" 周一至周五的上午10:15触发

  42. "0 15 10 15 * ?" 每月15日上午10:15触发

  43. "0 15 10 L * ?" 每月最后一日的上午10:15触发

  44. "0 15 10 ? * 6L" 每月的最后一个星期五上午10:15触发

  45. "0 15 10 ? * 6L 2002-2005" 2002年至2005年的每月的最后一个星期五上午10:15触发

  46. "0 15 10 ? * 6#3" 每月的第三个星期五上午10:15触发

  47. * **/

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。