有两种流行Spring定时器配置:Java的Timer类和OpenSymphony的Quartz。

  1.Java Timer定时

  首先继承java.util.TimerTask类实现run方法

import java.util.TimerTask;     public class EmailReportTask extends TimerTask{         @Override         public void run() {             ...         }       }  

  在Spring定义

  ...

  配置Spring定时器

<bean id="scheduleReportTask" class="org.springframework.scheduling.timer.ScheduledTimerTask">     <property name="timerTask" ref="reportTimerTask" />     <property name="period">     <value>86400000value>     property>     bean>

  timerTask属性告诉ScheduledTimerTask运行哪个。86400000代表24个小时

  启动Spring定时器

  Spring的TimerFactoryBean负责启动定时任务

<bean class="org.springframework.scheduling.timer.TimerFactoryBean">     <property name="scheduledTimerTasks">        <list><ref bean="scheduleReportTask"/>list>     property>     bean>     scheduledTimerTasks里显示一个需要启动的定时器任务的列表。     可以通过设置delay属性延迟启动     <bean id="scheduleReportTask" class="org.springframework.scheduling.timer.ScheduledTimerTask">     <property name="timerTask" ref="reportTimerTask" />     <property name="period">     <value>86400000value>     property>     <property name="delay">     <value>3600000value>     property>     bean>

  这个任务我们只能规定每隔24小时运行一次,无法精确到某时启动

  2.Quartz定时器

  首先继承QuartzJobBean类实现executeInternal方法

import org.quartz.JobExecutionContext;     import org.quartz.JobExecutionException;     import org.springframework.scheduling.quartz.QuartzJobBean;       public class EmailReportJob extends QuartzJobBean{     protected void executeInternal(JobExecutionContext arg0)     throws JobExecutionException {     ...     }     } 

  在Spring中定义

<bean id="reportJob" class="org.springframework.scheduling.quartz.JobDetailBean">     <property name="jobClass">     <value>EmailReportJobvalue>     property>     <property name="jobDataAsMap">         <map>             <entry key="courseService">                 <ref bean="courseService"/>                 entry>         map>     property>     bean> 

  在这里我们并没有直接声明一个EmailReportJob Bean,而是声明了一个JobDetailBean。这个是Quartz的特点。JobDetailBean是Quartz的org.quartz.JobDetail的子类,它要求通过jobClass属性来设置一个Job对象。

  使用Quartz的JobDetail中的另一个特别之处是EmailReportJob的courseService属性是间接设置的。JobDetail的jobDataAsMap属性接受一个Map,包括设置给jobClass的各种属性,当。JobDetailBean实例化时,它会将courseService Bean注入到EmailReportJob 的courseService 属性中。

  启动定时器

  Quartz的org.quartz.Trigger类描述了何时及以怎样的频度运行一个Quartz工作。Spring提供了两个触发器SimpleTriggerBean和CronTriggerBean。

  SimpleTriggerBean与scheduledTimerTasks类似。指定工作的执行频度,模仿scheduledTimerTasks配置 .

<bean id="simpleReportTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">     <property name="jobDetail" ref="reprotJob" />     <property name="startDelay">     <value>360000value>     property>     <property name="repeatInterval">         <value>86400000value>     property>     bean>  

  startDelay也是延迟1个小时启动

  CronTriggerBean指定工作的准确运行时间

<bean id="cronReportTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">     <property name="jobDetail" ref="reprotJob" />     <property name="cronExpression">     <value>0 0 6 * * ?value>     property>     bean>

  属性cronExpression告诉何时触发。最神秘就是cron表达式:

  Linux系统的计划任务通常有cron来承担。一个cron表达式有至少6个(也可能7个)有空格分隔的时间元素。从左到右:

  1.秒2.分3.小时4.月份中的日期(1-31)5.月份(1-12或JAN-DEC)6.星期中的日期(1-7或SUN-SAT)7.年份(1970-2099)

  每个元素都显示的规定一个值(如6),一个区间(9-12),一个列表(9,11,13)或一个通配符(*)。因为4和6这两个元素是互斥的,因此应该通过设置一个问号(?)来表明不想设置的那个字段,“/”如果值组合就表示重复次数(10/6表示每10秒重复6次)。

  启动定时器

<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">         <property name="triggers">            <list><ref bean="cronReportTrigger"/>list>         property>     bean>

  triggers属性接受一组触发器。

