昊天

j2ee中使用Spring集成quartz计划任务

Spring提供了对quartz的集成,这样在j2ee的应用中,可以很方便的实现我们的任务计划,比如:你可以设定每天半夜的时候,来实现备份数据库,记录日志,因为这个时候web的压力相对比较小。也可以用来定时的发EMAIL等。

1、Spring中集成quartz 首先需要在web.xml 中配置个quartz的监听器。这样,随着WEB程序的启动,会自动启动quartz的调度配置。

Web.xml 中加入 监听器:

<listener> 
<listener-class>com.yangxinyong.quartz.listener.QuartzServiceLoader</listener-class>  
 </listener>

2、QuartzServiceLoader 实现了 javax.servlet.ServletContextListener  ,里面有两个方法。

QuartzServiceLoader类:

Java代码  
  1. public class QuartzServiceLoader implements ServletContextListener {     
  2.     public void contextDestroyed(ServletContextEvent arg0) {             
  3.         try {     
  4.                 //监听器关闭时关闭Sheduler     
  5.          JobListener.stop();     
  6.         } catch (Exception ex) {     
  7.             System.out.println(ex.getMessage());     
  8.         }     
  9.          
  10.     }     
  11.     
  12.     public void contextInitialized(ServletContextEvent arg0) {     
  13.         try {    //监听器启动时启动Sheduler      
  14.          JobListener.run();     
  15.         } catch (Exception ex) {     
  16.             System.out.println(ex.getMessage());     
  17.         }     
  18.     }     
  19.     
  20. }    

 

3、此时,我们要新建个 JobListener 类 ,来读取配置文件applicationContext-quartz.xml,启动quartz的计划任务配置,以及关闭监听时的关闭quartz的操作。

Java代码  
  1. package com.yangxinyong.quartz.listener;   
  2.   
  3. import org.quartz.Scheduler;   
  4. import org.quartz.impl.StdSchedulerFactory;   
  5. import org.springframework.beans.factory.xml.XmlBeanFactory;   
  6. import org.springframework.core.io.ClassPathResource;   
  7. import org.springframework.scheduling.quartz.CronTriggerBean;   
  8. import org.springframework.scheduling.quartz.JobDetailBean;   
  9.   
  10.   
  11. /**  
  12.  * @author Seyo816@gmail.com  
  13.  */  
  14. public class JobListener   
  15. {   
  16.     
  17.  public static void run() throws Exception {     
  18.   ClassPathResource res = new ClassPathResource( "applicationContext-quartz.xml" );   
  19.    XmlBeanFactory factory = new XmlBeanFactory( res );   
  20.    JobDetailBean job = ( JobDetailBean ) factory   
  21.      .getBean( "Job" );   
  22.    CronTriggerBean trigger = ( CronTriggerBean ) factory   
  23.      .getBean( "cronTrigger" );   
  24.    Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler( );   
  25.    scheduler.start( );   
  26.    scheduler.scheduleJob( job, trigger );   
  27.  }     
  28.   
  29.   
  30.  public static void stop() throws Exception {     
  31.        
  32.  }     
  33. }  

 

其中applicationContext-quartz.xml 放在classpath路径下,应用程序才可以找到该配置文件。配置文件定义了一个job bean 以及 触发器cronTrigger bean ,最后 通过 SchedulerFactoryBean 添加 添加触发器 。

applicationContext-quartz.xml  代码:

 

Xml代码  
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"    
  3. "http://www.springframework.org/dtd/spring-beans.dtd">  
  4. <beans>  
  5.   
  6.  <bean name="Job" class="org.springframework.scheduling.quartz.JobDetailBean">  
  7.     
  8.   <property name="jobClass">  
  9.    <value>com.yangxinyong.quartz.listener.Job</value>  
  10.   </property>  
  11.     
  12.   <property name="jobDataAsMap">  
  13.    <map>  
  14.     <entry key="email"><value>seyo816@gmail.com</value></entry>  
  15.    </map>  
  16.   </property>  
  17.     
  18.     
  19.  </bean>  
  20.     
  21.  <bean id="cronTrigger" class="com.yangxinyong.quartz.listener.InitializingCronTrigger">  
  22.   <property name="jobDetail">  
  23.    <ref bean="Job"/>  
  24.   </property>  
  25.   <property name="cronExpression">  
  26.    <value>0 50 16 * * ?</value>  
  27.   </property>  
  28.  </bean>  
  29.     
  30.     
  31.  <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">  
  32.   
  33.   <!-- 添加触发器 -->  
  34.   <property name="triggers">  
  35.    <list>  
  36.     <ref local="cronTrigger"/>  
  37.    </list>  
  38.   </property>  
  39.  </bean>  
  40.     
  41. </beans>  

 

3、job bean  对应的类:com.yangxinyong.quartz.listener.Job 是spring的 QuartzJobBean接口的实现,用来定义 执行的任务。

Java代码  
  1. package com.yangxinyong.quartz.listener;   
  2.   
  3. import java.util.Date;   
  4.   
  5. import org.quartz.JobDetail;   
  6. import org.quartz.JobExecutionContext;   
  7. import org.quartz.JobExecutionException;   
  8. import org.springframework.scheduling.quartz.QuartzJobBean;   
  9.   
  10. /**  
  11.  * @author Seyo816@gmail.com  
  12.  */  
  13. public class Job extends QuartzJobBean {   
  14.  private String email;   
  15.   
  16.  protected void executeInternal(JobExecutionContext context)   
  17.    throws JobExecutionException {   
  18.   JobDetail job= context.getJobDetail();   
  19.   System.out.println(new Date()+":"+job.getName()+"["+email+"]");   
  20.  }   
  21.   
  22.   
  23.  public String getEmail() {   
  24.   return email;   
  25.  }   
  26.   
  27.  public void setEmail(String email) {   
  28.   this.email = email;   
  29.  }   
  30.   
  31. }  

 

该job只是简单的 在控制台中输出 一串字符串。

最后你可以 部署该 web应用,根据触发器中的时间配置,将实现 每天的16:50分的时候 执行操作。我们可以看到下面的输出字符串。

Tue Nov 18 16:50:00 CST 2008:Job[seyo816@gmail.com]

posted on 2011-05-24 11:50 昊天 阅读(3702) 评论(1)  编辑  收藏 所属分类: Quartz

评论

# re: j2ee中使用Spring集成quartz计划任务[未登录] 2011-06-17 13:19 wind

非常感谢这篇文章,解决了我的问题  回复  更多评论   


只有注册用户登录后才能发表评论。


网站导航:
 

导航

<2011年5月>
24252627282930
1234567
891011121314
15161718192021
22232425262728
2930311234

统计

留言簿(1)

随笔分类

随笔档案

搜索

最新评论

阅读排行榜

评论排行榜