quartz 持久化 數據庫表


此處只包括配置數據庫操作

 


quartz 持久化數據庫表格字段解釋建表,SQL語句在dbTables文件夾中可以找到,介紹下我們開發主要使用到的表: (版本不一樣,可能數據庫表也不一樣,這里使用2.2.1)

1、QRTZ_JOB_DETAILS:存儲的是job的詳細信息,包括:[DESCRIPTION]描述,[IS_DURABLE]是否持久化,[JOB_DATA]持久化對象等基本信息。

2、QRTZ_TRIGGERS:觸發器信息,包含:job的名,組外鍵,[DESCRIPTION]觸發器的描述等基本信息,還有[START_TIME]開始執行時間,[END_TIME]結束執行時間,[PREV_FIRE_TIME]上次執行時間,[NEXT_FIRE_TIME]下次執行時間,[TRIGGER_TYPE]觸發器類型:simple和cron,[TRIGGER_STATE]執行狀態:WAITING,PAUSED,ACQUIRED分別為:等待,暫停,運行中。

3、QRTZ_CRON_TRIGGERS:保存cron表達式。

4、QRTZ_SCHEDULER_STATE:存儲集群中note實例信息,quartz會定時讀取該表的信息判斷集群中每個實例的當前狀態,INSTANCE_NAME:之前配置文件中org.quartz.scheduler.instanceId配置的名字,就會寫入該字段,如果設置為AUTO,quartz會根據物理機名和當前時間產生一個名字。  [LAST_CHECKIN_TIME]上次檢查時間,[CHECKIN_INTERVAL]檢查間隔時間。

5、QRTZ_PAUSED_TRIGGER_GRPS:暫停的任務組信息。

6、QRTZ_LOCKS,悲觀鎖發生的記錄信息。

7、QRTZ_FIRED_TRIGGERS,正在運行的觸發器信息。

8、QRTZ_SIMPLE_TRIGGERS,簡單的出發器詳細信息。

9、QRTZ_BLOB_TRIGGERS,觸發器存為二進制大對象類型(用於Quartz用戶自己觸發數據庫定制自己的觸發器,然而JobStore不明白怎么存放實例的時候)。

 

 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

org.quartz.jobStore.misfireThreshold= 60000

org.quartz.jobStore.class=  org.quartz.impl.jdbcjobstore.JobStoreTX
org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.StdJDBCDelegate
org.quartz.jobStore.tablePrefix = QRTZ_

#不需要整合其他框架數據庫時--------需要整合時,以下備注相反即可 org.quartz.jobStore.dataSource = qzDS org.quartz.dataSource.qzDS.driver= net.sourceforge.jtds.jdbc.Driver org.quartz.dataSource.qzDS.URL = jdbc:jtds:sqlserver://localhost:1433/quartz org.quartz.dataSource.qzDS.user= sa org.quartz.dataSource.qzDS.password= 1234 org.quartz.dataSource.qzDS.maxConnections = 30 #org.quartz.jobStore.selectWithLockSQL=select * from {0}LOCKS UPDLOCK WHERE LOCK_NAME=?

  

整合SpringMVC:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

<!-- 導入數據庫配置 --> <import resource="spring-datasources.xml"/>
   <!-- 配置job可使用springmvc bean --> <bean id="jobFactory" class="com.ice.quartz.factory.JobFactory"></bean>
<bean id="jobRecordListener" class="com.ice.quartz.listener.JobRecordListener" /> <bean id="triggerRecordListener" class="com.ice.quartz.listener.TriggerRecordListener" /> <bean id="DefaultQuartzScheduler" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="dataSource" ref="defaultDataSource"/> <property name="configLocation" value="classpath:quartz.properties" /> <property name="jobFactory" ref="jobFactory"></property> <property name="triggers"> <list> <ref bean="hourTrigger"></ref> <ref bean="dayTrigger"></ref> <ref bean="weekTrigger"></ref> <ref bean="monthTrigger"></ref> <ref bean="quarterTrigger"></ref> <ref bean="yearTrigger"></ref> </list> </property>
     <!-- 使用注入方式使用springMVC bean --> <property name="schedulerContextAsMap"> <map> <entry key="jobTriggerRecordMapper" value-ref="jobTriggerRecordMapper"></entry> </map> </property> <property name="globalJobListeners" ref="jobRecordListener"></property> <property name="globalTriggerListeners" ref="triggerRecordListener"></property> </bean> </beans>

  

配置job可使用springmvc bean 
package com.ice.quartz.factory;

import org.quartz.spi.TriggerFiredBundle;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.scheduling.quartz.SpringBeanJobFactory;

/**
 * @author sky
 * @version 1.0
 * @since 4.0
 */
public class JobFactory extends SpringBeanJobFactory implements ApplicationContextAware {
    private ApplicationContext applicationContext;

    @Override
    protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {
        Object jobInstance = super.createJobInstance(bundle);
        applicationContext.getAutowireCapableBeanFactory().autowireBean(jobInstance);
        return jobInstance;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM