美文网首页
Spring定时触发器(quartz)的问题

Spring定时触发器(quartz)的问题

作者: jarWorker | 来源:发表于2018-08-29 17:47 被阅读0次

    Spring配置

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
        xmlns:task="http://www.springframework.org/schema/task" xmlns:mvc="http://www.springframework.org/schema/mvc"
        xsi:schemaLocation="
              http://www.springframework.org/schema/beans
              http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
              http://www.springframework.org/schema/tx
              http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
              http://www.springframework.org/schema/context
              http://www.springframework.org/schema/context/spring-context-3.0.xsd
              http://www.springframework.org/schema/aop
              http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
              http://www.springframework.org/schema/task 
              http://www.springframework.org/schema/task/spring-task-3.0.xsd
              http://www.springframework.org/schema/mvc  
              http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd" default-autowire="byName"><!-- 自动装配default-autowire="byName"按名称 -->
    
    
    <bean id="fistJob"  class="com.startsi.hnapply.controller.HZStartThread"/>
        
        <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"  >
            
            <property name="autoStartup">
                <value>true</value> 
            </property>
            <property name="triggers">
                <list>
                    <ref bean="cronTriggerBean" />
                </list>
            </property>
        </bean>
        
        <bean  id="cronTriggerBean" class="org.springframework.scheduling.quartz.CronTriggerBean" >
            <property name="jobDetail">
                <ref bean="jobDetailBean" />
            </property>
            <property name="cronExpression">
                      <!--每5秒目标方法触发一次-->
                <value>0/5 * * * * ?</value>
            </property>
        </bean> 
        
        <bean  id="jobDetailBean" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
            <property name="targetObject" ref="fistJob"></property>     
            <property name="targetMethod" value="hzzxThread"></property>
            <property name="concurrent" value="false" />
        </bean>
    
    

    tomcat报错

    严重: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.scheduling.quartz.SchedulerFactoryBean#0' defined in class path resource [applicationContext.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: 表或视图不存在
    [See nested exception: java.sql.SQLException: ORA-00942: 表或视图不存在]]

    错误原因

    原因:其实这都是Spring的自动装配惹得祸,当存在dataSource的bean,在初始化SchedulerFactoryBean时就自动装配该dataSource属性,此时就启用了jobstore的LocalDataSourceJobStore模式使用数据库来维持状态,quartz的jobstore会从数据库中查询任务。quartz使用数据库进行job状态的维护,但是在数据库中没有找到相应的table,从而无法正常初始化scheduler 。

    解决方案

        <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"  autowire="no" >
            
            <property name="autoStartup">
                <value>true</value> 
            </property>
            <property name="triggers">
                <list>
                    <ref bean="cronTriggerBean" />
                </list>
            </property>
        </bean>
    

    屏蔽自动装配:autowire="no"

    总结说明

    自动装配就是指由Spring来自动地注入依赖对象,无需人工参与。目前Spring3.0支持“no”、“byName ”、“byType”、“constructor”四种自动装配,默认是“no”指不支持自动装配的,其中Spring3.0已不推荐使用之前版本的“autodetect”自动装配,推荐使用@Autowired注解方式代替;如果想支持“autodetect”自动装配,请将schema改为“spring-beans-2.5.xsd”。

    参考链接

    相关文章

      网友评论

          本文标题:Spring定时触发器(quartz)的问题

          本文链接:https://www.haomeiwen.com/subject/zzukwftx.html