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");//为什么必须用这个方法触发呢不触发为什么不行呢?
}
}
这是我的一个测试,我配的2个任务。 spring 的配置:
运行时,首先我采用MainTest 测试
发现程序打印end后就结束了。 然后我用Main测试,可以正常执行。
job类的内容为:
最好不要测试Quartz本身...
PS:你可以把有问题的代码发上来看看.
很奇怪,在main方法中能正常运行, 而在测试类中,即继承TestCase的类中写一个方法,运行上述代码,则启动不了调度。 这是为什么呢?
jdk的版本 eclipse版本 project -> javacompiler的jdk版本 tomcat jdk 版本...
Quarzt已经配置好 tomcat 5.*启动报错 大佬 帮忙看下
严重: Error listenerStart 2009-5-8 16:00:39 org.apache.catalina.core.StandardContext start 严重: Context startup failed due to previous errors
代码:
:idea: 谢谢
在web.xml配置spring的监听器就可以,如果还要硬编码的形式来获取Scheduler,可能不能完全成为定时器,或则定时任务。 org.springframework.web.context.ContextLoaderListener
设置lazy-init为false
factory.getBean("z");//为什么必须用这个方法触发呢不触发为什么不行呢? 因为这个bean没有加载,scheduler没有实例化。如果自定义scheduler,还需要显示调用scheduler.start().或者再配置文件中声明。