Spring定时器的两种实现方式的更多相关文章

  1. (一)spring aop的两种配置方式。

    sring aop的方式有两种:(1)xml文件配置方式(2)注解的方式实现,我们可以先通过一个demo认识spring aop的实现,然后再对其进行详细的解释. 一.基于注解的springAop配置 ...

  2. spring AOP的两种配置方式

    连接点(JoinPoint) ,就是spring允许你是通知(Advice)的地方,那可就真多了,基本每个方法的前.后(两者都有也行),或抛出异常是时都可以是连接点,spring只支持方法连接点.其他 ...

  3. Spring与Hibernate两种组合方式

    第一种 1.hibernate.cfg.xml文件 xml version='1.0' encoding='utf-8'?> "-//Hibernate/Hibernate Confi ...

  4. Spring常见的两种增强方式

    一.编程式增强 不借助spring的配置,通过自己实例化对象来实现的增强方式 创建增强类,需要实现你需要的增强接口,(只有实现了该接口,这个类就是一个通知)) /** * 增强类 */ public ...

  5. spring ----> aop的两种实现方式

    实现1:基于xml package com.rr.spring3.interf; //接口 public interface SayHello { public void sayHello(); } ...

  6. 实例化Spring容器的两种常用方式

    //在类路径下寻找配置文件来实例化容器 ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[]{"b ...

  7. struts2+spring的两种整合方式

    也许有些人会因为学习了struts1,会以为struts2.struts1与spring的整合也是一样的,其实这两者相差甚远.下面就来讲解一下struts2与spring的整合两种方案.(部分转载,里 ...

  8. Spring的两种代理方式:JDK动态代理和CGLIB动态代理

    代理模式 代理模式的英文叫做Proxy或Surrogate,中文都可译为”代理“,所谓代理,就是一个人或者一个机构代表另一个人或者另一个机构采取行动.在一些情况下,一个客户不想或者不能够直接引用一个对 ...

  9. Spring 注入的两种方式

    Spring 的两种注入方式: 1. 属性注入:通过无参构造函数+setter方法注入 2. 构造注入:通过有参的构造函数注入. 优缺点: 1. 属性注入直白易懂,缺点是对于属性可选的时候,很多个构造 ...

随机推荐

  1. nginx动态配置及服务发现那些事

    Reference: http://xiaorui.cc/2016/10/16/nginx%E5%8A%A8%E6%80%81%E9%85%8D%E7%BD%AE%E5%8F%8A%E6%9C%8D% ...

  2. Apache重写RewriteCond

    RewriteCond就像我们程序中的if语句一样,表示如果符合某个或某几个条件则执行RewriteCond下面紧邻的RewriteRule语句,这就是RewriteCond最原始.基础的功能,为了方 ...

  3. browser_autopwn2

    browser_autopwn2是个啥玩意儿? 一个集合了浏览器漏洞的小框架 option msf > use auxiliary/server/browser_autopwn2 msf aux ...

  4. Json转list,二层解析转换

    一层结构的数据: { "code": "0", "results": { "boyTotal": 0, "cl ...

  5. 【Java】使用BigDecimal类进行精确小数计算

    在商业计算中(尤其是计算价格)需要使用BigDecimal类来进行精确小数计算,因为用其他类型计算(如double)得到的结果不是精确的! 写个测试类. import org.junit.Test; ...

  6. 【Unity Shader】UnityCG.cginc中一些常用的函数

    // 摄像机方向(视角方向) float3 WorldSpaceViewDir(float4 v) // 根据模型空间中的顶点坐标 得到 (世界空间)从这个点到摄像机的观察方向 float3 Unit ...

  7. 【转】26张PPT让你告别拖延症

    不论你如何富有,你都赚不到更多的时间,你也回不到过去.没有那么多的假如,只有指针滴答的时光飞逝和你应该好好把握的现在. 可能的话,请仔细读读PPT原件而不要只是看翻译吧. 1.时间常有,时间优先. 2 ...

  8. 阿里云免费SSL证书绑定+sever2012 IIS配置

    1.阿里云域名 2.点击证书 3.免费证书 4.下载证书 5.服务器-运行-mmc 进入制台程序 6.制台程序,选择菜单“文件"中的"添加/删除管理单元”-> “添加”,从“ ...

  9. Bootstrap 里的 popover 被挡住的解决方案

    在Bootstarp 中我们可以使用 popover 插件做一些内容的展示, 代码如下: <a data-toggle="popover" data-placement=&q ...

  10. 通过 Spark R 操作 Hive

    作为数据工程师,我日常用的主力语言是R,HiveQL,Java与Scala.R是非常适合做数据清洗的脚本语言,并且有非常好用的服务端IDE——RStudio Server:而用户日志主要储存在hive ...