您的位置:首页 > 编程语言 > Java开发

在Spring注解驱动配置下使用Quartz调度有问题,大家帮忙看看

2015-08-19 16:35 405 查看

http://www.iteye.com/problems/30872

最近一直在使用JSF+Hibernate+Spring的配置进行项目二期的重构与开发,并采用了注解的方式,确实减少了很大的工作量。 

但目前碰到一个问题,就是需要在其中使用调度程序,每天执行一些调度处理。 

之前一期项目中使用了XML配置方式,调度能正常运行,而现在采用注解后就不行了。 

代码如下: 

Java代码  


/** 

 * 报表调度器 

 *  

 * @author angeltears 2009-11-22 

 *  

 */  

public class ReportScheduler {  

    protected Logger logger = LoggerFactory.getLogger(ReportScheduler.class);  

  

    private static final int SIZE_PER_TIME = 5000;  

  

    private ReportCatalogBS reportCatalogBS;  

  

    private RptModifyIntBS rptModifyIntBS;  

  

    public void doFromModify2MakeJob() {  

             ......  

         }  

  

         public void doSummaryJob() {  

             ......  

         }  

}  

此类用于执行调度,方法doFromModify2MakeJob和doSummaryJob都是调度执行的方法。它们在不同的时间点分别执行。 

XML配置文件如下: 

Java代码  


<?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:p="http://www.springframework.org/schema/p"  

    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"  

    xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"  

    xsi:schemaLocation="  

            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd  

            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  

            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd  

            http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd  

            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"  

    default-lazy-init="true">  

    <!-- import resource="securityContext.xml" / -->  

    <!-- 定义受环境影响易变的变量 -->  

    <bean  

        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  

        <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />  

        <property name="ignoreResourceNotFound" value="true" />  

        <property name="locations">  

            <list>  

                <!-- 标准配置 -->  

                <value>classpath*:/application.properties  

                </value>  

                <!-- 本地开发环境配置 -->  

                <!-- value>classpath*:/application.local.properties</value -->  

                <!-- 服务器生产环境配置 -->  

                <!-- <value>file:/var/myapp/application.server.properties</value> -->  

            </list>  

        </property>  

    </bean>  

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"  

        p:driverClassName="${jdbc.driverClassName}" p:url="${jdbc.url}"  

        p:username="${jdbc.username}" p:password="${jdbc.password}"  

        p:initialSize="5" p:maxActive="100" p:maxIdle="30" p:maxWait="1000"  

        p:poolPreparedStatements="true" p:defaultAutoCommit="false" />  

  

    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">  

        <property name="dataSource" ref="dataSource" />  

    </bean>  

  

    <bean id="csvServiceDao" class="com.fenet.gddb.cds.dao.csv.CsvServiceDao">  

        <property name="jdbcTemplate" ref="jdbcTemplate" />  

    </bean>  

  

    <!-- 数据源配置,使用应用服务器的数据库连接池 -->  

    <!--  

        <jee:jndi-lookup id="dataSource"  

        jndi-name="java:comp/env/jdbc/ExampleDB" />  

    -->  

  

    <!-- Hibernate配置 -->  

    <bean id="sessionFactory"  

        class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"  

        p:dataSource-ref="dataSource">  

        <property name="namingStrategy">  

            <bean class="org.hibernate.cfg.ImprovedNamingStrategy" />  

        </property>  

        <property name="hibernateProperties">  

            <props>  

                <prop key="hibernate.dialect">${hibernate.dialect}</prop>  

                <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>  

                <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>  

                <prop key="hibernate.generate_statistics">${hibernate.generate_statistics}  

                </prop>  

                <prop key="hibernate.cache.use_query_cache">  

                    ${hibernate.cache.use_query_cache}  

                </prop>  

                <prop key="hibernate.cache.use_second_level_cache">  

                    ${hibernate.cache.use_second_level_cache}  

                </prop>  

                <prop key="hibernate.use_sql_comments">${hibernate.use_sql_comments}  

                </prop>  

                <prop key="hibernate.jdbc.fetch_size">${hibernate.jdbc.fetch_size}  

                </prop>  

                <prop key="hibernate.jdbc.batch_size">${hibernate.jdbc.batch_size}  

                </prop>  

                <prop key="hibernate.query.factory_class">  

                    ${hibernate.query.factory_class}  

                </prop>  

                <prop key="hibernate.cache.provider_class">  

                    ${hibernate.cache.provider_class}  

                </prop>  

                <prop key="hibernate.cache.provider_configuration_file_resource_path">${hibernate.ehcache_config_file}  

                </prop>  

            </props>  

        </property>  

        <property name="packagesToScan"  

            value="com.fenet.gddb.cds.model.*,com.fenet.gddb.cds.model.*.*,com.fenet.gddb.cds.model.*.*.*" />  

    </bean>  

  

    <!-- 事务管理器配置,单数据源事务 -->  

    <bean id="transactionManager"  

        class="org.springframework.orm.hibernate3.HibernateTransactionManager">  

        <property name="sessionFactory" ref="sessionFactory" />  

    </bean>  

  

    <!-- 事务管理器配置,多数据源JTA事务-->  

    <!--  

        <bean id="transactionManager"  

        class="org.springframework.transaction.jta.JtaTransactionManager or  

        WebLogicJtaTransactionManager" />  

    -->  

  

    <context:component-scan base-package="com.fenet.gddb.cds" />  

    <context:annotation-config />  

    <tx:annotation-driven transaction-manager="transactionManager" />  

  

    <bean id="reportScheduler" class="com.fenet.gddb.cds.scheduler.ReportScheduler">  

        <property name="reportCatalogBS" ref="reportCatalogBS" />  

        <property name="rptModifyIntBS" ref="rptModifyIntBS" />  

    </bean>  

    <bean id="summaryJobDetail"  

        class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">  

        <property name="targetObject" ref="reportScheduler" />  

        <property name="targetMethod" value="doSummaryJob" />  

    </bean>  

    <bean id="summaryJobCronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">  

        <property name="jobDetail" ref="summaryJobDetail" />  

        <!-- 每天18点10分到20点执行,每隔1分钟执行一次 -->  

        <property name="cronExpression" value="0 10/1 18,20 * * ?" />  

        <!-- 每月的最后一天18点10分开始到20点之间,每隔1分钟运行一次的执行 -->  

        <!--property name="cronExpression" value="0 10/1 18,20 L * ?" /-->  

    </bean>  

    <bean id="modify2MakeJobDetail"  

        class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">  

        <property name="targetObject" ref="reportScheduler" />  

        <property name="targetMethod" value="doFromModify2MakeJob" />  

    </bean>  

    <bean id="modify2MakeTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">  

        <property name="jobDetail" ref="modify2MakeJobDetail" />  

        <!-- 每天0点至6点执行,每隔30分钟运行一次 -->  

        <property name="cronExpression" value="0 10/5 18,20 * * ?" />  

    </bean>  

    <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">  

        <property name="triggers">  

            <list>  

                <ref bean="modify2MakeTrigger" />  

                <ref bean="summaryJobCronTrigger" />  

            </list>  

        </property>  

    </bean>  

</beans>  

Web.xml中的ContextLoaderListener和contextConfigLocation都配置了,但在指定的时间内,却一点反应也没有,请各路英雄解救下!谢谢了
问题补充:

我明天试试,谢谢! 

另外,spring封装quartz类SchedulerFactoryBean不能使用惰加载,和<bean  class ="org.springframework.scheduling.quartz.SchedulerFactoryBean" 

lazy-init="true" > 是冲突的吧?不能使用惰加载不是应该设置为lazy-init="false"吗 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: