1,   内容简介

所谓的定时调度,是指在无人值守的时候系统可以在某一时刻执行某些特定的功能采用的一种机制,对于传统的开发而言,定时调度的操作分为两种形式:

定时触发:到某一时间点上执行某些处理操作;

间隔触发:每隔几秒后进行某些操作的自动处理。

所有的处理都依赖于计算机系统底层的时钟发生器,在java最初的实现过程里面,真对于定时处理专门提供有两个类:Timer,TimerTask两个类,其中TimerTask主要是定义任务的执行,相当于启动一个线程去执行某些任务。

public class MyTask extends TimerTask{

@Override

public void run() {//定义要执行的任务

// TODO Auto-generated method stub

String currentTime=new  SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date());

System.out.println(currentTime);

}

}

public class MyTaskTest {

public static void main(String[] args) {

Timer timer=new Timer();

timer.schedule(new MyTask(), 1000);//启动任务,延迟1秒后执行。

}

}

但是,如果要求要在每年的某月某时某分某秒执行某个任务,使用Timer和TimerTask就无能为力了。在项目开发中往往会有两种定时控制的选择:

quartz组件:企业及定时调度组件,需要单独配置;

SpringTask:轻量级组件,配置简单,可以利用Annotation实现配置处理。

2,Quartz定义定时调度

使用Quartz组件,我们需要导入quartz的开发包,在pom.xml中添加quartz的开发包。

<dependency>

<groupId>org.quartz-scheduler</groupId>

<artifactId>quartz</artifactId>

<version>2.2.3</version>

</dependency>

引入包后,就可以进行定时调度的开发了。

有两种实现模式:

去继承QuartzJobBean父类;

直接利用配置就可以实现方法的调度控制。

1,继承一个父类实现任务的处理。

public class MyTask2 extends QuartzJobBean{

@Override

protected void executeInternal(JobExecutionContext context) throws JobExecutionException {

// TODO Auto-generated method stub

String currentTime=new  SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date());

System.out.println(currentTime);

System.out.println("具体的任务实现!!!");

}

}

所有的定时调度的启用都要在Spring的控制文件中完成,即,不需要去写一个明确的类来进行定时任务启用。

2,在applicationContext.xml文件中增加定时调度的配置,通过定时调度工厂类实现。

<bean id="taskFactory"

class="org.springframework.scheduling.quartz.JobDetailFactoryBean">

<property name="jobClass" value="cn.wnh.timerSask.MyTask1" />

<property name="jobDataMap">

<map>

<entry key="timeout" value="0" />

</map>

</property>

</bean>

随后配置任务的触发作业,对于作业的配置有两类:

使用间隔触发:若干时间之后重复执行;

工厂类:org.springframework.scheduling.quartz.SimpleTriggerFactoryBean

<bean id="simpleTrigger"

class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean">

<!-- 定义间隔触发的执行程序类 -->

<property name="jobDetail" ref="taskFactory"></property>

<!-- 设置定时触发延迟时间 -->

<property name="startDelay" value="0"></property>

<!-- 单位是”毫秒“ -->

<property name="repeatInterval" value="2000"></property>

</bean>

设置间隔触发调度器:org.springframework.scheduling.quartz.SchedulerFactoryBean

<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">

<property name="triggers">

<list>

<ref bean="simpleTrigger" />

</list>

</property>

</bean>

3,此时所有的间隔触发控制都交由Spring管理了,现在只需要启动Spring容器即可实现间隔触发任务。

使用Cron实现定时触发

Quartz不仅可以实现间隔触发,它还可以结合Cron实现定时触发,这也是它最重要的功能。

一般项目中使用最多的模式:小时触发,月初触发,年初触发。

修改之前的间隔触发配置,使用CronTriggerFactoryBean实现定时触发。

<bean id="taskFactory"

class="org.springframework.scheduling.quartz.JobDetailFactoryBean">

<property name="jobClass" value="cn.wnh.timerSask.MyTask1" />

<property name="jobDataMap">

<map>

<entry key="timeout" value="0" />

</map>

</property>

</bean>

<bean id="cronTrigger"

class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">

<property name="jobDetail" ref="taskFactory" />

<!-- cron表达式,描述每分钟触发一次 -->

<property name="cronExpression" value="0 * * * * ?" />

</bean>

<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">

<property name="triggers">

<list>

<ref bean="cronTrigger" />

</list>

</property>

</bean>

启动Spring容器即可实现。

2,不继承任何类实现定时调度

在项目开发中,继承直接会导致单继承的局限控制,所以在这种情况下Spring中提供了一种可以不继承任何类即可实现定时操作的任务处理。

定义一个任务执行类,不继承任何类。

public class MyTask2 {

public void taskSelf(){

String task=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new java.util.Date());

System.out.println(task);

System.out.println("执行具体任务操作");

}

}

在applicationContext.xml在配置工厂类:org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean

<bean id="taskFactory2"

class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">

<property name="targetObject">

<bean class="cn.wnh.timerSask.MyTask2" />

</property>

<!--配置要执行的方法 -->

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

</bean>

随后在任务调度配置上配置新的程序类

<bean id="cronTrigger"

class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">

<property name="jobDetail" ref="taskFactory2" />

<!-- cron表达式,描述每分钟触发一次 -->

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

</bean>

启动容器即可实现定时调度。

这种模式没有类的继承依赖,处理会更加灵活。

Spring Task实现定时调度

在Spring中自己有对定时调度的支持,使用起来感觉比Quartz还要好用。

它有两种实现方式,1,在applicationContext.xml中配置实现;2,使用Annotation实现。

