`
抛出异常的爱
  • 浏览: 620704 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

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


	}
	

}
分享到:
评论
9 楼 fly_ever 2009-11-05  
这是我的一个测试,我配的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");
	}
}
8 楼 抛出异常的爱 2009-11-05  
fly_ever 写道
很奇怪,在main方法中能正常运行,
而在测试类中,即继承TestCase的类中写一个方法,运行上述代码,则启动不了调度。
这是为什么呢?


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

PS:你可以把有问题的代码发上来看看.
7 楼 fly_ever 2009-11-05  
很奇怪,在main方法中能正常运行,
而在测试类中,即继承TestCase的类中写一个方法,运行上述代码,则启动不了调度。
这是为什么呢?

6 楼 抛出异常的爱 2009-05-12  
jdk的版本
eclipse版本 project -> javacompiler的jdk版本
tomcat jdk 版本...
12616383 写道

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

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






5 楼 12616383 2009-05-08  
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


代码:

<!--配置一个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>
	

4 楼 抛出异常的爱 2008-07-15  

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

相关推荐

    spring3配置quartz定时任务

    spring3 配置quartz定时任务的使用。一个小例子。可直接运行Test.java。就可以启动定时任务。每10秒钟执行一次,可以根据自己配置。

    quartz入门例子,spring集成配置使用

    Spring集成quartz跑定时任务实例 自己写的例子并为实现job 有测试的主函数,请参考...springCon.quartz文件夹下 为对上诉博客理解透彻 结合spring看其配置的相关信息 就可以明白。

    一个Spring+quartz的例子,希望对大家有帮助

    Spring+quartz 一个简单的 定时任务的例子,希望能对大家的学习有帮助。 最后,附:cronExpression配置说明 字段 允许值 允许的特殊字符 秒 0-59 , - * / 分 0-59 , - * / 小时 0-23 , - * / 日期 1-31 , - *...

    Quartz spring配置

    Quartz spring配置 简单例子,

    spring3 配置quartz定时任务的使用

    spring3 配置quartz定时任务的使用。一个小例子。可直接运行Test.java。就可以启动定时任务。

    spring quartz定时器的简单配置和使用

    我自己亲自测试过的例子,spring quartz定时器的简单配置和使用,保证成功运行

    spring quartz的配置完整Demo

    看了网上很介绍配置文章,感觉很乱。而且不一定能跑起来。现在我把我的demo上传来。已经跑过。请放心使用。但是jar包太大上传不上来。自己导一下spring的jar就可以了。务必是commons-collections-3.1.jar不然会报错...

    Spring+Quartz2.2.0+Demo源码实战演练【精华篇2014】

    spring3.2之后quartz和之前的定时服务配置稍有不同 在这里分享一个自己写的例子(代码、源码、jar包都齐全) 直接导入myeclipse即可

    SpringMVC利用Quartz实现可配置的定时器

    待完善。简单的例子,免费下载。参考http://www.meiriyouke.net/?p=82 谢谢原作者 也谢谢ZJ

    spring-boot示例项目

    本项目示例基于spring boot 最新版本(2.1.9)实现,Spring Boot、Spring Cloud 学习示例...多数Spring Boot应用只需要很少的Spring配置。 Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Sprin

    Spring+3.x企业应用开发实战光盘源码(全)

     第4章:讲解如何在Spring配置文件中使用Spring 3.0的Schema格式配置Bean的内容,并对各个配置项的意义进行了深入的说明。  第5章:对Spring容器进行解构,从内部探究Spring容器的体系结构和运行流程。此外,我们...

    Spring-Reference_zh_CN(Spring中文参考手册)

    6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ Load-time weaving(LTW) 6.9. 其它资源 7. Spring AOP APIs 7.1. 简介 7.2. Spring中的切入点API 7.2.1. 概念 7.2.2. 切入点实施 ...

    Spring.3.x企业应用开发实战(完整版).part2

    2.2.3 类包及Spring配置文件规划 2.3 持久层 2.3.1 建立领域对象 2.3.2 UserDao 2.3.3 LoginLogDao 2.3.4 在Spring中装配DAO 2.4 业务层 2.4.1 UserService 2.4.2 在Spring中装配Service 2.4.3 单元测试 2.5 展现层 ...

    Spring 2.0 开发参考手册

    6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ Load-time weaving(LTW) 6.9. 其它资源 7. Spring AOP APIs 7.1. 简介 7.2. Spring中的切入点API 7.2.1. 概念 7.2.2. 切入点...

    java各种job

    此资源包括了java各中job:java基础job,quartz框架的job,spring scheduling job,spring +quartz 数据库配置

    spring chm文档

    6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ Load-time weaving(LTW) 6.9. 其它资源 7. Spring AOP APIs 7.1. 简介 7.2. Spring中的切入点API 7.2.1. 概念 7.2.2. 切入点...

    陈开雄 Spring+3.x企业应用开发实战光盘源码.zip

     第4章:讲解如何在Spring配置文件中使用Spring 3.0的Schema格式配置Bean的内容,并对各个配置项的意义进行了深入的说明。  第5章:对Spring容器进行解构,从内部探究Spring容器的体系结构和运行流程。此外,我们...

    Spring中文帮助文档

    6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ加载时织入(LTW) 6.9. 更多资源 7. Spring AOP APIs 7.1. 简介 7.2. Spring中的切入点API 7.2.1. 概念 7.2.2. 切入点运算 ...

    Spring API

    6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ加载时织入(LTW) 6.9. 更多资源 7. Spring AOP APIs 7.1. 简介 7.2. Spring中的切入点API 7.2.1. 概念 7.2.2. 切入点运算 ...

    Spring3.x企业应用开发实战(完整版) part1

    2.2.3 类包及Spring配置文件规划 2.3 持久层 2.3.1 建立领域对象 2.3.2 UserDao 2.3.3 LoginLogDao 2.3.4 在Spring中装配DAO 2.4 业务层 2.4.1 UserService 2.4.2 在Spring中装配Service 2.4.3 单元测试 2.5 展现层 ...

Global site tag (gtag.js) - Google Analytics