自主开发的定时任务工具,spring task,可以将它比作一个轻量级的Quartz,而且使用起来很简单,除spring相关的包外不需要额外的包,而且支持注解和配置文件两种

  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
  3. xmlns:task="http://www.springframework.org/schema/task"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  6. http://www.springframework.org/schema/task
  7. http://www.springframework.org/schema/task/spring-task-3.0.xsd">
  8. <!--
  9. 这里加入了
  10. xmlns:task="http://www.springframework.org/schema/task"
  11. http://www.springframework.org/schema/task
  12. http://www.springframework.org/schema/task/spring-task-3.0.xsd
  13. -->
  14. <task:annotation-driven /> <!-- 定时器开关-->
  15. <bean id="taskTest" class="com.jungle.test.TaskTest"></bean>
  16. <task:scheduled-tasks>
  17. <!--
  18. 这里表示的是从第五秒开始 ,每三秒执行一次 (而不是 三分之五 秒执行一次哦~~)
  19. -->
  20. <task:scheduled ref="taskTest" method="say" cron="5/3 * * * * ?" />
  21. <task:scheduled ref="taskTest" method="hello" cron="5/3 * * * * ?"/>
  22. </task:scheduled-tasks>
  • import java.util.Date;
  • public class TaskTest {
  • public void say() {
  • System.out.println("这个真好用!!!" + new Date());
  • }
  • public void hello(){
  • System.out.println("hello!!!");
  • }
  • }

第二种:spring注解形式

也许我们不想每写一个任务类还要在xml文件中配置下,我们可以使用注解@Scheduled,我们看看源文件中该注解的定义:

Java代码  
  1. @Target({java.lang.annotation.ElementType.METHOD, java.lang.annotation.ElementType.ANNOTATION_TYPE})
  2. @Retention(RetentionPolicy.RUNTIME)
  3. @Documented
  4. public @interface Scheduled
  5. {
  6. public abstract String cron();
  7. public abstract long fixedDelay();
  8. public abstract long fixedRate();
  9. }
cron:指定cron表达式
fixedRate:官方文档解释:An interval-based trigger where the interval is
measured from the start time of the previous task. The time unit value
is measured in milliseconds.即从上一个任务开始到下一个任务开始的间隔,单位是毫秒。
第一步:编写pojo
  1. import org.springframework.scheduling.annotation.Scheduled;
  2. import org.springframework.stereotype.Component;
  3. @Component(“taskJob”)
  4. public class TaskJob {
  5. @Scheduled(cron = "0 0 3 * * ?")
  6. public void job1() {
  7. System.out.println(“任务进行中。。。”);
  8. }
  9. }
  1. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
  2. xmlns:context="http://www.springframework.org/schema/context"
  3. xmlns:tx="http://www.springframework.org/schema/tx"
  4. xmlns:task="http://www.springframework.org/schema/task"
  5. xsi:schemaLocation="
  6. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  7. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
  8. http://www.springframework.org/schema/context
  9. http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
  10. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
  11. http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd"
  12. default-lazy-init="false">
  13. <context:annotation-config />
  14. <!—spring扫描注解的配置   -->
  15. <context:component-scan base-package="com.gy.mytask" />
  16. <!—开启这个配置,spring才能识别@Scheduled注解   -->
  17. <task:annotation-driven scheduler="qbScheduler" mode="proxy"/>
  18. <task:scheduler id="qbScheduler" pool-size="10"/>
附录:
字段   允许值   允许的特殊字符
秒    0-59    , - * /
分    0-59    , - * /
小时    0-23    , - * /
日期    1-31    , - * ? / L W C
月份    1-12 或者 JAN-DEC    , - * /
星期    1-7 或者 SUN-SAT    , - * ? / L C #
年(可选)    留空, 1970-2099    , - * / 
- 区间  
* 通配符  
下面只例出几个式子
 
CRON表达式    含义 
"0 0 12 * * ?"    每天中午十二点触发 
"0 15 10 ? * *"    每天早上10:15触发 
"0 15 10 * * ?"    每天早上10:15触发 
"0 15 10 * * ? *"    每天早上10:15触发 
"0 15 10 * * ? 2005"    2005年的每天早上10:15触发 
"0 * 14 * * ?"    每天从下午2点开始到2点59分每分钟一次触发 
"0 0/5 14 * * ?"    每天从下午2点开始到2:55分结束每5分钟一次触发 
"0 0/5 14,18 * * ?"    每天的下午2点至2:55和6点至6点55分两个时间段内每5分钟一次触发 
"0 0-5 14 * * ?"    每天14:00至14:05每分钟一次触发 
"0 10,44 14 ? 3 WED"    三月的每周三的14:10和14:44触发 
"0 15 10 ? * MON-FRI"    每个周一、周二、周三、周四、周五的10:15触发 
 
 
原文:http://gong1208.iteye.com/blog/1773177

