欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

基于spring+quartz的分布式定時(shí)任務(wù)框架實(shí)現(xiàn)

 更新時(shí)間:2017年01月12日 15:34:06   作者:阿Xin  
在Spring中的定時(shí)任務(wù)功能,最好的辦法當(dāng)然是使用Quartz來實(shí)現(xiàn)。這篇文章主要介紹了基于spring+quartz的分布式定時(shí)任務(wù)框架實(shí)現(xiàn),有興趣的可以了解一下。

問題背景

我公司是一個(gè)快速發(fā)展的創(chuàng)業(yè)公司,目前有200人,主要業(yè)務(wù)是旅游和酒店相關(guān)的,應(yīng)用迭代更新周期比較快,因此,開發(fā)人員花費(fèi)了更多的時(shí)間去更=跟上迭代的步伐,而缺乏了對整個(gè)系統(tǒng)的把控

沒有集群之前,公司定時(shí)任務(wù)的實(shí)現(xiàn)方式

在初期應(yīng)用的訪問量并不是那么大,一臺服務(wù)器完全滿足使用,應(yīng)用中有很多定時(shí)任務(wù)需要執(zhí)行

有了集群之后,公司定時(shí)任務(wù)實(shí)現(xiàn)的方式

隨著用戶的增加,訪問量也就隨之增加,一臺服務(wù)器滿足不了高并發(fā)的要求,因此公司把應(yīng)用給部署到集群中,前端通過nginx代理(應(yīng)用服務(wù)器ip可能是用防火墻進(jìn)行了隔離,避免了直接使用ip+端口+應(yīng)用名訪問的方式)。

在集群環(huán)境中,同樣的定時(shí)任務(wù),在集群中的每臺機(jī)器都會執(zhí)行,這樣定時(shí)任務(wù)就會重復(fù)執(zhí)行,不但會增加服務(wù)器的負(fù)擔(dān),還會因?yàn)槎〞r(shí)任務(wù)重復(fù)執(zhí)行造成額外的不可預(yù)期的錯(cuò)誤,因此公司的解決方案是:根據(jù)集群的數(shù)量,來把定時(shí)任務(wù)中的任務(wù)平均分到集群中的每臺機(jī)器上(這里的平均分是指以前一個(gè)定時(shí)任務(wù)本來是在一臺機(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ù)就會掛掉或者不能一次性跑完,會對業(yè)務(wù)產(chǎn)生嚴(yán)重的影響

針對缺陷的解決方案(本文的重點(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ù)唯一的名稱,將會持久化到數(shù)據(jù)庫-->
    <property name="schedulerName" value="baseScheduler"/>

    <!-- 每臺集群機(jī)器部署應(yīng)用的時(shí)候會更新觸發(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()));

  }
}

測試結(jié)果:

由于只有一臺電腦,所有我開了8080和8888兩個(gè)端口來測試的,上面的定時(shí)任務(wù)我設(shè)置了每10秒運(yùn)行一次。

當(dāng)只我啟動(dòng)8080端口時(shí),可以看到控制臺每隔10秒打印一條語句

兩個(gè)端口同時(shí)啟動(dòng)的對比測試中可以看到,只有一個(gè)端口在跑定時(shí)任務(wù)

 

 這個(gè)關(guān)了正在跑定時(shí)任務(wù)的端口后,之前的另一個(gè)沒有跑的端口開始接管,繼續(xù)運(yùn)行定時(shí)任務(wù)

至此,我們可以清楚地看到,在分布式定時(shí)任務(wù)中(或者集群),同一時(shí)刻只會有一個(gè)定時(shí)任務(wù)運(yùn)行。

整個(gè)demo地址:http://xiazai.jb51.net/201701/yuanma/spring-cluster-quartz_jb51.rar

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • spring aop兩種配置方式

    spring aop兩種配置方式

    這篇文章主要為大家詳細(xì)介紹了spring aop兩種配置方式,主要是注解配置AOP和xml配置aop,需要的朋友可以參考下
    2015-09-09
  • 兩個(gè)jar包下相同包名類名引入沖突的解決方法

    兩個(gè)jar包下相同包名類名引入沖突的解決方法

    本文主要介紹了兩個(gè)jar包下相同包名類名引入沖突的解決方法,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • 如何解決Mybatis--java.lang.IllegalArgumentException: Result Maps collection already contains value for X

    如何解決Mybatis--java.lang.IllegalArgumentException: Result Maps

    這兩天因?yàn)轫?xiàng)目需要整合spring、struts2、mybatis三大框架,但啟動(dòng)的時(shí)候總出現(xiàn)這個(gè)錯(cuò)誤,困擾我好久,折騰了好久終于找到問題根源,下面小編給大家分享下問題所在及解決辦法,一起看看吧
    2016-12-12
  • 如何通過properties文件配置web.xml中的參數(shù)

    如何通過properties文件配置web.xml中的參數(shù)

    這篇文章主要介紹了如何通過properties文件配置web.xml中的參數(shù)方法,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • SpringCloud Gateway路由組件詳解

    SpringCloud Gateway路由組件詳解

    SpringCloud Gateway 是 Spring Cloud 的一個(gè)全新項(xiàng)目,它旨在為微服務(wù)架構(gòu)提供一種簡單有效的統(tǒng)一的 API 路由管理方式。這篇文章主要介紹了SpringCloud Gateway網(wǎng)關(guān)作用,需要的朋友可以參考下
    2023-02-02
  • java實(shí)現(xiàn)表格數(shù)據(jù)的存儲

    java實(shí)現(xiàn)表格數(shù)據(jù)的存儲

    這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)表格數(shù)據(jù)的存儲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-04-04
  • Spring事務(wù)@Transactional注解四種不生效案例場景分析

    Spring事務(wù)@Transactional注解四種不生效案例場景分析

    這篇文章主要為大家介紹了Spring事務(wù)@Transactional注解四種不生效的案例場景示例分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-07-07
  • 通過字節(jié)碼看java中this的隱式傳參詳解

    通過字節(jié)碼看java中this的隱式傳參詳解

    這篇文章主要給大家介紹了關(guān)于如何通過字節(jié)碼看java中this的隱式傳參的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-11-11
  • java swing框架實(shí)現(xiàn)貪吃蛇游戲

    java swing框架實(shí)現(xiàn)貪吃蛇游戲

    這篇文章主要為大家詳細(xì)介紹了java swing框架實(shí)現(xiàn)貪吃蛇游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-12-12
  • Java中實(shí)體與Map之間的相互轉(zhuǎn)換代碼示例

    Java中實(shí)體與Map之間的相互轉(zhuǎn)換代碼示例

    生活中經(jīng)常用到map數(shù)據(jù)與實(shí)體類的轉(zhuǎn)換,下面這篇文章主要給大家介紹了關(guān)于Java中實(shí)體與Map之間相互轉(zhuǎn)換的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-12-12

最新評論