ITeye博客

Spring配置Quartz例子

抛出异常的爱 · 2006-12-18 10:13 · 技术 · Markdown

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!--起动Bean-->
 <bean id="z" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
 	<property name="triggers">
 		<list>
 	
			<ref bean="cronReportTrigger"/>
 		</list>
 	</property>
 </bean> 
<!--实际的工作Bean-->
  <bean id="courseService" class="com.spring.helloworld.CourseService"></bean>
<!--jobBean用于设定启动时运用的Bean与方法-->
 <bean id="scheduledReportJobDetail"  
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
	 <property name="targetObject">
	 	<ref  bean="courseService"/>	
	 </property>
	 <property name="targetMethod">
		 <value>sendCourseEnrollmentReport</value>
	 </property>
 </bean>
<!--定时器设定起动频率&启动时间我设的是每5秒起动一次 (0 0  4 * * ?每日四点起动....)-->
 <bean id="cronReportTrigger" 
class="org.springframework.scheduling.quartz.CronTriggerBean">
 <property  name="jobDetail"><ref bean="scheduledReportJobDetail"/>
 </property>
 <property name="cronExpression"><value>10,15,20,25,30,35,40,45,50,55 * * * * ?</value>  
</property>
 </bean>
 	
</beans>

测试

package com.spring.helloworld;


import java.io.FileNotFoundException;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;


import com.spring.hellow.LogInit;
import com.spring.hellow.QuestException;


public class TaskApp {

	public static void main(String[] args) throws BeansException, FileNotFoundException, QuestException, InterruptedException {
		new LogInit("WEB-INF/classes/com/spring/helloworld/log4j.properties");
		BeanFactory factory = new XmlBeanFactory(new FileSystemResource(
        "WEB-INF/classes/" +
        "com/spring/helloworld/" +
        "helloworld.xml"));
		factory.getBean("z");//为什么必须用这个方法触发呢不触发为什么不行呢?


	}
	

}

