Spring-task-timer定时器
· spring定时控制器配置文件实现方式
一. 编写一个正常的业务类
public class SyncDataTaskTimer {
private final static Logger log = Logger.getLogger(SyncDataTaskTimer.class);
/**
* 同步组织
*/
public void syncOrg(){
log.info("同步组织:"+System.currentTimeMillis());
}
/**
* 同步用户
*/
public void syncUser(){
log.info("同步用户:"+System.currentTimeMillis());
}
}
二. applicationContext-task.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
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/task
http://www.springframework.org/schema/task/spring-task-3.0.xsd"> <!-- spring定时任务配置项 -->
<bean id="scheduledTask" class="org.springframework.scheduling.timer.ScheduledTimerTask"/> <!-- 同步ipm数据-定时任务配置 -->
<bean id="syncDataTaskTimer" class="cn.chinaunicom.pmis.interfaces.ipm.server.task.SyncDataTaskTimer"/> <!-- 任务时间项配置 -->
<task:scheduled-tasks>
<task:scheduled ref="syncDataTaskTimer" method="syncOrg" cron="0 30 23 * * ?"/>
<task:scheduled ref="syncDataTaskTimer" method="syncUser" cron="0 40 23 * * ?"/>
</task:scheduled-tasks>
三. applicationContext.xml 引用
<!-- 支持自带定时任务配置 -->
<import resource="applicationContext-task.xml" />
注:这里把spring的配置文件进行了拆分。
----------------------------------------分割线-----------------------------------------
· 难点解析
一. cron语法
<task:scheduled ref="syncDataTaskTimer" method="syncOrg" cron="0 30 23 * * ?"/>
cron="0 30 23 * * ?"意思为每天的23点30分执行一次。
cron语法使用和字符解释
cron的使用语法:
| (Seconds) | (Minutes) | (Hours) | (DayofMonth) | (Month) | (DayofWeek) | [Year] |
| 秒 | 分 | 时 | 日 | 月 | 周 | [年] |
cron的字符意义:
① * : 表示在当前区域内匹配任意值,列如如果在秒上,则表示每秒都出发该事件
② ? : 表示任意值,使用在dayofmonth和dayofweek上
③ / : 使用方式5/15,假如写在秒所在区域上,左边是开始时间,右边是在隔15秒后执行,所以执行频率是, 5秒执行一次,20秒执行一次,35,50
④ - : 表示一个区间,如果在分钟区域1-5,其时间内每分钟执行一次
⑤ , : 枚举值,在分钟位置上5,15,31,代表这5分,15分,31分各执行一次
⑥ # : 2#3,每个月第三个周的星期一(1代表日,2代表星期一)
⑦ L : 最后的意思,使用在dayofmonth和dayofweek上,如放在dayofweek=6L,意思是星期五触发。
⑧ W :正常工作日周一到周五
⑨ LW:每个月最后的一个周五
在网上摘抄一些例子,基本上都包含了,
| “0 0 12 * * ?” | 每天12:00触发事件 |
| “0 15 10 ? * *” | 每天10:15触发事件 |
| “0 15 10 * * ?” | 每天10:15触发事件 |
| “0 15 10 * * ? *” | 每天10:15触发事件 |
| “0 15 10 * * ? 2005″ | 2005年的每天10:15触发事件 |
| “0 * 14 * * ?” | 每天14点开始触发,每分钟触发一次,14:59分结束 |
| “0 0/5 14 * * ?” | 每天14点开始触发到14:59分结束的每5分钟触发一次事件 |
| “0 0/5 14,18 * * ?” | 每天14点开始到14:59期间和18点到18:59期间的每5分钟触发一次事件 |
| “0 0-5 14 * * ?” | 每天14点到14:05期间的每1分钟触发一次事件 |
| “0 10,44 14 ? 3 WED” | 每年3月的星期三的14:10和14:44触发一次事件 |
| “0 15 10 ? * MON-FRI” | 周一至周五的10:15触发一次事件 |
| “0 15 10 15 * ?” | 每月15日10:15触发一次事件 |
| “0 15 10 L * ?” | 每月最后一日的10:15触发一次事件 |
| “0 15 10 ? * 6L” | 每月的最后一个星期五10:15触发一次事件 |
| “0 15 10 ? * 6L 2002-2005″ | 2002年至2005年的每月的最后一个星期五10:15触发一次事件 |
| “0 15 10 ? * 6#3″ | 每月的第三个星期五10:15触发一次事件 |
Spring-task-timer定时器的更多相关文章
- [Java定时器]用Spring Task实现一个简单的定时器.
今天做一个项目的的时候需要用到定时器功能.具体需求是: 每个月一号触发一次某个类中的方法去拉取别人的接口获取上一个月份车险过期的用户.如若转载请附上原文链接:http://www.cnblogs.co ...
- java Quartz定时器任务与Spring task定时的几种实现,
java Quartz定时器任务与Spring task定时的几种实现 基于java 的定时任务实现, Quartz 时间详细配置 请查阅 http://www.cnblogs.com/si ...
- 任务调度的方式:Timer、ScheduledExecutorService、spring task、quartz、XXL-JOB、Elastic-Job
任务调度 定时任务调度:基于给定的时间点.给定的时间间隔.给定的执行次数自动执行的任务. Timer 介绍 Timer,简单无门槛,一般也没人用. Timer位于java.util包下,其内部包含且仅 ...
- java定时任务实现的几种方式(Timer、Spring Task、Quartz)
Timer JDK自带的Timer类,允许调度一个TimerTask任务. Demo: /** * Timer测试类 */ public class TimerDemo { public static ...
- spring task定时器的配置使用
spring task的配置方式有两种:配置文件配置和注解配置. 1.配置文件配置 在applicationContext.xml中增加spring task的命名空间: xmlns:task=&qu ...
- 任务调度 Spring Task 4(一)
深入浅出spring task定时任务 在工作中有用到spring task作为定时任务的处理,spring通过接口TaskExecutor和TaskScheduler这两个接口的方式为异步定时任务提 ...
- spring task 配置
Spring对Quartz作了一个封装,同时,Spring自己也提供了一个任务定时器(spring-task),现把它总结一下. 对于Quartz,我们使用的时候主要是注重两个方面,一个是定时任 ...
- Quartz Spring与Spring Task总结
Spring对Quartz作了一个封装,同时,Spring自己也提供了一个任务定时器(spring-task),现把它总结一下. 对于Quartz,我们使用的时候主要是注重两个方面,一个是定时任 ...
- 关于Spring定时任务(定时器)用法
Spring定时任务的几种实现 Spring定时任务的几种实现 一.分类 从实现的技术上来分类,目前主要有三种技术(或者说有三种产品): 从作业类的继承方式来讲,可以分为两类: 从任务调度的触发时机来 ...
- uartz Spring与Spring Task总结
Spring对Quartz作了一个封装,同时,Spring自己也提供了一个任务定时器(spring-task),现把它总结一下. 对于Quartz,我们使用的时候主要是注重两个方面,一个是定时任 ...
随机推荐
- paper 76:膨胀、腐蚀、开、闭运算——数字图像处理中的形态学
膨胀.腐蚀.开.闭运算是数学形态学最基本的变换.本文主要针对二值图像的形态学膨胀:把二值图像各1像素连接成分的边界扩大一层(填充边缘或0像素内部的孔):腐蚀:把二值图像各1像素连接成分的边界点去掉从而 ...
- ubuntu的目录结构
/:根目录,一般根目录下只存放目录,不要存放文件,/etc./bin./dev./lib./sbin应该和根目录放置在一个分区中 /bin:/usr/bin:可执行二进制文件的目录,如常用的命令ls. ...
- UITableView(转)
一.UITableView概述 UITableView继承自UIScrollView,可以表现为Plain和Grouped两种风格,分别如下图所示: 其中左边的是Plain风格的,右 ...
- js正则函数match、exec、test、search、replace、split使用介绍集合
match 方法 使用正则表达式模式对字符串执行查找,并将包含查找的结果作为数组返回. stringObj.match(rgExp) 参数 stringObj 必选项.对其进行查找的 String 对 ...
- 精简高效的CSS命名准则/方法
/* ---------------------single CSS----------------------- */ /* display */ .dn{display:none;} .di{di ...
- 【python cookbook】【字符串与文本】3.利用shell通配符做字符串匹配
问题:当工作在Linux shell下时,使用常见的通配符模式(即,*.py.Dat[0-9]*.csv等)来对文本做匹配 解决方案:fnmatch模块提供的两个函数fnmatch().fnmatch ...
- python paramiko ssh.exec_command()启动tomcat服务器应用进程失败问题解决方法- Neither the JAVA_HOME nor the JRE_HOME environment variable is defined At least one of these environment variable is needed to run this progr
问题说明:
- laravel事务小例子
发生异常则自动回滚,正常则自动提交,示例如下: DB::connection('vshare')->transaction(function() use($id,$reason,$refuser ...
- 【Pro ASP.NET MVC 3 Framework】.学习笔记.5.SportsStore一个真实的程序
我们要建造的程序不是一个浅显的例子.我们要创建一个坚固的,现实的程序,坚持使它成为最佳实践.与Web Form中拖控件不同.一开始投入MVC程序付出利息,它给我们可维护的,可扩展的,有单元测试卓越支持 ...
- 【Pro ASP.NET MVC 3 Framework】.学习笔记.1.主要语言特性
C# 是一个富有特性的语言,并不是所有的程序员都熟悉本书依赖的所有特性.在本章,我们看看作为一个好的MVC程序员需要知道的C#特性. 1 C#主要特性 } 1.1 使用扩展方法 扩展方法 在你不能拥有 ...