1. 程式人生 > >Quartz(09) quartz spring web 專案的整合(終極版)

Quartz(09) quartz spring web 專案的整合(終極版)

上一節(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就可以正常工作了.