ITeye博客

疑问:Spring配置Quartz例子出错,请看下.谢谢

moogle · 2006-12-13 14:35 · Java · 企业架构 · Markdown

研究了一天,在官方论坛也查询了很多,就是锁定不了原因。请各位朋友帮忙看看 spring的配置如下:


    <bean id="emailJobBean" class="org.springframework.scheduling.quartz.JobDetailBean">
    	<property name="jobClass" value="EmailJobBean"/>
    </bean>
	<bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
	    <property name="jobDetail" ref="emailJobBean" />
	    <property name="startDelay" value="2000" />
	    <property name="repeatInterval" value="60000" />
	</bean>
	<bean name="quartzScheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
           <list>
           		<ref bean="simpleTrigger"/>
            </list>
        </property>
    </bean>  

其中被调度的bean


public class EmailJobBean extends QuartzJobBean {

	protected void executeInternal(JobExecutionContext arg0)
			throws JobExecutionException {
		System.out.println("Quartz !!!");
	}

}

启动后就保错

14:29:38,11882 ERROR Context initialization failed org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'quartzScheduler' defined in ServletContext resource : Invocation of init method failed; nested exception is org.quartz.SchedulerConfigException: Failure occured during job recovery. 不知道有没有朋友遇到过类似问题?请大家指点 环境我用的是resin2.1.16,jdk1.4.2,spring2.0.1,quartz1.6.0

