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属性接受一组触发器。

一个具体的CronTriggerBean的例子

<!-- 自动确认维护量(除去存量) -->

<bean id="autoConfirmMaintain" class="com.geosoft.webtp.service.maintainaccount.AutoConfirmMaintain">

<property name="maintainDao" ref="MaintainDaoImpl"></property>

<property name="maintainCommDao" ref="MaintainCommDaoImpl"></property>

</bean>

<bean id="autoConfirmJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">

<property name="targetObject"><ref bean="autoConfirmMaintain"></ref></property>

<property name="targetMethod"><value>autoConfirmNC</value></property>

</bean>

<bean id="autoConfirmMTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">

<property name="jobDetail"><ref bean="autoConfirmJobDetail" /></property>

<property name="cronExpression"><value>00 00 00 1 * ?</value></property>

</bean>

两种流行Spring定时器配置:Java的Timer类和OpenSymphony的Quartz的更多相关文章

  1. reportConfig.xml两种数据源连接的配置方式

     在reportConfig.xml配置文件中,我们提供了两种数据源连接的配置方式,分别如下: 1.jndi数据源配置(即:在dataSource中配置) 此配置适用于在j2ee的服务器中配置了j ...

  2. spring定时器配置

    在此记录两种定时任务的配置: 一种是quart定时器: <1>配置xml文件(定时任务配置) <!--定时任务 --> <bean id="txfwBomc&q ...

  3. Spring定时器配置与运用,及Cron表达式的详解

    一:首先在spring的配置文件里配置一个定时器 <task:executor id="executor" pool-size="5" /> < ...

  4. spring boot:解决cors跨域问题的两种方法(spring boot 2.3.2)

    一,什么是CORS? 1,CORS(跨域资源共享)(CORS,Cross-origin resource sharing), 它是一个 W3C 标准中浏览器技术的规范, 它允许浏览器向非同一个域的服务 ...

  5. 求一个int型整数的两种递减数之和(java)--2015华为机试题

    题目描述: 给出一个整数(负数使用其绝对值),输出这个整数中的两种递减数(1.最大递减数:2.递减数中各位数之和最大的数)之和. 递减数:一个数字的递减数是指相邻的数位从大到小排列的数字,不包含相邻的 ...

  6. Android Studio的两种模式及签名配置

    我们使用Android Studio 运行我们的app,无非两种模式:debug和release模式. debug模式 debug模式使用一个默认的debug.keystore进行签名. 这个默认签名 ...

  7. 两种常用的全排列算法(java)

    问题:给出一个字符串,输出所有可能的排列. 全排列有多种算法,此处仅介绍常用的两种:字典序法和递归法. 1.字典序法: 如何计算字符串的下一个排列了?来考虑"926520"这个字符 ...

  8. CentOS 6 搭建SVN支持httpd和svnserve独立服务器两种模式 以及邮件配置

    Linux下SVN服务器同时支持Apache的http和svnserve独立服务器两种模式且使用相同的访问权限账号 服务器操作系统:CentOS 6.x 1.在服务器上安装配置SVN服务: 2.配置S ...

  9. SSH的两种登录方式以及配置

    前言 SSH简介 Secure Shell(SSH) 是由 IETF(The Internet Engineering Task Force) 制定的建立在应用层基础上的安全网络协议.它是专为远程登录 ...

随机推荐

  1. 浏览器 HTTP 协议缓存机制详解

    最近在准备优化日志请求时遇到了一些令人疑惑的问题,比如为什么响应头里出现了两个 cache control.为什么明明设置了 no cache 却还是发请求,为什么多次访问时有时请求里带了 etag, ...

  2. 今天第一次接触到typescript,看了第一个知识点就是变量的声明,来回忆回忆,做做笔记

    以前只用过JavaScript原生写网站特效,今天还是第一次听说typescript的,然后看了一下它的基本知识,感觉很像Java,真的太像了,但是又有不同点.很让我惊奇看到的第一个知识点就和以前不同 ...

  3. 苹果Xcode 证书生成、设置、应用完整图文教程

    Xcode 证书生成.设置.应用,与大家分享. 为了能够在iPhone或iPod Touch上运行iPhone应用程序,必须使用有效的数字证书签名.这个证书用于将您的开发者身份与在注册期间所提供的已确 ...

  4. 使用bat脚本添加JAVA_HOME和修改PATH

    add_jre.bat ::添加环境变量JAVA_HOME @echo off echo 添加java环境变量 set regpath=HKEY_LOCAL_MACHINE\SYSTEM\Curren ...

  5. 程序员写的东西出了bug,造成了损失谁来承担?

    这是个持续多年的话题了,很多大公司,尤其是牛逼的独立分包公司(开发公司)都会有代码审核和严格QA程序,一般的公司就很难说咯,在法律上目前还没有完全支持处罚程序员bug经济损失的判例(国内如此),国外也 ...

  6. Unity扩展编辑器学习笔记--从路径下找到拥有某个组件类型的预设

    public static List<T> GetAssetsWithScript<T>(string path) where T:MonoBehaviour { T tmp; ...

  7. delegate用法

    一般来说 delegate 可以申明一个delegate类型  比如 public delegate funa(object b) 然后使用的时候申明 funa 作为类型  new funa(回调函数 ...

  8. 【转载】Python 描述符简介

    来源:Alex Starostin 链接:www.ibm.com/developerworks/cn/opensource/os-pythondescriptors/ 关于Python@修饰符的文章可 ...

  9. Linux常用的安全工具 转自https://yq.aliyun.com/articles/52540?spm=5176.100239.blogcont24250.8.CfBYE9

    摘要: 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://chenguang.blog.51cto.com/350944/85790 ...

  10. python print 进度条的例子

    def progress(width, percent): print "%s %d%%\r" % (('%%-%ds' % width) % (width * percent / ...