spring 中使用quartz实现定时任务
一般开发系统,使用定时任务非常常见。当然也可以用Java实现。比如定时器。大致如下:
1: public static void main(String[] args) {
2: Timer timer = new Timer();
3: timer.schedule(new java.util.TimerTask() {
4: int flag = 1;
5:
6: @Override
7: public void run() {
8: System.out.println("print" + flag++);
9: }
10: }, 1000, 5000);
11: }
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
使用Timer类,缺点是对于任务执行的时间不是很好控制。而使用quartz则有强大的 cronExpression。方便定制时间。
使用spring 去quartz:
- 1. 在配置文件中配置quartz
1: <bean id="startQuertz"
2: class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
3: <property name="triggers">
4: <list>
5: <ref bean="testPrintTrigger" />
6: </list>
7: </property>
8: </bean>
9:
10: <!-- 触发类 -->
11: <bean id="testPrintTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
12: <property name="jobDetail">
13: <ref bean="printTask" />
14: </property>
15: <property name="cronExpression" value="30 * * * * ?" />
16: </bean>
17:
18: <!--工作工厂类-->
19: <bean id="printTask"
20: class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
21: <property name="targetObject">
22: <ref bean="printActivitor" />
23: </property>
24: <property name="targetMethod">
25: <value>excutePrint</value>
26: </property>
27: <property name="concurrent" value="false" />
28: </bean>
29:
30: <bean id="printActivitor" class="com.yingzi.springscheduledemo.task.PrintTast">
31:
32: </bean>
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; } - 2. 简单测试用的实现类:
1: public class PrintTast {
2:
3: private int flag = 0;
4:
5: public void excutePrint(){
6: System.out.println("spring-schedule task print" + (flag++));
7: }
8:
9: }
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
- 运行项目,输出如下:
1: spring-schedule task print1
2: spring-schedule task print1
3: spring-schedule task print2
4: spring-schedule task print2
5: spring-schedule task print3
6: spring-schedule task print3
7: spring-schedule task print4
8: spring-schedule task print4
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
比较奇怪的是居然打印是每次打印两遍一样的内容。经过调试发现,原因是spring的配置文件被载入两遍导致的。所以使用quartz的时候需要注意载入容器的次数。当然也可以在代码里面加入标志防止多次执行。
附上cronExpression 的表达式。转自网络,出处 http://gwh-08.iteye.com/blog/1601258
1.秒(0–59)
2.分钟(0–59)
3.小时(0–23)
4.月份中的日期(1–31)
5.月份(1–12或JAN–DEC)
6.星期中的日期(1–7或SUN–SAT)
7.年份(1970–2099)
秒 0-59 , - * /
分 0-59 , - * /
小时 0-23 , - * /
日期 1-31 , - * ? / L W C
月份 1-12 或者 JAN-DEC , - * /
星期 1-7 或者 SUN-SAT , - * ? / L C #
年(可选)留空, 1970-2099 , - * /
表达式意义
"0 0 12 * * ?" 每天中午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期间的每1分钟触发
"0 0/5 14 * * ?" 在每天下午2点到下午2:55期间的每5分钟触发
"0 0/5 14,18 * * ?" 在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发
"0 0-5 14 * * ?" 在每天下午2点到下午2:05期间的每1分钟触发
"0 10,44 14 ? 3 WED" 每年三月的星期三的下午2:10和2: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触发
每天早上6点
0 6 * * *
每两个小时
0 */2 * * *
晚上11点到早上7点之间每两个小时,早上八点
0 23-7/2,8 * * *
每个月的4号和每个礼拜的礼拜一到礼拜三的早上11点
0 11 4 * 1-3
1月1日早上4点
0 4 1 1 *
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
spring 中使用quartz实现定时任务的更多相关文章
- Spring 中使用Quartz实现任务调度
前言:Spring中使用Quartz 有两种方式,一种是继承特定的基类:org.springframework.scheduling.quartz.QuartzJobBean,另一种则不需要,(推荐使 ...
- 浅谈Spring中的Quartz配置
浅谈Spring中的Quartz配置 2009-06-26 14:04 樊凯 博客园 字号:T | T Quartz是一个强大的企业级任务调度框架,Spring中继承并简化了Quartz,下面就看看在 ...
- 10 -- 深入使用Spring -- 5...2 在Spring中使用Quartz
10.5.2 在Spring中使用Quartz Spring 的任务调度抽象层简化了任务调度,在Quartz基础上提供了更好的调度抽象.本系统使用Quartz框架来完成任务调度,创建Quartz的作业 ...
- Spring中使用Quartz之MethodInvokingJobDetailFactoryBean配置任务
Quartz是一个强大的企业级任务调度框架,Spring中继承并简化了Quartz. Spring中使用Quartz的3种方法(MethodInvokingJobDetailFactoryBean,i ...
- Spring Boot整合Quartz实现定时任务表配置
最近有个小项目要做,spring mvc下的task设置一直不太灵活,因此在Spring Boot上想做到灵活的管理定时任务.需求就是,当项目启动的时候,如果有定时任务则加载进来,生成schedule ...
- 在spring中实现quartz的动态调度(开始、暂停、停止等)
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/fantasic_van/article/details/74942062 需求: 需要在页面设定某个 ...
- Spring Boot集成quartz实现定时任务并支持切换任务数据源
org.quartz实现定时任务并自定义切换任务数据源 在工作中经常会需要使用到定时任务处理各种周期性的任务,org.quartz是处理此类定时任务的一个优秀框架.随着项目一点点推进,此时我们并不满足 ...
- 定时任务-在spring中配置quartz
使用的版本Spring4.04+Quartz2.2.3,关于jar包自行下载. 详细需要以下几个步骤来完成: 1. 定义要执行的Job类 2. 定义quartz的配置文件applicationCo ...
- Spring中使用quartz插件实现定时任务
第一步:导入架包 *spring3.2.3版本的架包将spring的各个功能模块给分开了,我们必须将Spring必须依赖的包导入上去 第二步:编写配置文件 <?xml version=" ...
随机推荐
- CodeForces 279C Ladder (RMQ + dp)
题意:给定一个序列,每次一个询问,问某个区间是不是先增再降的. 析:首先先取处理以 i 个数向左能延伸到哪个数,向右能到哪个数,然后每次用RQM来查找最大值,分别向两边延伸,是否是覆盖区间. 代码如下 ...
- CSS类名命名规则
CSS样式命名 说明 网页公共命名 #wrapper 页面外围控制整体布局宽度 #container或#content 容器,用于最外层 #layout 布局 #head, #header 页头部分 ...
- shell脚本知识点汇总
sed中在对内容进行修改时,有时候需要引用外部变量的值或者获取一个shell命令执行的结果,以便达到更加可观的输出结果 1.sed中使用变量替换1)sed命令使用双引号的情况下,使用$var直接引用[ ...
- 使用Realsense D400 camera系列跑rgbdslamv2
Ubuntu16.04,kinetic 在之前写的博文<如何使用ROS查找rgbdslam代码包框架的输入>中提到,一开始不知道整体框架,只用感性认识去跑rgbdslamv2的包,是一个天 ...
- 洛谷P3478 [POI2008]STA-Station
P3478 [POI2008]STA-Station 题目描述 The first stage of train system reform (that has been described in t ...
- Mysql-5-数据表的基本操作
1.创建表:之前需要use database database_name 然后create table 表名(): 例:创建员工表tb_employee1,结构如下表所示 字段名称 数据类型 备注 i ...
- luoguP4921 情侣?给我烧了!
luogu 考虑对于\(n\)对情侣,恰好\(k\)对是和谐的方案数是 \[ ans[n][k]=\binom{n}{k}A^k_n2^kg(n-k) \] \(g(n)\)为全部\(n\)对情侣不和 ...
- HTTP的学习记录(二)头部
本文主要讲一些 HTTP头部的信息 首先看一段 惊为天人 的文章. 来自于 <淘宝技术这十年> 你发现快要过年了,于是想给你的女朋友买一件毛衣,你打开了www.taobao.com.这时你 ...
- linux 文件权限除了r、w、x外还有s、t、i、a权限说明
linux 文件权限除了r.w.x外还有s.t.i.a权限 s: 文件属主和组设置SUID和GUID,文件在被设置了s权限后将以root身份执行.在设置s权限时文件属主.属组必须先设置相应的x权限,否 ...
- 关于MySQL AUDIT(审计)那点事
2017年06月02日MySQL社区版本最新版为MySQL_5.7.18,但是该版本不带AUDIT功能(MySQL Enterprise Edition自带AUDIT功能),因此需要加载plugin( ...