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,我们使用的时候主要是注重两个方面,一个是定时任 ...
随机推荐
- HDU 3685 Rotational Painting(多边形质心+凸包)(2010 Asia Hangzhou Regional Contest)
Problem Description Josh Lyman is a gifted painter. One of his great works is a glass painting. He c ...
- windows系统调用 线程创建
#include "windows.h" #include "iostream" using namespace std; class CWorkerThrea ...
- paper 77:[转载]ENDNOTE使用方法,常用!
一.简介 EndNote是一款用于海量文献管理和批量参考文献管理的工具软件,自问世起就成为科研界的必备武器.在前EndNote时代,文献复习阶段从各大数据库中搜集到的文献往往千头万绪.或重复或遗漏, ...
- paper 61:计算机视觉领域的一些牛人博客,超有实力的研究机构等的网站链接
转载出处:blog.csdn.net/carson2005 以下链接是本人整理的关于计算机视觉(ComputerVision, CV)相关领域的网站链接,其中有CV牛人的主页,CV研究小组的主页,CV ...
- XMLHttpRequest Level2实现跨域
Html5提供的XMLHttpRequest Level2已经实现的跨域访问以及一些新功能 1.ie10以下版本不支持 2.在服务器端做一些小改动即可: header("Access-Con ...
- sql 数据库查看主外键关联
SELECT 主键列ID=b.rkey ,主键列名=(SELECT name FROM syscolumns WHERE colid=b.rkey AND id=b.rkeyid) ,外键表ID=b. ...
- ORACLE CUP相关
遭遇cpu过多占用,表现为%usr很高,top 或者topas中cpu占用最多的进程为oracle server process. 则根据pid可以找出该pid对应的sql_text select s ...
- 搞不定linux下的无线网卡驱动的权宜之计
毕竟windows用了这么些年了,对windows下的一些东西也比较熟悉,还有就是windows的软件方式比较傻瓜. 在linux下搞不定无线网卡啊,幸亏有甲骨文的virtualbox,咱虚拟一个xp ...
- batch批的概念
批处理(Batch Requests), 批处理简单理解为同时执行的一批SQL处理语句,一个批处理中可能有多个DML.多个存储过程等等.如在SSMS操作,每个'GO'执行前都属于一个批处理. 注意区分 ...
- OpenGL笔试题
简述FrameBuffer,RenderBuffer,Depth Buffer,Framebuffer attachment,Stencil buffer的关系 简述利用OpenGL执行图像叠加(大P ...