spring集成quartz持久化简单应用


最近在做定时跑批任务的时候用开始接触quartz,写了一个简单的demo,由于经验不足,原理还没有深入理解,有什么不对之处还请各位博友加以指正

首先,quartz在与spring集成是是有版本匹配问题的:quartz-1.x与spring3.1之前的版本对应,而quartz2.x与spring3.1之后的版本对应,quartz1和quartz2是有很大区别的。
如下是quartz1.8.6数据库表结构和quartz2.2.1数据库表结构,根据需求构建数据库,见附件
集成spring与quartz:
一、spring3.1之前版本和quartz1.x集成
1、pom依赖(spring和quartz版本自行匹配)
<properties>
    <spring.version>3.2.9.RELEASE</spring.version>
    <!--<spring.version>3.0.6.RELEASE</spring.version>-->
    <!--<spring.version>3.2.2.RELEASE</spring.version>-->
    <!--<spring.version>4.0.0.RELEASE</spring.version>-->
    <quartz.version>2.2.1</quartz.version>
    <!--<quartz.version>1.8.6</quartz.version>-->
</properties>


<dependencies>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context-support</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>${spring.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>${spring.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>${spring.version}</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.11</version>
        <scope>test</scope>
    </dependency>

    <!-- quartz -->
    <dependency>
        <groupId>org.quartz-scheduler</groupId>
        <artifactId>quartz</artifactId>
        <version>${quartz.version}</version>
    </dependency>

    <!-- mysql begin  -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.30</version>
    </dependency>
    <!-- end of mysql -->
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid</artifactId>
        <version>1.0.16</version>
    </dependency>

</dependencies>
 2、定义job,实现序列化(内存方式不必实现):(quartz1.x版本)
public class HelloJob implements Serializable{

    private static final long serialVersionUID = -2348136105841790133L;

    public void execute(){
        System.out.println("============================");
    }
}
 
3、spring配置:
3.1 数据源配置(内存方式忽略此配置)
jdbc.driverClassName=com.mysql.jdbc.Driver
#quartz1.x数据库
#jdbc.url=jdbc:mysql://127.0.0.1:3306/quartz-test?useUnicode=true
#quartz2.x数据库
jdbc.url=jdbc:mysql://127.0.0.1:3306/quartz2?useUnicode=true
jdbc.username=root
jdbc.password=maidou
jdbc.initialSize=15
jdbc.maxActive=20
jdbc.maxIdle=20
jdbc.minIdle=5
 
<context:property-placeholder location="classpath:properties/*.properties"/>

<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
      destroy-method="close">
    <property name="driverClassName" value="${jdbc.driverClassName}" />
    <property name="url" value="${jdbc.url}" />
    <property name="username" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" />
    <!--initialSize: 初始化连接 -->
    <property name="initialSize" value="${jdbc.initialSize}" />
    <!--maxActive: 最大连接数量 -->
    <property name="maxActive" value="${jdbc.maxActive}" />
    <!--maxIdle: 最大空闲连接 -->
    <!--<property name="maxIdle" value="${jdbc.maxIdle}" />-->
    <!--minIdle: 最小空闲连接 -->
    <property name="minIdle" value="${jdbc.minIdle}" />
    <property name="defaultAutoCommit" value="true" /><!--这里一定要设置为true,否则在启动项目quartz事务不会自动提交,导致job不能持久化到数据库,抛出异常,正常的程序数据源此项要设置为false,自行控制事务-->

    <property name="validationQuery" value="SELECT 1 " />
</bean>
 3.2 quartz配置(quartz1.x):其中jobDetail bean配置的class 需要自己实现序列化,spring 的MethodInvokingJobDetailFactoryBean没有实现序列化,故需要如下两个java文件,见附件
<!-- 调度器 -->
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
     <property name="dataSource" ref="dataSource" />
    <property name="configLocation" value="classpath:properties/quartz.properties" />
    <property name="autoStartup" value="true" />
    <property name="applicationContextSchedulerContextKey" value="applicationContext" />
    <property name="startupDelay" value="20" />
    <property name="overwriteExistingJobs" value="true" />
    <property name="triggers">
        <list>
            <!-- 触发器列表 -->
            <ref bean="helloJobTrigger" />
        </list>
    </property>
</bean>

<bean id="helloJobBean" class="com.maidou.job.HelloJob"/>

<!-- 触发器 -->
<bean id="helloJobTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
    <property name="jobDetail" ref="helloJobDetail" />
    <property name="cronExpression" value="0/5 * * * * ?" />
</bean>

<bean id="helloJobDetail"
        class="com.maidou.factory.MethodInvokingJobDetailFactoryBean">
    <property name="targetObject" ref="helloJobBean" />
    <property name="targetMethod" value="execute" />
    <property name="concurrent" value="false" />
    <property name="durable" value="true"/>
    <!--<property name="volatility" value="true"/>-->
    <property name="shouldRecover" value="true" />
</bean>

<import resource="spring-db.xml"/>
 4、写quartz.properties覆盖原有quartz.properties;如下
#============================================================================
# Configure Main Scheduler Properties
#============================================================================

org.quartz.scheduler.instanceName = MY_JOB_SCHEDULER
org.quartz.scheduler.instanceId = AUTO

org.quartz.scheduler.rmi.export = false
org.quartz.scheduler.rmi.proxy = false
org.quartz.scheduler.wrapJobExecutionInUserTransaction = false


#============================================================================
# Configure ThreadPool
#============================================================================

org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount = 10
org.quartz.threadPool.threadPriority = 5
org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread = true

#============================================================================
# Configure JobStore
#============================================================================

org.quartz.jobStore.misfireThreshold = 60000
#内存方式
#org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore
#数据库持久化
org.quartz.jobStore.class=org.quartz.impl.jdbcjobstore.JobStoreTX
org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.StdJDBCDelegate
org.quartz.jobStore.useProperties=false
org.quartz.jobStore.maxMisfiresToHandleAtATime=1
#数据库表名称前缀
org.quartz.jobStore.tablePrefix=QRTZ_
#集群
org.quartz.jobStore.isClustered=false

#============================================================================
# Other Example Delegates
#============================================================================
#org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.CloudscapeDelegate
#org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.DB2v6Delegate
#org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.DB2v7Delegate
#org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.DriverDelegate
#org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.HSQLDBDelegate
#org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.MSSQLDelegate
#org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.PointbaseDelegate
#org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.PostgreSQLDelegate
#org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.StdJDBCDelegate
#org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.WebLogicDelegate
#org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.oracle.OracleDelegate
#org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.oracle.WebLogicOracleDelegate

#============================================================================
# Configure Datasources
#============================================================================

#org.quartz.dataSource.myDS.driver = com.mysql.jdbc.Driver
#org.quartz.dataSource.myDS.URL = jdbc:mysql://10.1.221.118/invest?useUnicode=true&amp;characterEncoding=utf-8
#org.quartz.dataSource.myDS.user = invest
#org.quartz.dataSource.myDS.password = invest
#org.quartz.dataSource.myDS.maxConnections = 5
#org.quartz.dataSource.myDS.validationQuery=

#============================================================================
# Configure Plugins
#============================================================================

#org.quartz.plugin.shutdownHook.class = org.quartz.plugins.management.ShutdownHookPlugin
#org.quartz.plugin.shutdownHook.cleanShutdown = true


#org.quartz.plugin.triggHistory.class = org.quartz.plugins.history.LoggingJobHistoryPlugin
 
二、spring3.1之后版本和quartz2.x集成
1、pom文件依赖如上pom依赖(自行匹配spring和quartz版本)
2、定义job,继承 QuartzJobBean:(quartz2.x版本)
public class HelloJob extends QuartzJobBean {
    @Override
    protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
        System.out.println("============================");
    }
}
 
3、spring配置:
3.1 数据源配置如上数据源配置
3.2 quartz配置(quartz2.x)配置,可以自定义Listener监听Job和Trigger的执行,分别实现JobListener和TriggerListener接口,如下,见附件
<!--<bean id="myJobListener" class="com.maidou.listener.MyJobListener"/>-->
<!--<bean id="myTriggerListener" class="com.maidou.listener.MyTriggerListener"/>-->

<!-- 调度器 -->
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
     <property name="dataSource" ref="dataSource" />
    <property name="configLocation" value="classpath:properties/quartz.properties" />
    <property name="autoStartup" value="true" />
    <property name="applicationContextSchedulerContextKey" value="applicationContext" />
    <property name="startupDelay" value="20" />
    <property name="overwriteExistingJobs" value="true" />
    <!-- 可以实现自定义的listener,监听JobListener和TriggerListener,同时自定义的也需要分别实现这两个接口-->
    <!--<property name="globalJobListeners" ref="myJobListener"/>-->
    <!--<property name="globalTriggerListeners" ref="myTriggerListener"/>-->

    <property name="triggers">
        <list>
            <!-- 触发器列表 -->
            <ref bean="helloJobTrigger" />
        </list>
    </property>
</bean>

<bean id="helloJobBean" class="com.maidou.job.HelloJob"/>

<!-- 触发器 -->
<bean id="helloJobTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
    <property name="jobDetail" ref="helloJobDetail" />
    <property name="cronExpression" value="0/5 * * * * ?" />
    <!--<property name="group" value="group1"/>-->
    <!--<property name="name" value="helloJobTrigger"/>-->
</bean>

<bean id="helloJobDetail"
        class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
    <property name="jobClass" value="com.maidou.job.HelloJob"/>
    <property name="group" value="group1"/>
    <property name="name" value="helloJobDetail"/>
    <property name="durability" value="true"/>
</bean>

<import resource="spring-db.xml"/>
 
4、写quartz.properties覆盖原有quartz.properties;同上
注意:数据源的配置一定要配置成自动提交,默认quartz用的数据库连接池是c3p0,我用的是alibaba的druid。
 
测试一下
编写测试类:
public class test {
    private static volatile boolean running = true;
    public static void main(String[] args) {
        ClassPathXmlApplicationContext cxt = new ClassPathXmlApplicationContext("classpath:application-context.xml");
        synchronized (test.class) {
            while (running) {
                try {
                    test.class.wait();
                } catch (Throwable e) {
                }
            }
        }
    }
}
 欢迎留言或到我的csdn博客讨论:http://blog.csdn.net/lingyundouer/article/details/50513194

相关推荐