1. 程式人生 > >基於spring+quartz的分散式定時任務框架

基於spring+quartz的分散式定時任務框架

問題背景

   我公司是一個快速發展的創業公司,目前有200人,主要業務是旅遊和酒店相關的,應用迭代更新週期比較快,因此,開發人員花費了更多的時間去更=跟上迭代的步伐,而缺乏了對整個系統的把控

沒有叢集之前,公司定時任務的實現方式

   在初期應用的訪問量並不是那麼大,一臺伺服器完全滿足使用,應用中有很多定時任務需要執行

有了叢集之後,公司定時任務實現的方式

  隨著使用者的增加,訪問量也就隨之增加,一臺伺服器滿足不了高併發的要求,因此公司把應用給部署到叢集中,前端通過nginx代理(應用伺服器ip可能是用防火牆進行了隔離,避免了直接使用ip+埠+應用名訪問的方式)。在叢集環境中,同樣的定時任務,在叢集中的每臺機器都會執行,這樣定時任務就會重複執行,不但會增加伺服器的負擔,還會因為定時任務重複執行造成額外的不可預期的錯誤,因此公司的解決方案是:根據叢集的數量,來把定時任務中的任務平均分到叢集中的每臺機器上(這裡的平均分是指以前一個定時任務本來是在一臺機器上執行,先在人為的把這個任務分成幾部分,讓所有的機器都去執行這個人去)

目前叢集中定時任務實現方式的缺陷

  目前公司在叢集中處理定時任務的方式不是正真的分散式處理方式,而是一種偽分散式(公司內部俗稱土方法),這種方式存在一個明顯的缺陷就是當叢集中機器宕機,那麼整個定時任務就會掛掉或者不能一次性跑完,會對業務產生嚴重的影響

針對缺陷的解決方案(本文的重點之處)

  利用spring+quartz構建一套真正的分散式定時任務系統,經過查閱相關資料得知:quartz框架是原生就支援分散式定時任務的

 開發IDE:Intellij IDEA

JDK版本:1.8

Spring版本:4.2.6

Quartz版本:2.2.1

Spring與Quartz整合配置

複製程式碼
<?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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"
> <context:component-scan base-package="com.aaron.clusterquartz.job"/> <bean name="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> <!-- tomcat --> <!--<property name="jndiName" value="java:comp/env/jndi/mysql/quartz"/>--> <!-- jboss --> <property name="jndiName" value="jdbc/quartz"/> </bean> <!-- 分散式事務配置 start --> <!-- 配置執行緒池--> <bean name="executor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor"> <property name="corePoolSize" value="15"/> <property name="maxPoolSize" value="25"/> <property name="queueCapacity" value="100"/> </bean> <bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <!-- 配置排程任務--> <bean name="quartzScheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="configLocation" value="classpath:quartz.properties"/> <property name="dataSource" ref="dataSource"/> <property name="transactionManager" ref="transactionManager"/> <!-- 任務唯一的名稱,將會持久化到資料庫--> <property name="schedulerName" value="baseScheduler"/> <!-- 每臺叢集機器部署應用的時候會更新觸發器--> <property name="overwriteExistingJobs" value="true"/> <property name="applicationContextSchedulerContextKey" value="appli"/> <property name="jobFactory"> <bean class="com.aaron.clusterquartz.autowired.AutowiringSpringBeanJobFactory"/> </property> <property name="triggers"> <list> <ref bean="printCurrentTimeScheduler"/> </list> </property> <property name="jobDetails"> <list> <ref bean="printCurrentTimeJobs"/> </list> </property> <property name="taskExecutor" ref="executor"/> </bean> <!-- 配置Job詳情 --> <bean name="printCurrentTimeJobs" class="org.springframework.scheduling.quartz.JobDetailFactoryBean"> <property name="jobClass" value="com.aaron.clusterquartz.job.PrintCurrentTimeJobs"/>
    <!--因為我使用了spring的註解,所以這裡可以不用配置scheduler的屬性-->
