Spring中的Quartz配置

来源:互联网 发布:老电视如何看网络电视 编辑:程序博客网 时间:2024/05/16 19:56

首先我们来写一个被调度的类: package com.kay.quartz;

  1. public class QuartzJob  
  2. {  
  3.     public void work()  
  4.     {  
  5.     System.out.println("Quartz的任务调度!!!");  
  6.     }  

Spring的Quartz配置文件:

  1. <?xml version="1.0" encoding="UTF-8"?> 
  2. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> 
  3.  
  4. <beans>      
  5.         <!-- 要调用的工作类 --> 
  6.         <bean id="quartzJob" class="com.kay.quartz.QuartzJob"></bean> 
  7.         <!-- 定义调用对象和调用对象的方法 --> 
  8.         <bean id="jobtask" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> 
  9.             <!-- 调用的类 --> 
  10.             <property name="targetObject"> 
  11.                 <ref bean="quartzJob"/> 
  12.             </property> 
  13.             <!-- 调用类中的方法 --> 
  14.             <property name="targetMethod"> 
  15.                 <value>work</value> 
  16.             </property> 
  17.         </bean> 
  18.         <!-- 定义触发时间 --> 
  19.         <bean id="doTime" class="org.springframework.scheduling.quartz.CronTriggerBean"> 
  20.             <property name="jobDetail"> 
  21.                 <ref bean="jobtask"/> 
  22.             </property> 
  23.             <!-- cron表达式 --> 
  24.             <property name="cronExpression"> 
  25.                 <value>10,15,20,25,30,35,40,45,50,55 * * * * ?</value> 
  26.             </property> 
  27.         </bean> 
  28.         <!-- 总管理类 如果将lazy-init='false'那么容器启动就会执行调度程序  --> 
  29.         <bean id="startQuertz" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> 
  30.             <property name="triggers"> 
  31.                 <list> 
  32.                     <ref bean="doTime"/> 
  33.                 </list> 
  34.             </property> 
  35.         </bean> 
  36.       
  37. </beans>测试程序:  
  38. package com.kay.quartz;  
  39. import org.springframework.context.ApplicationContext;  
  40. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  41.  
  42. public class MainTest  
  43. {  
  44.     /**  
  45.      * @param args  
  46.      */  
  47.     public static void main(String[] args)  
  48.     {  
  49.         System.out.println("Test start.");  
  50.         ApplicationContext context = new ClassPathXmlApplicationContext("quartz-config.xml");  
  51.         //如果配置文件中将startQuertz bean的lazy-init设置为false 则不用实例化  
  52.         //context.getBean("startQuertz");  
  53.         System.out.print("Test end..");        
  54.     }  

我们需要把log4j的配置文件放入src目录下,启动main类就可以了。

0 0
原创粉丝点击