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. ERROR- 开发常见error

    一,数据插入MySql中出现中文乱码 解决办法有: 1.新建数据库选择 create database 'GG' CHARACTER SET 'utf8 ' COLLATE 'utf8_general ...

  2. go语言学习之从例子开始

    [目录] go语言从例子开始之Example1.helloworld go语言从例子开始之Example2.类型 go语言从例子开始之Example3.变量 go语言从例子开始之Example4.常量 ...

  3. fastjson 1.1.1填坑

    java封装Echart数据模型 xAxis  yAxis 属性  转json赋值失败..换名即可 解决办法:升级1.2.2后即可

  4. ftp 上传文件时报 cant open output connection for file "ftp://129.28.149.240/shop/web/index.html". Reason: "550 Permission denied.".

    原因:没有写入权限 修改权限即可 vsftpd.conf vim /etc/vsftpd.conf write_enable=YES #加入这句

  5. Java double转long方法

    Java double转long方法 double random = Math.round(Math.random()*10000); long l = new Double(random).long ...

  6. Linux命令行工具之free命令

    原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/11524691.html 使用 free 查看整个系统的内存使用情况 Note:不同版本的free输出可 ...

  7. 【leetcode】623. Add One Row to Tree

    题目如下: Given the root of a binary tree, then value v and depth d, you need to add a row of nodes with ...

  8. Bugku | 数字验证正则绕过

    语法: int preg_match_all (字符串$ pattern ,字符串$ subject [,数组和$ matches [,整数$ flags = PREG_PATTERN_ORDER [ ...

  9. [信息学奥赛一本通oj1741]电子速度 题解

    对于$100 \%$的数据,$1≤n,m≤1e6 \ \ \ 0<=x_i,y_i<20170927 \ \ \ 1≤l_i,r_i≤n $ $Solution:$ 一开始没看懂题.后来大 ...

  10. oracle 远程访问

    oracle 本机能连外部访问不了 新装的 oracle 数据库经常会出现本地计算机能连接,但是局域网内的其他计算机不能连接的问题,如果出现此问题可以参考此文来解决. 本文中用的数据库版本为 Orac ...