【SSH项目实战】国税协同平台-33.quartz&SimpleTrigge

来源:互联网 发布:php的进程是什么 编辑:程序博客网 时间:2024/05/21 08:53
在我们“投诉管理”和“我要投诉”这两个模块完成之后,我们要做一个“自动投诉受理”功能。
我们来回顾一下“自动投诉受理”功能的需求分析:
自动投诉受理:在每个月月底最后一天对本月之前的投诉进行自动处理;将投诉信息的状态改为已失效。在后台管理中不能对该类型投诉进行回复。

自动投诉受理是在一定时间内自动的调度任务执行投诉状态的更新;那么需要使用任务调度,常见的任务调度有Jdk 的Timer 以及 quartz任务调度框架等。在spring中可以将这些任务调度集成由spring管理这些任务调度。

我们先使用Timer来完成这个模块:
一、JDK Timer & TimerTask
要使用JDK的Timer,需要完成以下两个动作:
① 继承TimerTask
② 制定Timer执行频率

我们首先在投诉管理的包下创建MyTimerTask,并继承TimerTask,我们在其中的run方法中显示了执行时间栈是当前的时间:
package cn.edu.hpu.tax.complain;import java.text.SimpleDateFormat;import java.util.Date;import java.util.TimerTask;public class MyTimerTask extends TimerTask {@Overridepublic void run() {SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");System.out.println("doing TimerTask..."+sdf.format(new Date()));}}

如果你看一下TimerTask的源码,你就应该发现其实是继承了Runnable的,所以它是一个线程类。

我们写一个测试类来测试MyTimerTask:
package cn.edu.hpu.tax.complain;import java.util.Timer;public class MyTimer {public static void main(String[] args) {Timer timer=new Timer();//延迟2秒执行,每3秒执行一次timer.schedule(new MyTimerTask(),2000,3000);}}

运行结果:
doing TimerTask...2015-12-02 10:51:30doing TimerTask...2015-12-02 10:51:33doing TimerTask...2015-12-02 10:51:36doing TimerTask...2015-12-02 10:51:39doing TimerTask...2015-12-02 10:51:42doing TimerTask...2015-12-02 10:51:45......

不停止运行的话,就以每3秒执行一次的频率无限执行下去。
也就是使用Timer(给定一个TimerTask任务栈),它就会按照规定的频率隔一段时间执行TimerTask栈中的run方法中的代码。

这就是我们的Timer和TimerTask。
如果要用Timer来做我们的“自动投诉受理”功能,我们就要每时每刻的去判断现在是不是本月的最后一天,这样做并不是太友好。

也就是说使用jdk的timer时发现无法满足这次的开发需求;即无法在指定的日期进行执行任务。

我们接下来介绍专门用来执行任务调度的框架,叫做Quartz 
二、Spring 集成Quartz 

Quartz是一个优秀的开源任务调度框架,这里加入的是quartz-1.8.6版本。Quart的官网:http://www.quartz-scheduler.org/;项目中的框架的spring是spring 3.0版本无法集成quartz 2.x及其后续版本;所以这里用quartz 1.8.6版本。

在spring中整合Quartz 进行任务调度:
首先将需要用到的jar包引入项目的lib目录下: 
(1)“org.springframework.context.support-3.0.2.RELEASE.jar”【 此包是spring根据quartz中的主要类进行再次封装成具有bean风格的类】
(2)“quartz-1.8.6.jar”【quartz的核心包】

我们把相关jar包引入进来之后,我们来使用quartz。
quartz也有两种运行机制,一个是以固定的频率来执行任务(这里和我们的Timer类似),另一个就是它可以配置非常精确的时间来执行你的任务(这个就是我们想要的)。

先来了解固定频率执行任务:
一、固定频率执行任务
我们要执行下面三个步骤来使用quartz

1、制定任务信息 bean
   作用:任务详细信息;包括调用哪个类;类中的哪个方法;执行时是否可并行执行任务。

   ① 设置执行对象
   ② 设置执行对象中对应的执行方法
   ③ 是否可以同步执行

2、制定任务执行时机(执行触发器) bean
   作用:主要用于定义任务信息 bean中任务什么时候执行

   2.1、简单触发器(SimpleTrigger)
   ① 设置任务详细
   ② 设置任务延迟执行时间
   ③ 设置任务执行频率

   2.2、任务触发器(CronTrigger)
   ① 设置任务详细
   ② 设置执行时机(cronExpression)
    cronExpression:秒 分 时 日 月 周 年(可选)

3、设置任务调度工厂 bean
   作用:用于调度各个任务触发器,设置触发器们

我们按步骤来编写代码测试:
首先我们写一个制定任务信息的bean:
package cn.edu.hpu.tax.complain;import java.text.SimpleDateFormat;import java.util.Date;public class QuartzTask {public void doSimpleTriggerTask() {SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");System.out.println("doing simpleTrigger task..."+sdf.format(new Date()));}}

然后是我们的quartz-spring.xml配置文件:
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">        <!-- 注册一个普通bean -->    <bean id="quartzTask" class="cn.edu.hpu.tax.complain.QuartzTask"></bean>        <!-- 1.指定任务详细信息 -->    <bean id="jobDetial1" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">    <!--  ① 设置执行对象 -->    <property name="targetObject" ref="quartzTask"></property>    <!--  ② 设置执行对象中对应的执行方法 -->    <property name="targetMethod" value="doSimpleTriggerTask"></property>    <!--  ③ 是否可以同步执行(这里设置不同步执行) -->    <property name="concurrent" value="false"></property>    </bean>        <!-- 2.制定任务执行时机(任务执行触发器) -->    <bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">    <!-- ① 设置任务详细 -->    <property name="jobDetail" ref="jobDetial1"></property>   <!-- ② 设置任务延迟执行时间(延迟2秒) -->   <property name="startDelay" value="2000"></property>   <!-- ③ 设置任务执行频率(执行频率为每2秒执行一下) -->   <property name="repeatInterval" value="2000"></property>    </bean>        <!-- 3.设置调度工厂 -->    <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">    <property name="triggers">    <list>    <!-- 第一个触发器 -->    <ref bean="simpleTrigger"/>    </list>    </property>    </bean></beans>

启动服务器测试,注意我们的服务器控制台,可以看到我们的任务正在后台悄无声息的按照我们配置的频率执行着:
十二月 02, 2015 12:45:39 下午 org.apache.coyote.http11.Http11Protocol start信息: Starting Coyote HTTP/1.1 on http-80十二月 02, 2015 12:45:39 下午 org.apache.jk.common.ChannelSocket init信息: JK: ajp13 listening on /0.0.0.0:8009十二月 02, 2015 12:45:39 下午 org.apache.jk.server.JkMain start信息: Jk running ID=0 time=0/19  config=null十二月 02, 2015 12:45:39 下午 org.apache.catalina.startup.Catalina start信息: Server startup in 33863 msdoing simpleTrigger task...2015-12-02 12:45:39doing simpleTrigger task...2015-12-02 12:45:41doing simpleTrigger task...2015-12-02 12:45:43doing simpleTrigger task...2015-12-02 12:45:45doing simpleTrigger task...2015-12-02 12:45:47doing simpleTrigger task...2015-12-02 12:45:49......

这个作用和Timer并没有太大区别,只是Timer是不可控的,参数不好改(要在代码里修改),但是spring架构下的quartzde的所有参数都是可以在配置文件中随意修改的,这里就比较可控。

我们上面使用的是spring架构下的quartzde的简单触发器,这个还不够好,我们的需求是到我们月末执行自动回复,我们可以利用quartzde的另一个触发器来实行点到点的定义(多少秒,什么时候来执行都可以)。

下一次我们继续。

转载请注明出处:http://blog.csdn.net/acmman/article/details/50183987

1 0
原创粉丝点击