一般开发系统,使用定时任务非常常见。当然也可以用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. 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. 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实现定时任务的更多相关文章

  1. Spring 中使用Quartz实现任务调度

    前言:Spring中使用Quartz 有两种方式,一种是继承特定的基类:org.springframework.scheduling.quartz.QuartzJobBean,另一种则不需要,(推荐使 ...

  2. 浅谈Spring中的Quartz配置

    浅谈Spring中的Quartz配置 2009-06-26 14:04 樊凯 博客园 字号:T | T Quartz是一个强大的企业级任务调度框架,Spring中继承并简化了Quartz,下面就看看在 ...

  3. 10 -- 深入使用Spring -- 5...2 在Spring中使用Quartz

    10.5.2 在Spring中使用Quartz Spring 的任务调度抽象层简化了任务调度,在Quartz基础上提供了更好的调度抽象.本系统使用Quartz框架来完成任务调度,创建Quartz的作业 ...

  4. Spring中使用Quartz之MethodInvokingJobDetailFactoryBean配置任务

    Quartz是一个强大的企业级任务调度框架,Spring中继承并简化了Quartz. Spring中使用Quartz的3种方法(MethodInvokingJobDetailFactoryBean,i ...

  5. Spring Boot整合Quartz实现定时任务表配置

    最近有个小项目要做,spring mvc下的task设置一直不太灵活,因此在Spring Boot上想做到灵活的管理定时任务.需求就是,当项目启动的时候,如果有定时任务则加载进来,生成schedule ...

  6. 在spring中实现quartz的动态调度(开始、暂停、停止等)

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/fantasic_van/article/details/74942062 需求: 需要在页面设定某个 ...

  7. Spring Boot集成quartz实现定时任务并支持切换任务数据源

    org.quartz实现定时任务并自定义切换任务数据源 在工作中经常会需要使用到定时任务处理各种周期性的任务,org.quartz是处理此类定时任务的一个优秀框架.随着项目一点点推进,此时我们并不满足 ...

  8. 定时任务-在spring中配置quartz

    使用的版本Spring4.04+Quartz2.2.3,关于jar包自行下载. 详细需要以下几个步骤来完成: 1.  定义要执行的Job类 2.  定义quartz的配置文件applicationCo ...

  9. Spring中使用quartz插件实现定时任务

    第一步:导入架包 *spring3.2.3版本的架包将spring的各个功能模块给分开了,我们必须将Spring必须依赖的包导入上去 第二步:编写配置文件 <?xml version=" ...

随机推荐

  1. IOS开发学习 碎片S

    非常感谢提供一下内容的人和组织! 字符串编码:http://www.cnblogs.com/KevinYang/archive/2010/06/18/1760597.html Foundation框架 ...

  2. 业务、数据记录——ThreadPool.QueueUserWorkItem及Redis的实现

    业务描述 当用户执行完业务操作,或者数据操作后,讲业务记录/数据追踪插入到Redis中.ThreadPool.QueueUserWorkItem定时检查队列并将上述数据插入到数据库中持久化. 实现流程 ...

  3. Enigma模拟-Python

    设计思想 Enigma机的机械结构: 键盘:加密人员通过键盘进行输入 转子:Enigma机上一般装有至少3个转轮.每个转轮有代表26个字母的触头和触点,触点和触头在转轮内部有导线相连(一个转轮相当于一 ...

  4. 《OD学spark》20161022

    一.Spark Core 1. 什么是Spark Shuffle Wide Dependencies *ByKey: groupByKey,reduceByKey 关联操作:join,cogroup ...

  5. 40.QT-QPropertyAnimationdong和QParallelAnimationGroup动画实现

    简述:QPropertyAnimation (动画类,用来向QObject对象添加动画) 该类的继承框图如下所示: 1.QAbstractAnimation(所有动画的抽象基类) 该抽象类为QProp ...

  6. 当构造方法参数过多时使用builder模式

    静态工厂和构造方法都有一个限制:它们不能很好地扩展到很多可选参数的情景.请考虑一个代表包装食品上的营养成分标签的例子.这些标签有几个必需的属性——每次建议的摄入量,每罐的份量和每份卡路里 ,以及超过 ...

  7. CF987B High School: Become Human 数学

    题意翻译 题目大意 输入一个 xxx ,一个 yyy ,求是 xyx^yxy 大还是 yxy^xyx 大. (1≤x,y≤109)(1≤x,y≤10^9)(1≤x,y≤109) 输入输出格式 输入格式 ...

  8. Git练习2 使用码云

  9. JS对DOM节点操作整理

    获取节点: //按照ID获取 document.getElementById('element'); //按照节点名称获取,返回类数组对象 document.getElementsByTagName( ...

  10. F. Relatively Prime Powers (求([2,n],内不是次方的数量)

    题目:经过提炼后, 题目的意思就是问[2,n] 内,不是次方数的数量 ,: 思路: 答案就是 原理是利用容斥,注意n开i次根是向下取整(这题巨卡精度) 这是大神的思路 ,, 我还没有理解, 先放着,等 ...