(七)sping之定时任务Quartz

来源:互联网 发布:淘宝合并付款没有代付 编辑:程序博客网 时间:2024/05/29 09:24

实际项目中有关定时任务的使用还是比较普遍的,比如定时做报表,定时取数据,定时发送邮件等等,不过我们熟悉的并且比较方便的要么就是TimerTask要么就是Quartz,不过要按方便来讲和使用率来讲,大家还是普遍愿意在实际项目中使用更优秀的Quartz,尤其是和Spring配合起来十分方便,不过TimerTask也可以配置在Spring中

首先还是讲Quartz吧,之前大家都知道在不需要框架的项目中,我们的定时任务通常将执行代码写入到servlet的init方法中,然后在web.xml中配置

Java代码  收藏代码
  1. <span style="font-size: medium;"><servlet>  
  2.         <servlet-name>Init</servlet-name>  
  3.         <servlet-class>  
  4.             com.cn.util.timer.TestTimerTaskServlet  
  5.         </servlet-class>  
  6.         <load-on-startup>1</load-on-startup>  
  7.   </servlet></span>  

 

这样的代码,只要服务器一直在开启状态,就会按时执行任务的

一般来说Quart中有几个比教重要的概念:定时任务,触发器Trigger,调度器Scheduler,并且定时任务需要传入一个具体执行任务的类,这个类必须继承import org.quartz.Job这个类,并且实现execute这个方法,至于execute方法里面就是功能代码了,就是上面提到的统计报表,获取数据,发送邮件之类的事情了,欲了解更多的在Java中使用Quartz的知识请参考我很久以前写的有关Quartz的文章

Spring和Quartz的整合

必须包



 

 

首先说一种不必继承任何类的具体定时任务的执行类JobTask.java

Java代码  收藏代码
  1. <span style="font-size: medium;">package com.javacrazyer.comon;  
  2.   
  3.   
  4. public class JobTask {  
  5.       
  6.     public void doJob(){  
  7.         System.out.println("执行发送邮件的程序...");  
  8.     }  
  9. }  
  10. </span>  

 

那么这个具体实现功能的类就没有继承任何其他第三方类

 Spring的配置文件applicationContext.xml具体配置

Xml代码  收藏代码
  1. <span style="font-size: medium;"><?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"  
  4.     xmlns:tx="http://www.springframework.org/schema/tx"  
  5.     xsi:schemaLocation="  
  6.             http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  7.             http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd  
  8.             http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">  
  9.   
  10.     <bean id="jobTask" class="com.javacrazyer.comon.JobTask"></bean>  
  11.     <!-- 定义JobDetail 具体的要执行的目标BEAN和执行的目标方法-->  
  12.     <bean id="jobDetail"  
  13.         class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">  
  14.        <!--引用实际目标bean-->  
  15.         <property name="targetObject" ref="jobTask" />  
  16.        <!--目标bean中的目标方法:doJob方法,意思为将来执行定时任务就是该bean中的doJob方法-->  
  17.         <property name="targetMethod" value="doJob" />  
  18.   
  19.     </bean>  
  20.       
  21.   
  22.     <!-- 使用triggers和SchedulerFactoryBean来包装任务 -->  
  23.     <!--定义触发的时间-->   
  24.     <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">  
  25.         <property name="jobDetail" ref="jobDetail" />  
  26.         <!--                       cron表达式          秒       分 时  日 月  周  (年) -->  
  27.         <property name="cronExpression" value="0/15 * * * * ?" />  
  28.     </bean>  
  29.       
  30. <!--  管理触发器,就相当于在java中使用Quartz代码中的调度器scheduler  -->  
  31.     <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">  
  32.         <property name="triggers">  
  33.             <list>  
  34.                 <ref bean="cronTrigger" />  
  35.             </list>  
  36.         </property>  
  37.     </bean>  
  38.       
  39.       
  40.       
  41. </beans>  
  42. </span>  

 

有关cron表达式的的我之前的文章介绍过

 

