Spring Quartz Java工程版和Web工程版示例

来源:互联网 发布:淘宝查看付款排名 编辑:程序博客网 时间:2024/05/19 13:43

转载自:http://blog.csdn.net/justdb/article/details/7750605


环境: MyEclipse8.6 + Tomcat6.0.18 + Spring2.5.6


最近研究Spring Quartz定时器任务调度,写了两个Demo,欢迎阅读指正。 


Spring Quartz Java工程版


Mission.java(任务源文件)


[java] view plaincopy
  1. package com.springquartz.bean;  
  2.   
  3. import org.apache.log4j.Logger;  
  4.   
  5. /**  
  6.  * @className:Mission.java 
  7.  * @classDescription: 
  8.  * @author:Wentasy 
  9.  * @createTime:2012-7-15 下午07:14:36 
  10.  * @since:JDK 1.6 
  11.  */  
  12. public class Mission {  
  13.     private Logger logger = Logger.getLogger(this.getClass().getName());  
  14.       
  15.       
  16.     public void print(){  
  17.         logger.info("开始进行任务调度......");  
  18.     }  
  19. }  


SpringQuartzTest.java(测试源文件)


[java] view plaincopy
  1. package com.springquartz.test;  
  2.   
  3. import org.springframework.context.ApplicationContext;  
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  5.   
  6. /**  
  7.  * @className:SpringQuartzTest.java 
  8.  * @classDescription: 
  9.  * @author:Wentasy 
  10.  * @createTime:2012-7-15 下午07:14:55 
  11.  * @since:JDK 1.6 
  12.  */  
  13. public class SpringQuartzTest {  
  14.   
  15.     /** 
  16.      * @param args 
  17.      */  
  18.     public static void main(String[] args) {  
  19.         // TODO Auto-generated method stub  
  20.         System.out.println("测试开始......");  
  21.         ApplicationContext  actx = new ClassPathXmlApplicationContext("applicationContext.xml");  
  22.         System.out.println("测试结束......");  
  23.     }  
  24.   
  25. }  



applicationContext.xml(Spring Quartz配置文件)


[html] view plaincopy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">  
  4. <beans>  
  5.   
  6.     <!-- 要调度的对象 -->  
  7.     <bean id="job" class="com.springquartz.bean.Mission"></bean>  
  8.     <!-- 定义目标bean和bean中的方法 -->  
  9.     <bean id="jobTask" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">  
  10.         <property name="targetObject">  
  11.             <ref local="job"/>  
  12.         </property>  
  13.         <property name="targetMethod">  
  14.             <value>print</value>  
  15.         </property>  
  16.     </bean>  
  17.     <!-- 定义触发的时间 -->  
  18.     <bean id="cron" class="org.springframework.scheduling.quartz.CronTriggerBean">  
  19.     <property name="jobDetail">  
  20.         <ref bean="jobTask"/>  
  21.     </property>  
  22.     <property name="cronExpression">  
  23.         <value>10,15,20,25,30,35,40,45,50,55,00 * * * * ?</value>  
  24.         <!--  <value>00,05 53,54 * * * ?</value>-->  
  25.     </property>  
  26.     </bean>  
  27.     <!-- 总管理 -->  
  28.     <bean autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">  
  29.         <property name="triggers">  
  30.             <list>  
  31.                 <ref local="cron"/>  
  32.             </list>  
  33.         </property>  
  34.     </bean>  
  35. </beans>  


 

运行效果截图:





Spring Quartz  Web工程版

PrintInfoJob.java(任务源文件)


[java] view plaincopy
  1. package com.springquartz.bean;  
  2.   
  3. import org.quartz.JobExecutionContext;  
  4. import org.quartz.JobExecutionException;  
  5. import org.springframework.scheduling.quartz.QuartzJobBean;  
  6.   
  7. import com.springquartz.service.IPrintInfoService;  
  8.   
  9. /**  
  10.  * @className:Mission.java 
  11.  * @classDescription: 
  12.  * @author:Wentasy 
  13.  * @createTime:2012-7-15 下午07:14:36 
  14.  * @since:JDK 1.6 
  15.  */  
  16. public class PrintInfoJob extends QuartzJobBean{  
  17.       
  18.     private IPrintInfoService prinfInfoService = null;  
  19.     public IPrintInfoService getPrinfInfoService() {  
  20.         return prinfInfoService;  
  21.     }  
  22.     public void setPrinfInfoService(IPrintInfoService prinfInfoService) {  
  23.         this.prinfInfoService = prinfInfoService;  
  24.     }  
  25.     @Override  
  26.     protected void executeInternal(JobExecutionContext arg0)  
  27.             throws JobExecutionException {  
  28.         // TODO Auto-generated method stub  
  29.         this.prinfInfoService.print();  
  30.           
  31.     }  
  32. }  



IprintInfoService.java(任务接口源文件)


