基于spring+quartz的分布式定時(shí)任務(wù)框架實(shí)現(xiàn)
問題背景
我公司是一個(gè)快速發(fā)展的創(chuàng)業(yè)公司,目前有200人,主要業(yè)務(wù)是旅游和酒店相關(guān)的,應(yīng)用迭代更新周期比較快,因此,開發(fā)人員花費(fèi)了更多的時(shí)間去更=跟上迭代的步伐,而缺乏了對(duì)整個(gè)系統(tǒng)的把控
沒有集群之前,公司定時(shí)任務(wù)的實(shí)現(xiàn)方式
在初期應(yīng)用的訪問量并不是那么大,一臺(tái)服務(wù)器完全滿足使用,應(yīng)用中有很多定時(shí)任務(wù)需要執(zhí)行
有了集群之后,公司定時(shí)任務(wù)實(shí)現(xiàn)的方式
隨著用戶的增加,訪問量也就隨之增加,一臺(tái)服務(wù)器滿足不了高并發(fā)的要求,因此公司把應(yīng)用給部署到集群中,前端通過nginx代理(應(yīng)用服務(wù)器ip可能是用防火墻進(jìn)行了隔離,避免了直接使用ip+端口+應(yīng)用名訪問的方式)。
在集群環(huán)境中,同樣的定時(shí)任務(wù),在集群中的每臺(tái)機(jī)器都會(huì)執(zhí)行,這樣定時(shí)任務(wù)就會(huì)重復(fù)執(zhí)行,不但會(huì)增加服務(wù)器的負(fù)擔(dān),還會(huì)因?yàn)槎〞r(shí)任務(wù)重復(fù)執(zhí)行造成額外的不可預(yù)期的錯(cuò)誤,因此公司的解決方案是:根據(jù)集群的數(shù)量,來把定時(shí)任務(wù)中的任務(wù)平均分到集群中的每臺(tái)機(jī)器上(這里的平均分是指以前一個(gè)定時(shí)任務(wù)本來是在一臺(tái)機(jī)器上運(yùn)行,先在人為的把這個(gè)任務(wù)分成幾部分,讓所有的機(jī)器都去執(zhí)行這個(gè)人去)
目前集群中定時(shí)任務(wù)實(shí)現(xiàn)方式的缺陷
目前公司在集群中處理定時(shí)任務(wù)的方式不是正真的分布式處理方式,而是一種偽分布式(公司內(nèi)部俗稱土方法),這種方式存在一個(gè)明顯的缺陷就是當(dāng)集群中機(jī)器宕機(jī),那么整個(gè)定時(shí)任務(wù)就會(huì)掛掉或者不能一次性跑完,會(huì)對(duì)業(yè)務(wù)產(chǎn)生嚴(yán)重的影響
針對(duì)缺陷的解決方案(本文的重點(diǎn)之處)
利用spring+quartz構(gòu)建一套真正的分布式定時(shí)任務(wù)系統(tǒng),經(jīng)過查閱相關(guān)資料得知:quartz框架是原生就支持分布式定時(shí)任務(wù)的
開發(fā)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>
<!-- 分布式事務(wù)配置 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>
<!-- 配置調(diào)度任務(wù)-->
<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"/>
<!-- 任務(wù)唯一的名稱,將會(huì)持久化到數(shù)據(jù)庫-->
<property name="schedulerName" value="baseScheduler"/>
<!-- 每臺(tái)集群機(jī)器部署應(yīng)用的時(shí)候會(huì)更新觸發(fā)器-->
<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"/>
<!--因?yàn)槲沂褂昧藄pring的注解,所以這里可以不用配置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>
<!-- 配置觸發(fā)時(shí)間 -->
<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>
<!-- 分布式事務(wù)配置 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
相關(guān)類說明
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的自動(dòng)注入
* @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);
//這里就是因?yàn)橛猩衔闹械腁utowiringSpringBeanJobFactory才可以使用@Autowired注解,否則只能在配置文件中設(shè)置這屬性的值
@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()));
}
}
測(cè)試結(jié)果:
由于只有一臺(tái)電腦,所有我開了8080和8888兩個(gè)端口來測(cè)試的,上面的定時(shí)任務(wù)我設(shè)置了每10秒運(yùn)行一次。
當(dāng)只我啟動(dòng)8080端口時(shí),可以看到控制臺(tái)每隔10秒打印一條語句

兩個(gè)端口同時(shí)啟動(dòng)的對(duì)比測(cè)試中可以看到,只有一個(gè)端口在跑定時(shí)任務(wù)
這個(gè)關(guān)了正在跑定時(shí)任務(wù)的端口后,之前的另一個(gè)沒有跑的端口開始接管,繼續(xù)運(yùn)行定時(shí)任務(wù)

至此,我們可以清楚地看到,在分布式定時(shí)任務(wù)中(或者集群),同一時(shí)刻只會(huì)有一個(gè)定時(shí)任務(wù)運(yùn)行。
整個(gè)demo地址:http://xiazai.jb51.net/201701/yuanma/spring-cluster-quartz_jb51.rar
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
mybatis中映射文件include標(biāo)簽的應(yīng)用
這篇文章主要介紹了mybatis中映射文件include標(biāo)簽的應(yīng)用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11
idea中將單個(gè)java類導(dǎo)出為jar包文件的方法
這篇文章主要給大家介紹了關(guān)于idea中將單個(gè)java類導(dǎo)出為jar包文件的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-09-09
SpringBoot使用ip2region獲取地理位置信息的方法
這篇文章主要介紹了SpringBoot使用ip2region獲取地理位置信息的相關(guān)知識(shí),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06
Java class文件格式之方法_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要為大家詳細(xì)介紹了Java class文件格式之方法的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06
如何在maven本地倉庫中添加oracle的jdbc驅(qū)動(dòng)
文章介紹了在Maven項(xiàng)目中添加Oracle數(shù)據(jù)庫驅(qū)動(dòng)ojdbc5時(shí)遇到的問題以及解決問題的兩種方法,方法一為簡單粗暴,但沒有體現(xiàn)Maven倉庫的作用,需要手動(dòng)管理jar包,方法二為在Maven本地倉庫中添加Oracle的JDBC驅(qū)動(dòng),過程較為繁瑣,但配置一次后可以多次使用2024-11-11

