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=" ...
随机推荐
- product of大数据平台搭建------CM 和CDH安装
一.安装说明 CM是由cloudera公司提供的大数据组件自动部署和监控管理工具,相应的和CDH是cloudera公司在开源的hadoop社区版的基础上做了商业化的封装的大数据平台. 采用离线安装模式 ...
- remap——ROS中修改订阅的节点名称
跑数据集或者使用不同传感器时,难免会遇到需要修改topic名称的时候,此时可以有两种做法. 一.直接修改源码.如果有launch文件,则修改launch文件对应的topic 二.直接进行remap操作 ...
- BootStrap系统
BootStrsp的引入: <!-- 新 Bootstrap 核心 CSS 文件 --> <link rel="stylesheet" href="// ...
- rsyslog服务器同步其他服务器上面应用日志(如mysql审计日志 、nginx日志)
**环境说明**系统:ubuntu 14.04 (CentOS可以参考http://www.cnblogs.com/hanyifeng/p/5463338.html) rsyslog版本 :8.16. ...
- springboot和Druid整合配置数据源
@Configuration public class DruidConfiguration { @ConfigurationProperties(prefix = "spring.data ...
- BZOJ 2973 石头游戏 矩乘加速递推
FFFFFFF,看了一上午才看懂,又调了一中午.....我终于明白为何自己如此菜了qwq 这个题加速的思路是:因为每个序列的长度小于6,他们的lcm是60,所以六十次以后就会回到原来的序列. 加速的就 ...
- Eugeny and Array(水题,注意题目描述即可)
Eugeny has array a = a1, a2, ..., an, consisting of n integers. Each integer ai equals to -1, or to ...
- Subversion Server Edge的搭建与配置
1.Subversion Server Edge的搭建 当在操作系统为64位的配置服务器上部署时只能够选择Collabnet Subversion Edge,它集合了Subversion所需要一切资源 ...
- js——bootstrap框架
前端开发框架,移动优先,响应式布局开发. 网址:www.bootcss.com js有一个特性是阻塞加载,也就是说js文件如果没有加载完,不会进行后面的加载,所以例子中会把js文件写在body的最后一 ...
- LeetCode 179 Largest Number 把数组排成最大的数
Given a list of non negative integers, arrange them such that they form the largest number.For examp ...