大家发现没有JobDetail 那里配置指明了类,还必须指明具体要执行的方法,具体原因是MethodInvokingJobDetailFactoryBean这个类配置里面必须要求同时配上类和方法,所以我之前的类就没有继承任何类。

 

那么,如果有人懒得配置方法的话,还想按Quartz正常道路来走的话,那么具体执行类要变化了

Java代码  收藏代码
  1. <span style="font-size: medium;">package com.javacrazyer.comon;  
  2.   
  3. import org.quartz.JobExecutionContext;  
  4. import org.quartz.JobExecutionException;  
  5. import org.springframework.scheduling.quartz.QuartzJobBean;  
  6.   
  7.   
  8. public class JobTask extends QuartzJobBean{  
  9.   
  10.     @Override  
  11.     protected void executeInternal(JobExecutionContext arg0)  
  12.             throws JobExecutionException {  
  13.         System.out.println("执行发送邮件的程序...");       
  14.     }  
  15. }  
  16. </span>  

 

并且spring配置中个JobDetail那个bean的整体换成

Java代码  收藏代码
  1. <span style="font-size: medium;"><bean id="jobDetail"    
  2.         class="org.springframework.scheduling.quartz.JobDetailBean">    
  3.         <property name="jobClass">    
  4.             <value>com.javacrazyer.comon.JobTask</value>    
  5.         </property>    
  6.     
  7.     </bean>  </span>  

 

像这样两种方式的都可以进行执行了,根据个人喜好,想选哪个都可以

web.xml大家再熟悉不过了,还是写下吧

Java代码  收藏代码
  1. <span style="font-size: medium;"><?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
  5.     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  
  6.     <!-- Spring ApplicationContext Definition -->  
  7.     <context-param>  
  8.         <param-name>contextConfigLocation</param-name>  
  9.         <param-value>/WEB-INF/applicationContext.xml</param-value>  
  10.     </context-param>  
  11.     <listener>  
  12.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  13.     </listener>  
  14.   
  15.     <welcome-file-list>  
  16.         <welcome-file>index.jsp</welcome-file>  
  17.     </welcome-file-list>  
  18. </web-app>  
  19. </span>  

 

好了部署项目吧,启动tomcat吧,看看control控制台是不是每隔15秒就输出

执行发送邮件的程序...

 

 差点忘记还有TimerTask跟Spring的结合的配置,需要一个实现的具体类

Java代码  收藏代码
  1. <span style="font-size: medium;">package com.javacrazyer.comon;  
  2.   
  3. import java.util.TimerTask;  
  4.   
  5. public class EamilTask extends TimerTask{  
  6.     
  7.     @Override  
  8.     public void run() {  
  9.         System.out.println("开始收取数据了.........");       
  10.     }  
  11. }  
  12. </span>  

 

spring配置

Xml代码  收藏代码
  1. <span style="font-size: medium;"><?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"  
  4.     xmlns:tx="http://www.springframework.org/schema/tx"  
  5.     xsi:schemaLocation="  
  6.             http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  7.             http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd  
  8.             http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">  
  9. <bean id="eamilTask" class="com.javacrazyer.comon.EamilTask" />  
  10. <!--  对定时器进行设置每隔30秒调用一次emailTask -->  
  11.     <bean id="springSchedule" class="org.springframework.scheduling.timer.ScheduledTimerTask">  
  12.         <property name="timerTask" ref="eamilTask"></property>  
  13.   
  14.         <property name="period">  
  15.             <value>10000</value>  
  16.         </property>  
  17.   
  18.         <property name="delay">  
  19.             <value>1000</value>  
  20.         </property>  
  21.     </bean>  
  22. <!--   启动定时器  -->  
  23.     <bean id="startTimeTask" class="org.springframework.scheduling.timer.TimerFactoryBean">  
  24.         <property name="scheduledTimerTasks">  
  25.             <list>  
  26.                 <ref bean="springSchedule" />  
  27.             </list>  
  28.         </property>  
  29.     </bean>  
  30.   
  31.   
  32. </beans>  
  33. </span>  

 

 同样,启动服务器后,发现每隔10秒,执行下输出

开始收取数据了.........

原创粉丝点击