不过使用什么模式,必须先有一个任务处理类。

定义任务处理类。

这里直接使用之前的MyTask2类,不再重复写。

修改applicationContext.xml文件:

需要追加task处理的命名空间定义:

xmlns:task="http://www.springframework.org/schema/task"

http://www.springframework.org/schema/task

http://www.springframework.org/schema/task/spring-task-4.1.xsd

1配置task操作的配置,实现间隔触发。

<bean id="myTask" class="cn.wnh.timerSask.MyTask2" />

<task:scheduled-tasks>

<task:scheduled ref="myTask" method="taskSelf"

fixed-rate="2000" />

</task:scheduled-tasks>

使用cron实现定时触发

<bean id="myTask" class="cn.wnh.timerSask.MyTask2" />

<task:scheduled-tasks>

<task:scheduled ref="myTask" method="taskSelf" cron="* * * * * ?" />

</task:scheduled-tasks>

可见,SpringTask实现起来更加简单。

Spring中实现定时调度的更多相关文章

  1. spring中的定时调度实现TimerFactoryBean引起的隐患

    手中的一个老项目,其中使用的TimerFactoryBean实现的调度任务.一般都是spring quartz实现,这种的着实少见.正因为少见资料比较少,当初为了确认这个会不会2个调度任务同时并行执行 ...

  2. Spring中的定时调度(Scheduling)和线程池(Thread Pooling)

    使用triggers和SchedulerFactoryBean来包装任务 我们已经创建了job details,jobs.我们同时回顾了允许你调用特定对象上某一个方法的便捷的bean. 当然我们仍需要 ...

  3. Spring使用@Scheduled定时调度

    一.spring配置文件中增加对这个注解的支持: 配置文件如下: <?xml version="1.0" encoding="UTF-8"?> &l ...

  4. Spring中使用Schedule调度

    在spring中两种办法使用调度,以下使用是在spring4.0中. 一.基于application配置文件,配置入下: <bean id="jobDetail" class ...

  5. spring中的定时任务调度用例

    在application-quartz.xml配置文件中添加如下配置信息: <!-- Quartz -->     <bean id="getSendEmailObject ...

  6. JAVA中的定时调度(Timer和TimerTask)

    某些时候我们需要定时去完成一些任务,这里举一个例子:我们需要在3秒钟后打印当前系统时间,此后每隔5秒重复此操作.代码如下: import java.util.TimerTask; import jav ...

  7. Spring使用Quartz定时调度Job无法Autowired注入Service的解决方案

    1)自定义JobFactory,通过spring的AutowireCapableBeanFactory进行注入,例如: public class MyJobFactory extends  org.s ...

  8. Java中的定时调度

    Timer类是一个线程设施,用于实现在某个时间或者某一段时间后安排某个任务执行一次或者定期重复执行.需要与TimerTask配合使用. TimerTask类用来实现由Timer安排的一次或重复执行的某 ...

  9. Spring Quartz定时调度任务配置

    applicationContext-quartz.xml定时调度任务启动代码: <?xml version="1.0" encoding="UTF-8" ...

随机推荐

  1. convertView的疑问(软件管理器)

    package com.hixin.appexplorer; import java.util.List; import android.app.Activity; import android.co ...

  2. 《算法4》1.5 - Union-Find 算法解决动态连通性问题,Python实现

    Union-Find 算法(中文称并查集算法)是解决动态连通性(Dynamic Conectivity)问题的一种算法,作者以此为实例,讲述了如何分析和改进算法,本节涉及三个算法实现,分别是Quick ...

  3. 利用HTTP-only Cookie缓解XSS之痛

    在Web安全领域,跨站脚本攻击时最为常见的一种攻击形式,也是长久以来的一个老大难问题,而本文将向读者介绍的是一种用以缓解这种压力的技术,即HTTP-only cookie. 我们首先对HTTP-onl ...

  4. AtomicInteger的使用

    JDK API 1.7相关介绍 可以用原子方式更新的 int 值.有关原子变量属性的描述,请参阅 java.util.concurrent.atomic 包规范.AtomicInteger 可用在应用 ...

  5. xjoi 2082: 小明的序列

    本文为博主原创文章,未均允许…… 反正我也没法管对吧 www点cnblogs点com/AwD-/ 维护一个序列,初始全为\(1\) 支持两种操作: 1.对于所有的位置\(i\),将它的值乘上\(i + ...

  6. Linux笔记③(ftp、nfs、ssh服务器搭建)

    1.ftp服务器搭建(利用vsftpd这个工具) 作用:文件的上传和下载 服务器端: 修改配置文件,配置文件目录:/etc/vsftpd.conf ,修改里面的允许匿名访问.指定匿名访问目录等操作,根 ...

  7. JVM-6.即时编译器

    一.即时编译器 二.运行模式 三.基本原理 四.编译优化技术 五.Java与C/C++的编译器对比 六.参考       一.即时编译器 1.在部分虚拟机(如Hotspot.IBM J9)中,Java ...

  8. nodejs中exports与module.exports的区别详细介绍

    如果模块是一个特定的类型就用Module.exports.如果模块是一个典型的"实例化对象"就用exports. exports.name = function() { conso ...

  9. java基础(九章)

    一.理解查询的机制 客户端应用程序(c/s.b/s)向后台服务器的DB发送一条select语句进行查询操作,会将结果集(虚拟表)返回到客户端应用程序 二.select语句 1.查询表中的全部列和行 s ...

  10. [Leetcode] Binary search--436. Find Right Interval

      Given a set of intervals, for each of the interval i, check if there exists an interval j whose st ...