Quartz(09) quartz spring web 项目的整合(终极版)

x33g5p2x  于2021-12-21 转载在 其他  
字(4.4k)|赞(0)|评价(0)|浏览(339)

上一节(Quartz(08) quartz spring web 项目的整合(方法二))
这一章我们将采用最简单的一种方式整合quartz spring web. 达到的效果是,我们只需要编写自己的job类,关于job,trigger 的配置信息都存放到数据库.(注:我们公司的的项目就是这么配置的,非常方便)

源码下载地址

1.编写我们自定义的job类Q1,Q2

package com.quartz.job;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

public class Q1 implements Serializable, Job {

    private static final long serialVersionUID = 6890216263057956690L;

    public void execute(JobExecutionContext arg0) throws JobExecutionException {
        System.out.println("------------------------");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd hh:mm:ss");
        System.out.println(sdf.format(new Date()));
        System.out.println("------------------------");
    }

}
package com.quartz.job;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.quartz.Job;
import org.quartz.JobExecutionContext;

public class Q2 implements Serializable, Job {

    private static final long serialVersionUID = 5004730246347558783L;

    public void execute(JobExecutionContext arg0) {
        System.out.println("************************");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd hh:mm:ss");
        System.out.println(sdf.format(new Date()));
        System.out.println("************************");
    }

}
  1. 配置quartz.properties 文件(并无特殊之处)
# Default Properties file for use by StdSchedulerFactory
# to create a Quartz Scheduler Instance, if a different
# properties file is not explicitly specified.
#

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

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

#jobStoreTX TEST
org.quartz.jobStore.misfireThreshold=60000
# TX method
org.quartz.jobStore.class=org.quartz.impl.jdbcjobstore.JobStoreTX
#jdbc Delegate
org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.StdJDBCDelegate
#table prefix
org.quartz.jobStore.tablePrefix=qrtz_
#
org.quartz.jobStore.dataSource=myDS

org.quartz.dataSource.myDS.driver=com.mysql.jdbc.Driver   
org.quartz.dataSource.myDS.URL=jdbc:mysql://localhost:3306/test
org.quartz.dataSource.myDS.user=root   
org.quartz.dataSource.myDS.password=123456   
org.quartz.dataSource.myDS.maxConnections=10
  1. 配置applicationContext.xml 文件
<?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" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">

    <!-- 配置Quartz第三种方式,完全基于数据库,无需配置自定义的job类-->
    <bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <!-- 以下配置实现了job的持久化 (JobStoreTX)-->
        <!-- 事先在数据库里面配置好quartz的信息,然后quartz自动去读取,并实例化 -->
        <!-- 运行资源文件myQuartzTest.sql文件,就配置好了相关的信息三张表 -->
        <property name="configLocation" value="classpath:quartz.properties" />  
       <property name="applicationContextSchedulerContextKey" value="applicationContextKey" />    
       <property name="autoStartup" value="true" /> 
    </bean>
</beans>

那么我们的关于job和trigger的信息到哪里去了? 去了数据库,配置如下:

DELETE from qrtz_cron_triggers;
DELETE from qrtz_triggers;
DELETE from qrtz_fired_triggers;
DELETE from qrtz_job_details;
COMMIT;

INSERT INTO `qrtz_job_details` VALUES ('scheduler', 'myJobDetail1', 'DEFAULT', NULL, 'com.quartz.job.Q1', '1', '0', '0', '0', null);
INSERT INTO `qrtz_job_details` VALUES ('scheduler', 'myJobDetail2', 'DEFAULT', NULL, 'com.quartz.job.Q2', '1', '0', '0', '0', null);
INSERT INTO `qrtz_triggers` VALUES ('scheduler', 'my_job1', 'my_group1', 'myJobDetail1', 'DEFAULT', NULL, 0, 0, 0, 'ACQUIRED', 'CRON', 1470241748000, 0, NULL, 0, null);
INSERT INTO `qrtz_triggers` VALUES ('scheduler', 'my_job1', 'my_group2', 'myJobDetail2', 'DEFAULT', NULL, 0, 0, 0, 'ACQUIRED', 'CRON', 1470241748000, 0, NULL, 0, null);
INSERT INTO `qrtz_cron_triggers` VALUES ('scheduler', 'my_job1', 'my_group1', '0/5 * * * * ?', 'Asia/Shanghai');
INSERT INTO `qrtz_cron_triggers` VALUES ('scheduler', 'my_job1', 'my_group2', '0/5 * * * * ?', 'Asia/Shanghai');
COMMIT;

这样在启动web项目的时候,Quartz就可以正常工作了.

相关文章