大战熟女丰满人妻av-荡女精品导航-岛国aaaa级午夜福利片-岛国av动作片在线观看-岛国av无码免费无禁网站-岛国大片激情做爰视频

專注Java教育14年 全國咨詢/投訴熱線:400-8080-105
動力節(jié)點LOGO圖
始于2009,口口相傳的Java黃埔軍校
首頁 hot資訊 基于spring+quartz的分布式定時任務(wù)框架

基于spring+quartz的分布式定時任務(wù)框架

更新時間:2021-08-17 09:29:37 來源:動力節(jié)點 瀏覽868次

目前集群中定時任務(wù)實現(xiàn)方式的缺陷

目前在集群中處理定時任務(wù)的方式不是正真的分布式處理方式,而是一種偽分布式,這種方式存在一個明顯的缺陷就是當集群中機器宕機,那么整個定時任務(wù)就會掛掉或者不能一次性跑完,會對業(yè)務(wù)產(chǎn)生嚴重的影響

針對缺陷的解決方案

利用spring+quartz構(gòu)建一套真正的分布式定時任務(wù)系統(tǒng),經(jīng)過查閱相關(guān)資料得知:quartz框架是原生就支持分布式定時任務(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"/>
        <!-- 每臺集群機器部署應(yīng)用的時候會更新觸發(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"/>    <!--因為我使用了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>
    <!-- 配置觸發(fā)時間 -->
    <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的自動注入
 * @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注解,否則只能在配置文件中設(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兩個端口來測試的,上面的定時任務(wù)設(shè)置了每10秒運行一次。

當只啟動8080端口時,可以看到控制臺每隔10秒打印一條語句

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

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

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

以上就是動力節(jié)點小編介紹的"基于spring+quartz的分布式定時任務(wù)框架",希望對大家有幫助,想了解更多可查看Java分布式應(yīng)用教程。動力節(jié)點在線學(xué)習(xí)教程,針對沒有任何Java基礎(chǔ)的讀者學(xué)習(xí),讓你從入門到精通,主要介紹了一些Java基礎(chǔ)的核心知識,讓同學(xué)們更好更方便的學(xué)習(xí)和了解Java編程,感興趣的同學(xué)可以關(guān)注一下。

提交申請后,顧問老師會電話與您溝通安排學(xué)習(xí)

免費課程推薦 >>
技術(shù)文檔推薦 >>
主站蜘蛛池模板: 成人国产亚洲欧美成人综合网 | 奇米色偷偷| 国产欧美日韩在线播放 | 激情在线播放免费视频高清 | 久久99精品这里精品动漫6 | 91精品在线免费 | 人做人爱视频欧美在线观看 | 日韩亚射吧 | 精品精品国产理论在线观看 | 看a网址 | 四虎在线最新地址公告 | 成人短视频视频在线观看网站 | 九久久| 亚洲国产精品综合久久20 | 够爱久久 | 黄色片网站大全 | 九九九九精品视频在线播放 | 外国一级黄色毛片 | 手机在线精品视频 | 国产高清自拍视频 | 久久专区| 在线看污网站 | 亚洲一级毛片在线播放 | 亚洲国产美女精品久久 | 久久精品午夜视频 | 亚洲国产精品婷婷久久 | 欧美一二区视频 | 色婷婷六月 | 伊人久久伊人 | 美女视频黄的免费视频网页 | 手机看片自拍日韩日韩高清 | 福利不卡| 欧洲亚洲视频 | 色综合合久久天天给综看 | 国产黄网永久免费 | 国产精品视频在线免费观看 | 成人国产精品免费视频不卡 | 日韩第1页 | 欧美视频a | 97视频在线免费 | jzz欧美 |