2006-12-18

Spring配置Quartz例子

关键字: spring quartz
<?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");//为什么必须用这个方法触发呢不触发为什么不行呢?


	}
	

}
评论
抛出异常的爱 2008-07-15

谢谢
sbe_steels 2008-07-05
在web.xml配置spring的监听器就可以,如果还要硬编码的形式来获取Scheduler,可能不能完全成为定时器,或则定时任务。
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
julycool 2007-09-05
设置lazy-init为false
Godlikeme 2006-12-24
factory.getBean("z");//为什么必须用这个方法触发呢不触发为什么不行呢?
因为这个bean没有加载,scheduler没有实例化。如果自定义scheduler,还需要显示调用scheduler.start().或者再配置文件中声明。
发表评论

您还没有登录,请登录后发表评论

抛出异常的爱
搜索本博客
我的相册
1569dbfd-b702-3ce0-a83c-605b146f22c2-thumb
长椅
共 39 张
存档
最新评论