Spring 3.0以后版本的定时任务的更多相关文章

  1. SpringBoot之整合Redis分析和实现-基于Spring Boot2.0.2版本

    背景介绍 公司最近的新项目在进行技术框架升级,基于的Spring Boot的版本是2.0.2,整合Redis数据库.网上基于2.X版本的整个Redis少之又少,中间踩了不少坑,特此把整合过程记录,以供 ...

  2. Spring Boot2.0以上版本EmbeddedServletContainerCustomizer被WebServerFactoryCustomizer替代

    在Spring Boot2.0以上配置嵌入式Servlet容器时EmbeddedServletContainerCustomizer类不存在,经网络查询发现被WebServerFactoryCusto ...

  3. SpringBoot之整合Quartz调度框架-基于Spring Boot2.0.2版本

    1.项目基础 项目是基于Spring Boot2.x版本的 2.添加依赖 <!-- quartz依赖 --> <dependency> <groupId>org.s ...

  4. Spring Boot2.0 静态资源被拦截问题

    在Spring Boot2.0+的版本中,只要用户自定义了拦截器,则静态资源会被拦截.但是在spring1.0+的版本中,是不会拦截静态资源的. 因此,在使用Spring Boot2.0+时,配置拦截 ...

  5. spring boot 2.0.0由于版本不匹配导致的NoSuchMethodError问题解析

    spring boot升级到2.0.0以后,项目突然报出 NoSuchMethodError: org.springframework.boot.builder.SpringApplicationBu ...

  6. 0.9.0.RELEASE版本的spring cloud alibaba sentinel+gateway网关实例

    sentinel除了让服务提供方.消费方用之外,网关也能用它来限流.我们基于上次整的网关(参见0.9.0.RELEASE版本的spring cloud alibaba nacos+gateway网关实 ...

  7. 0.9.0.RELEASE版本的spring cloud alibaba nacos+gateway网关实例

    gateway就是用来替换zuul的,功能都差不多,我们看下它怎么来跟nacos一起玩.老套路,三板斧: 1.pom: <?xml version="1.0" encodin ...

  8. 0.9.0.RELEASE版本的spring cloud alibaba sentinel+feign降级处理实例

    既然用到了feign,那么主要是针对服务消费方的降级处理.我们基于0.9.0.RELEASE版本的spring cloud alibaba nacos+feign实例添油加醋,把sentinel功能加 ...

  9. 0.9.0.RELEASE版本的spring cloud alibaba sentinel限流、降级处理实例

    先看服务提供方的,我们在原来的sentinel实例(参见0.9.0.RELEASE版本的spring cloud alibaba sentinel实例)上加上限流.降级处理,三板斧只需在最后那一斧co ...

随机推荐

  1. 很励志的帖子,转来自勉,也反省一下自己写码这几年【奋斗10年,一个.NET程序员从0到拥有5系】

    http://bbs.csdn.net/topics/390833230 想想自己毕业近8年,真正写码也5年.从当初毕业时的拒绝写码,到迫不得已开始写码,是命运也好,是自己的不努力也罢.今天看来,写码 ...

  2. u-boot启动流程分析(1)_平台相关部分

    转自:http://www.wowotech.net/u-boot/boot_flow_1.html 1. 前言 本文将结合u-boot的“board—>machine—>arch—> ...

  3. python 深入理解 赋值、引用、拷贝、作用域

    在 python 中赋值语句总是建立对象的引用值,而不是复制对象.因此,python 变量更像是指针,而不是数据存储区域, 这点和大多数 OO 语言类似吧,比如 C++.java 等 ~ 1.先来看个 ...

  4. ScalaTour 2.函数

    /** * 1. case class与模式匹配 */ object TestFunction extends App{ def value(expr:Expr): Int = expr match ...

  5. oProfile 学习

    oProfile工具可以分析CPU的负载量 只要对目标程序加上 -g 后重新编译,即可用oProfile进行分析 例如在测试apache的性能时, 增加 -g 编译选项[crifan@localhos ...

  6. You need tcl 8.5 or newer in order to run the Redis test

    安装Redis时候make test出现的 , 不用下什么tcl8.5再tar了 , 直接yum install tcl就好.

  7. Bugtags,产品经理的瑞士军刀

    做为设计移动应用的产品经理,每天的主要工作就是在手机上不停的体验自己的产品,发现问题.优化体验.你是否经常工作在这样的尴尬场景: 发现界面问题,将问题界面截屏传到电脑,用图片标记工具将问题标记出来,然 ...

  8. restful api设计中的幂等性的理解。

    以前就听说过这个词,但是最近看了一些文章,产生了一些疑惑, GET是幂等性.例如查一条数据,如果你给定一个id.那么你查多少次.都是这条数据,但是我疑问,这里的幂等性指的是,每次都返回一条.这个数量, ...

  9. vim扩展配置

    在用户根目录下新建 “.vimrc”文件,键入一下代码: set syntax=on "高亮语法 set fenc=utf- "设定默认解码 set fencs=utf-,usc- ...

  10. Liferay中actionURL能够执行后台方法 ,但是页面不跳转问题解决方案

    在学习liferay的过程中,遇到了这么一个问题,actionURL能够执行后台方法 ,但是页面不跳转,以下是两种解决方案: 方案1(不推荐此种办法): 强制将页面重定向. 在actionURL执行的 ...