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

第一种:基于注解

1、spring.xml中对应位置加入
xmlns:task="http://www.springframework.org/schema/task"

http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.0.xsd
定时任务开关
<task:annotation-driven />
另外,注解扫描是必须的
<context:component-scan base-package="com._21cn.flowgate.*" />
2、任意一个测试类
@Component
public class MyTask { @Scheduled(cron = "0/3 * * * * ?")
public void myjob() {
System. out.println( "task execing ...");
}


第二种:基于xml配置
 
1、在上面基于注解的配置基础上加上
(bean路径指向定时任务执行类)
 <bean id ="taskTest" class= "com._21cn.flowgate.self.web.TaskJob" ></bean >
<task:scheduled-tasks >
<!--这里表示的是每隔5/10秒执行一次 -->
<task:scheduled ref ="taskTest" method="show" cron= "*/5 * * * * ?" />
<task:scheduled ref ="taskTest" method="print" cron= "*/10 * * * * ?"/>
</task:scheduled-tasks >
2、任意测试类
public class TaskJob {
void show(){
System. out.println( "5s 执行一次。。。。" );
} void print(){
System. out.println( "10s 执行一次。。。。" );
}
}

【注意】

测试有两种方式

1、启动已经配置好的sping项目

2、

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestMain {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
}
}

这种方式,我一直提示类找不到。

可以使用以下方式解决:

1.加上classpath:前缀
ApplicationContext ctx=new FileSystemXmlApplicationContext("classpath:applicationContext.xml";
2.加上file:把路径写全
ApplicationContext ctx=new ClassPathXmlApplicationContext("ApplicationContext ctx=new ClassPathXmlApplicationContext("file:F:/workspace/SpringExercis/src/applicationContext.xml");
如果仅仅是测试,最简单的方法还是把xml放在src下方便

【附】

关于cron表达式,如果不去理解?很容易被用错

1) 每一位表示的是 [ 秒 分 时 日 月 周 年]

2) The '?' character is allowed for the day-of-month and day-of-week fields. It is used to specify 'no specific value'. This is useful when you need to specify something in one of the two fields, but not the other.

注意着两个概念 day-of-month /day-of-week

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://my.oschina.net/u/559635/blog/389558

轻量级Spring定时任务(Spring-task)的更多相关文章

  1. spring 定时任务配置

    1.(易)如何在spring中配置定时任务? spring的定时任务配置分为三个步骤: 1.定义任务 2.任务执行策略配置 3.启动任务 (程序中一般我们都是到过写的,直观些) 1.定义任务 < ...

  2. 关于Spring定时任务(定时器)用法

    Spring定时任务的几种实现 Spring定时任务的几种实现 一.分类 从实现的技术上来分类,目前主要有三种技术(或者说有三种产品): 从作业类的继承方式来讲,可以分为两类: 从任务调度的触发时机来 ...

  3. (3)Spring定时任务的几种实现

    Spring定时任务的几种实现 近日项目开发中需要执行一些定时任务,比如需要在每天凌晨时候,分析一次前一天的日志信息,借此机会整理了一下定时任务的几种实现方式,由于项目采用spring框架,所以我都将 ...

  4. spring定时任务的几种实现方式

    Spring定时任务的几种实现 近日项目开发中需要执行一些定时任务,比如需要在每天凌晨时候,分析一次前一天的日志信息,借此机会整理了一下定时任务的几种实现方式,由于项目采用spring框架,所以我都将 ...

  5. (转)Spring定时任务的几种实现

    Spring定时任务的几种实现 博客分类: spring框架 quartzspringspring-task定时任务注解  Spring定时任务的几种实现 近日项目开发中需要执行一些定时任务,比如需要 ...

  6. spring定时任务的集中实现

    转载博主:感谢博主 http://gong1208.iteye.com/blog/1773177 Spring定时任务的几种实现 近日项目开发中需要执行一些定时任务,比如需要在每天凌晨时候,分析一次前 ...

  7. 摆脱Spring 定时任务的@Scheduled cron表达式的困扰

    一.背景 最近因为需要,需要适用Spring的task定时任务进行跑定时任务,以前也接触过,但是因为懒没有好好地理解@Scheduled的cron表达式,这次便对它做了一个全方位的了解和任务,记录下来 ...

  8. 【Spring】Spring的定时任务

    > 参考的优秀文章 Task Execution and Scheduling > 版本说明 <dependencies> <dependency> <gro ...

  9. Spring 定时任务2

    转载自http://www.cnblogs.com/nick-huang/p/4864737.html > 版本说明 <dependencies> <dependency> ...

随机推荐

  1. Codeforces Round #393 (Div. 2) - A

    题目链接:http://codeforces.com/contest/760/problem/A 题意:给定一个2017年的月份和该月的第一天的星期,问该月份的日历表中需要多少列.行有7列表示星期一~ ...

  2. sass-RGB颜色函数-RGBA()函数

    rgba() 函数主要用来将一个颜色根据透明度转换成 rgba 颜色. 其语法有两种格式: rgba($red,$green,$blue,$alpha) //将一个rgba颜色转译出来,和未转译的值一 ...

  3. mysql小知识

    char(10): 换行符 char(13): 回车符 UPDATE tablename SET field = REPLACE(REPLACE(field, CHAR(10), ”), CHAR(1 ...

  4. 2、pycharm中设置pytest为默认运行

    1.打开File-setting 2.打开Tools-Python Integrated Tools 3.找到Default test runner选项,在下拉框中选择py.test 4.点Apply ...

  5. boost IOStreams

    Boost.IOStreams provides numerous implementations of the two concepts. Devices which describes data ...

  6. Lock之ReentrantLock及实现生产者消费者和死锁

    Lock是顶层接口,它的实现逻辑并未用到synchronized,而是利用了volatile的可见性.ReentrantLock对了Lock接口的实现主要依赖了Sync,而Sync继承了 Abstra ...

  7. LDD3 第9章 与硬件通信

    一.I/O端口和I/O内存 每种外设都通过读写寄存器进行控制.大部分外设都有几个寄存器,不管是在内村地址空间还是在I/O地址空间,这些寄存器的访问地址都是连续的. 在硬件层,内存区域和I/O区域没有区 ...

  8. FS,FT,DFS,DTFT,DFT,FFT的联系和区别 数字信号处理

    DCT变换的原理及算法 文库介绍 对于初学数字信号处理(DSP)的人来说,这几种变换是最为头疼的,它们是数字信号处理的理论基础,贯穿整个信号的处理. 学习过<高等数学>和<信号与系统 ...

  9. C#_winform登陆框验证码的实现

    验证码技术已愈来愈成熟,从最初的数字.字母.字符.汉字已经到目前的语言,其应用也甚广,之前大多数只有在网站上可以看到,现在在一些客户端软件也经常可见(比如证券相关软件).之前做的一个基于 C# 客户端 ...

  10. yifan的数组

    yifan的数组 时间限制: 1 Sec  内存限制: 128 MB提交: 159  解决: 47[提交][状态] 题目描述 给你一个数组,初始值都是0,然后有N个操作,每次在一段区间L,R上加W,操 ...