[java] view plaincopy
  1. package com.springquartz.service;  
  2. /**  
  3.  * @className:IPrintInfoService.java 
  4.  * @classDescription: 
  5.  * @author:Wentasy 
  6.  * @createTime:2012-7-15 下午08:00:31 
  7.  * @since:JDK 1.6 
  8.  */  
  9. public interface IPrintInfoService {  
  10.       
  11.     /** 
  12.      * 方法名:print 
  13.      * 功能:打印信息 
  14.      */  
  15.     public void print();  
  16. }  


PrintInfoServiceImpl.java(任务实现源文件)


[java] view plaincopy
  1. package com.springquartz.service.impl;  
  2.   
  3. import java.text.SimpleDateFormat;  
  4. import java.util.Calendar;  
  5. import java.util.Date;  
  6.   
  7. import com.springquartz.service.IPrintInfoService;  
  8.   
  9. /**  
  10.  * @className:PrintInfoServiceImpl.java 
  11.  * @classDescription: 
  12.  * @author:Wentasy 
  13.  * @createTime:2012-7-15 下午07:59:33 
  14.  * @since:JDK 1.6 
  15.  */  
  16. public class PrintInfoServiceImpl implements IPrintInfoService{  
  17.   
  18.     public void print() {  
  19.         // TODO Auto-generated method stub  
  20.         Calendar now = Calendar.getInstance();  
  21.         System.out.println("现在是北京时间:" + this.format(now.getTime()));  
  22.     }  
  23.       
  24.     public String format(Date date){  
  25.         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
  26.         return sdf.format(date);  
  27.     }  
  28.       
  29. }  


applicationContext.xml(Spring Quartz配置文件)


[html] view plaincopy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <beans xmlns="http://www.springframework.org/schema/beans"  
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5.     xmlns:context="http://www.springframework.org/schema/context"  
  6.     xmlns:tx="http://www.springframework.org/schema/tx"  
  7.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  8.                 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd  
  9.                 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">  
  10.   
  11.       
  12.     <bean id="printInfoService" class="com.springquartz.service.impl.PrintInfoServiceImpl"/>  
  13.     <!-- 配置一个Job -->  
  14.     <bean id="printInfoJob" class="org.springframework.scheduling.quartz.JobDetailBean">  
  15.         <property name="jobClass" value="com.springquartz.bean.PrintInfoJob"/>  
  16.         <property name="jobDataAsMap">  
  17.             <map>  
  18.                 <entry key="prinfInfoService" value-ref="printInfoService"></entry>  
  19.             </map>  
  20.         </property>  
  21.     </bean>  
  22.       
  23.     <!-- 简单的触发器 -->  
  24.     <bean id="simplePrintInfoTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">  
  25.         <property name="jobDetail">  
  26.             <ref bean="printInfoJob"/>  
  27.         </property>  
  28.         <property name="startDelay">  
  29.             <value>6000</value>  
  30.         </property>  
  31.         <property name="repeatInterval">  
  32.             <value>6000</value>       
  33.         </property>  
  34.     </bean>  
  35.       
  36.     <!--复杂的触发器 -->  
  37.     <bean id="complexPrintInfoTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">  
  38.         <property name="jobDetail">  
  39.             <ref bean="printInfoJob"/>  
  40.         </property>  
  41.         <property name="cronExpression">  
  42.             <!-- <value>0 0/1 * * * ?</value> -->  
  43.             <value>00,05,10,15,20,25,30,35,40,45,50,55 * * * * ?</value>  
  44.         </property>  
  45.     </bean>  
  46.       
  47.     <!-- spring触发工厂 -->  
  48.     <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">  
  49.         <property name="triggers">  
  50.             <list>  
  51.                 <ref bean="complexPrintInfoTrigger"/>  
  52.             </list>  
  53.         </property>  
  54.     </bean>  
  55. </beans>  


web.xml(Web工程配置文件)


[html] view plaincopy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="2.5"   
  3.     xmlns="http://java.sun.com/xml/ns/javaee"   
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  5.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
  6.     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  
  7.       
  8.     <context-param>  
  9.         <param-name>contextConfigLocation</param-name>  
  10.         <param-value>classpath:applicationContext*.xml</param-value>  
  11.     </context-param>  
  12.       
  13.     <listener>  
  14.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  15.     </listener>  
  16.       
  17.     <welcome-file-list>  
  18.         <welcome-file>index.jsp</welcome-file>  
  19.     </welcome-file-list>  
  20. </web-app>  


运行效果截图:



项目注意事项:

1.所需Jar包:

commons-collections-3.2.jar commons-logging-1.1.jar jta.jar log4j-1.2.15.jarquartz-all-1.6.5.jar spring.jar

 

本文源码,欢迎下载:

Spring Quartz Java工程版


Spring Quartz  Web工程版

 


参考资料:

 

Quartz CronTrigger最完整配置说明


 

Spring Quartz相关问题


 

关于Spirng Quartz定时触发器+源码示例!


 

Spring配置Quartz例子


 

Spring + Quartz配置实例


 

Spring中Quartz的配置


0 0
原创粉丝点击