评论(9)

  1. #9 楼 · fly_ever · 2009-11-05 16:27

    这是我的一个测试,我配的2个任务。 spring 的配置:

    
    	<!-- 与quartz有关的设置 -->
    	<bean id="billJob" class="test.BillReliabilityJob"/>
    	<bean id="exceptionJob" class="test.ExceptionJob"/>
    	
    	<bean id="billJobTask" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
    		<property name="targetObject">
    			<ref bean="billJob"/><!-- 调用的Job类 -->
    		</property>
    		<property name="targetMethod">
    			<value>run</value><!-- 调用的类中的方法 -->
    		</property>
    	</bean>
    	<bean id="exceptionJobTask" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
    		<property name="targetObject">
    			<ref bean="exceptionJob"/><!-- 调用的Job类 -->
    		</property>
    		<property name="targetMethod">
    			<value>run</value><!-- 调用的类中的方法 -->
    		</property>
    	</bean>
    	
    	<bean id="billRunTime" class="org.springframework.scheduling.quartz.CronTriggerBean">
    		<property name="jobDetail">
    			<ref bean="billJobTask"/>
    		</property>
    		<property name="cronExpression">
    			<value>0/5 * * * * ?</value>
    		</property>
    	</bean>
    	<bean id="ExceptionRunTime" class="org.springframework.scheduling.quartz.CronTriggerBean">
    		<property name="jobDetail">
    			<ref bean="exceptionJobTask"/>
    		</property>
    		<property name="cronExpression">
    			<value>10 * * * * ?</value>
    		</property>
    	</bean>
    	
    	<!-- 总管理类,如果lazy-init='false',则容器启动时就会执行调度程序-->
    	<!-- 如果lazy-init='true',则需要实例化该bean才能执行调度程序            -->
    	
    	<bean id="billStartQuartz" lazy-init="false" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
    		<property name="triggers">
    			<list>
    				<ref bean="billRunTime"/>
    				<ref bean="ExceptionRunTime"/>
    			</list>
    		</property>
    	</bean>
    
    

    运行时,首先我采用MainTest 测试

    
    public class MainTest extends TestCase {
    
    	public void testJob(){
    		System.out.println("start");
    		ApplicationContext context = new FileSystemXmlApplicationContext(
    		"./config/spring.xml");
    		System.out.println("end");
    	}
    }
    
    

    发现程序打印end后就结束了。 然后我用Main测试,可以正常执行。

    
    public class Main {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		System.out.println("start");
    		ApplicationContext context = new FileSystemXmlApplicationContext(
    		"./config/spring.xml");
    		System.out.println("end");
    	}
    
    }
    
    

    job类的内容为:

    
    public class ExceptionJob {
    
    	public void run(){
    		System.out.println("ExceptionJob running");
    	}
    }
    
    
  2. #8 楼 · 抛出异常的爱 · 2009-11-05 15:16

    fly_ever writes:

    很奇怪,在main方法中能正常运行, 而在测试类中,即继承TestCase的类中写一个方法,运行上述代码,则启动不了调度。 这是为什么呢?

    最好不要测试Quartz本身...

    PS:你可以把有问题的代码发上来看看.

  3. #7 楼 · fly_ever · 2009-11-05 15:04

    很奇怪,在main方法中能正常运行, 而在测试类中,即继承TestCase的类中写一个方法,运行上述代码,则启动不了调度。 这是为什么呢?

  4. #6 楼 · 抛出异常的爱 · 2009-05-12 15:23

    jdk的版本 eclipse版本 project -> javacompiler的jdk版本 tomcat jdk 版本...

    12616383 writes:

    Quarzt已经配置好  tomcat 5.*启动报错   大佬 帮忙看下

    严重: Error listenerStart 2009-5-8 16:00:39 org.apache.catalina.core.StandardContext start 严重: Context [/irdpcms] startup failed due to previous errors

  5. #5 楼 · 12616383 · 2009-05-08 16:15

    Quarzt已经配置好 tomcat 5.*启动报错 大佬 帮忙看下

    严重: Error listenerStart 2009-5-8 16:00:39 org.apache.catalina.core.StandardContext start 严重: Context startup failed due to previous errors

    代码:

    
    
    <!--配置一个Job-->
    	<bean id="testJobDetail" class="org.springframework.scheduling.quartz.JobDetailBean">
    			<property name="jobClass">
    				<value>com.zfsoft.irdpcms.action.QuartzJob</value>
    			</property>
    			
    			<property name="jobDataAsMap">
    				<map>
    					<entry key="pringThing">
    						<value>this is the first Quarzt</value>
    					</entry>	
    					
    					<entry key="printName">
    						<value>leo</value>
    					</entry>			
    				</map>
    			</property>
    	</bean>
    	
    	<bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
    		<property name="jobDetail">
    			<ref bean="testJobDetail"/>
    		</property>
    		
    		<property name="startDelay">
    			<value>10</value>
    		</property>
    		
    		<property name="repeatInterval">
    			<value>2000</value>
    		</property>
    		
    		<property name="repeatCount">
    			<value>20</value>
    		</property>
    		
    	</bean>
    	
    	<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
    		<property name="autoStartup">
    			<value>true</value>
    		</property>
    		
    		<property name="triggers">
    			<list>
    				<ref bean="simpleTrigger"/>
    			</list>
    		</property>
    	</bean>
    	
    
    
    
  6. #4 楼 · 抛出异常的爱 · 2008-07-15 11:34

    :idea: 谢谢

  7. #3 楼 · sbe_steels · 2008-07-05 18:31

    在web.xml配置spring的监听器就可以,如果还要硬编码的形式来获取Scheduler,可能不能完全成为定时器,或则定时任务。 org.springframework.web.context.ContextLoaderListener

  8. #2 楼 · julycool · 2007-09-05 16:39

    设置lazy-init为false

  9. #1 楼 · Godlikeme · 2006-12-24 13:22

    factory.getBean("z");//为什么必须用这个方法触发呢不触发为什么不行呢? 因为这个bean没有加载,scheduler没有实例化。如果自定义scheduler,还需要显示调用scheduler.start().或者再配置文件中声明。

评论功能维护中,当前仅展示历史评论。