评论(15)

  1. #15 楼 · eyejava · 2007-03-08 17:50

    dwangel writes:

    另外可能是自动装配惹得祸,存在dataSource这个bean就自动用数据库的状态维持了。

    dwangel writes:

    [quote=foxty]因为这个时候你的quartz是jobstore用的HDBCJobStore模式,此时会从数据库查询任务。

    你如果只是测试的话,可以在你的classpath下加一个文件quartz.properties,并且加上一句org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore 。就会采取内存存储的模式了。[/quote] 或者可以直接在spring配置中做:

    org.quartz.simpl.RAMJobStore

    autowire真是一个万恶的家伙, 这个问题整了我一个下午,怒了. :twisted: 设置org.quartz.jobStore.class为org.quartz.simpl.RAMJobStore也是没有用的. 因为

    
    if (this.dataSource != null) {
    				mergedProps.put(StdSchedulerFactory.PROP_JOB_STORE_CLASS, LocalDataSourceJobStore.class.getName());
    			}
    
    

    SchedulerFactoryBean的setDataSource(DataSource)方法javadoc说: Set the default DataSource to be used by the Scheduler. If set, this will override corresponding settings in Quartz properties.

    Note: If this is set, the Quartz settings should not define a job store "dataSource" to avoid meaningless double configuration.

    A Spring-specific subclass of Quartz' JobStoreCMT will be used. It is therefore strongly recommended to perform all operations on the Scheduler within Spring-managed (or plain JTA) transactions. Else, database locking will not properly work and might even break (e.g. if trying to obtain a lock on Oracle without a transaction).

    Supports both transactional and non-transactional DataSource access. With a non-XA DataSource and local Spring transactions, a single DataSource argument is sufficient. In case of an XA DataSource and global JTA transactions, SchedulerFactoryBean's "nonTransactionalDataSource" property should be set, passing in a non-XA DataSource that will not participate in global transactions.

    See Also: setNonTransactionalDataSource setQuartzProperties setTransactionManager LocalDataSourceJobStore

  2. #14 楼 · dwangel · 2006-12-18 12:15

    foxty writes:

    因为这个时候你的quartz是jobstore用的HDBCJobStore模式,此时会从数据库查询任务。

    你如果只是测试的话,可以在你的classpath下加一个文件quartz.properties,并且加上一句org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore 。就会采取内存存储的模式了。

    或者可以直接在spring配置中做:

    <bean name="quartzScheduler"
    	class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
    	<property name="triggers">
    		<list>
    			<ref bean="simpleTrigger" />
    		</list>
    	</property>
    	<property name="quartzProperties">
    		<props>
    			<prop key="org.quartz.jobStore.class">
    				org.quartz.simpl.RAMJobStore
    			</prop>
    		</props>
    	</property>
    </bean>
    
  3. #13 楼 · dwangel · 2006-12-18 11:57

    按照错误信息显示,你使用的quartz应该是使用数据进行job的状态的维护的,但是在数据库中没有找到相应的table,从而无法正常建立scheduler 。

    如果你的不需要错误重启功能,改成用内存维护就好。

    另外可能是自动装配惹得祸,存在dataSource这个bean就自动用数据库的状态维持了。

  4. #12 楼 · foxty · 2006-12-18 10:45

    因为这个时候你的quartz是jobstore用的HDBCJobStore模式,此时会从数据库查询任务。

    你如果只是测试的话,可以在你的classpath下加一个文件quartz.properties,并且加上一句org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore 。就会采取内存存储的模式了。

  5. #11 楼 · 抛出异常的爱 · 2006-12-18 10:05
    <?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 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>
    

    看了你的问题周日自己试着作了一次....

    14:29:38,11882 ERROR [org.springframework.web.context.contextloader.initwebapplicationcontext(contextloader.java:205)] Context initialization failed
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'quartzScheduler' defined in ServletContext resource [/web-inf/classes/spring-quartz.xml]: Invocation of init method failed; nested exception is org.quartz.SchedulerConfigException: Failure occured during job recovery. [see nested exception: org.quartz.impl.jdbcjobstore.lockexception: failure obtaining db row lock: ora-00942: [color=red]table or view does not exist[/color]

    从没出过这种错..... 看来好像是你又以什么方式去调用数据库之后产生的问题 PS:你的EmailJobBean生明方式不对应该以小写开头,并用Bean标签声明...

  6. #10 楼 · 抛出异常的爱 · 2006-12-14 11:20

    init method failed?是否是你的Quartz实现的接口不对? jobDataAsMap这个接口没看到配? 有没有关系?

  7. #9 楼 · moogle · 2006-12-14 10:33

    现在就是我的hibernate和quartz的配置不能并存 , 这2者之间不会有啥不兼容吧。。。

  8. #8 楼 · moogle · 2006-12-13 16:05

    ddandyy writes:

    很奇怪

    你没定义 id=EmailJobBean 为什么这句不报错呢

    赫赫 ,这个地方就是那个jobClass的值 就是一个类的完整名称, 我这个类没有包名,所以直接就是这么写的。

  9. #7 楼 · moogle · 2006-12-13 15:57
  10. #6 楼 · ddandyy · 2006-12-13 15:37

    很奇怪

    你没定义 id=EmailJobBean 为什么这句不报错呢

  11. #5 楼 · moogle · 2006-12-13 15:21

    ... 我只给出了和quartz相关的,其他的就还有一个hibernate的配置 然后就没有了。 去掉quartz的都很正常的跑。这个quartz跑得和hibernate一点关系都没有

    总共分了3个配置 spring.xml

    
    <beans default-autowire="byName">
    	<!-- 加载全局配置文件 -->
    	<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    		<property name="locations">
    			<list>
    				<value>classpath:/mail.properties</value>
    				<value>classpath:/jdbc.properties</value>
    				<value>classpath:/hibernate.properties</value>
    			</list>
    		</property>
    	</bean>
    </beans>
    
    

    spring-hibernate.xml就是配置了数据源和dao,service spring-quartz.xml就是一楼的那些内容

  12. #4 楼 · binker · 2006-12-13 15:18
  13. #3 楼 · ddandyy · 2006-12-13 15:06

    那就是配置文件有问题

    你给出来的不全吧

  14. #2 楼 · moogle · 2006-12-13 14:56

    public class EmailJobBean implements StatefulJob

    测试后,错误依旧。 @-@.

    PS:感谢回帖。

  15. #1 楼 · ddandyy · 2006-12-13 14:46

    public class EmailJobBean implements StatefulJob

    你试试

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