---
title: "spring quartz强制使用RAMJobStore解决办法"
author: "notcool"
date: "2009-09-26T19:00:49Z"
tags: []
slug: "spring-quartz-ramjobstore"
draft: false
lastmod: "2009-09-26T19:00:49Z"
id: 478003
url: "https://notcool.iteye.com/blog/478003"
---

spring quartz强制使用RAMJobStore
作者:sunshy 日期:2009-08-13

在 spring里用quartz时，因为quartz调用的是一个业务层的操作，配置文件里有个datesource，这样以来spring默认会给 quartz装载datesource，即quartz被设成了JDBCJobStore模式，这样就出问题了，一方面我需要在数据库中增加十来张表，另一方面当我们的任务不能被序列化的时候，存储数据库的时候会报错！

你说简简单单的RAMJobStore多好，当然如果你的调动任务比较复杂的时候用JDBCJobStore来持久维持状态也有可取之处，这里不讨论RAMJobStore和JDBCJobStore的区别，如感兴趣可看这篇文章。这里我要讨论的就是怎么干掉JDBCJobStore模式。

首先一个尝试，在spring配置中的SchedulerFactoryBean设置：

    <property name="quartzProperties">
                <props>
                    <prop key="org.quartz.threadPool.class">org.quartz.simpl.SimpleThreadPool</prop>
                    <prop key="org.quartz.threadPool.threadCount">1</prop>
                    <!--<prop key="org.quartz.threadPool.threadPriority">4</prop>-->
                    <prop key="org.quartz.jobStore.class">org.quartz.simpl.RAMJobStore</prop>
                    <prop key="org.quartz.jobStore.misfireThreshold">60000</prop>
                </props>
            </property>

测试好像没起作用，经查，发现时spring自动装载在作祟。这里有一篇介绍的文章。我是在spring配置最外层加了default-autowire="byName"属性，当然这个是有用的，因为我类中都用了@autowire自动装载，这个地方不能改。所以只能在SchedulerFactoryBean这个bean里改，这样既不影响全局，也实现了quartz强制使用 RAMJobStore的需求:

    <bean id="cmsScheduler"
              class="org.springframework.scheduling.quartz.SchedulerFactoryBean" autowire="no">
            <property name="triggers">
                <list>
                    <ref bean="simplePublishJobTrigger"/>
                    <ref bean="cronPublishJobTrigger"/>
                </list>
            </property>
            <property name="quartzProperties">
                <props>
                    <prop key="org.quartz.threadPool.class">org.quartz.simpl.SimpleThreadPool</prop>
                    <prop key="org.quartz.threadPool.threadCount">1</prop>
                    <!--<prop key="org.quartz.threadPool.threadPriority">4</prop>-->
                    <prop key="org.quartz.jobStore.class">org.quartz.simpl.RAMJobStore</prop>
                    <prop key="org.quartz.jobStore.misfireThreshold">60000</prop>
                </props>
            </property>
        </bean>