<!--<property name="jobDataAsMap"> <map> <entry key="clusterQuartz" value="com.aaron.framework.clusterquartz.job.ClusterQuartz"/> </map> </property>--> <property name="durability" value="true"/> <property name="requestsRecovery" value="false"/> </bean> <!-- 配置觸發時間 --> <bean name="printCurrentTimeScheduler" class="com.aaron.clusterquartz.cron.PersistableCronTriggerFactoryBean"> <property name="jobDetail" ref="printCurrentTimeJobs"/> <property name="cronExpression"> <value>0/10 * * * * ?</value> </property> <property name="timeZone"> <value>GMT+8:00</value> </property> </bean> <!-- 分散式事務配置 end --> </beans>
複製程式碼

quartz屬性檔案

複製程式碼
#============================================================================
# Configure JobStore
# Using Spring datasource in quartzJobsConfig.xml
# Spring uses LocalDataSourceJobStore extension of JobStoreCMT
#============================================================================
org.quartz.jobStore.useProperties=true
org.quartz.jobStore.tablePrefix = QRTZ_
org.quartz.jobStore.isClustered = true
org.quartz.jobStore.clusterCheckinInterval = 5000
org.quartz.jobStore.misfireThreshold = 60000
org.quartz.jobStore.txIsolationLevelReadCommitted = true

# Change this to match your DB vendor
org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX
org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.StdJDBCDelegate


#============================================================================
# Configure Main Scheduler Properties
# Needed to manage cluster instances
#============================================================================
org.quartz.scheduler.instanceId=AUTO
org.quartz.scheduler.instanceName=MY_CLUSTERED_JOB_SCHEDULER
org.quartz.scheduler.rmi.export = false
org.quartz.scheduler.rmi.proxy = 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
複製程式碼

相關類說明

AutowiringSpringBeanJobFactory類是為了可以在scheduler中使用spring註解,如果不使用註解,可以不適用該類,而直接使用
SpringBeanJobFactory 
複製程式碼
package com.aaron.clusterquartz.autowired;

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

/**
 * @author 
 * @description 使job類支援spring的自動注入
 * @date 2016-05-27
 */
public class AutowiringSpringBeanJobFactory extends SpringBeanJobFactory implements ApplicationContextAware
{
    private transient AutowireCapableBeanFactory beanFactory;

    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException
    {
        beanFactory = applicationContext.getAutowireCapableBeanFactory();
    }


    @Override
    protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception
    {
        Object job = super.createJobInstance(bundle);
        beanFactory.autowireBean(job);
        return job;
    }
}
複製程式碼 複製程式碼
package com.aaron.clusterquartz.job;

import com.arron.util.DateUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;

import java.util.Date;

/**
 * @author 
 * @description 一句話描述該檔案的用途
 * @date 2016-05-23
 */
public class PrintCurrentTimeJobs extends QuartzJobBean
{
    private static final Log LOG_RECORD = LogFactory.getLog(PrintCurrentTimeJobs.class);
  //這裡就是因為有上文中的AutowiringSpringBeanJobFactory才可以使用@Autowired註解,否則只能在配置檔案中設定這屬性的值
    @Autowired
    private ClusterQuartz clusterQuartz;


    protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException
    {
        LOG_RECORD.info("begin to execute task," + DateUtils.dateToString(new Date()));

        clusterQuartz.printUserInfo();

        LOG_RECORD.info("end to execute task," + DateUtils.dateToString(new Date()));

    }
}
複製程式碼

測試結果:

  由於只有一臺電腦,所有我開了8080和8888兩個埠來測試的,上面的定時任務我設定了每10秒執行一次。

  當只我啟動8080埠時,可以看到控制檯每隔10秒列印一條語句

  兩個埠同時啟動的對比測試中可以看到,只有一個埠在跑定時任務

 

這個關了正在跑定時任務的埠後,之前的另一個沒有跑的埠開始接管,繼續執行定時任務

 

至此,我們可以清楚地看到,在分散式定時任務中(或者叢集),同一時刻只會有一個定時任務執行。

整個demo地址已上傳git:[email protected]:AaronFeng2014/spring-cluster-quartz.git