问题背景

  我公司是一个快速发展的创业公司,目前有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:git@github.com:AaronFeng2014/spring-cluster-quartz.git

基于spring+quartz的分布式定时任务框架的更多相关文章

  1. 分布式定时任务框架比较,spring batch, tbschedule jobserver

    分布式定时任务框架比较,spring batch, tbschedule jobserver | 移动开发参考书 分布式定时任务框架比较,spring batch, tbschedule jobser ...

  2. Elastic-Job - 分布式定时任务框架

    Elastic-Job - 分布式定时任务框架 摘要 Elastic-Job是ddframe中dd-job的作业模块中分离出来的分布式弹性作业框架.去掉了和dd-job中的监控和ddframe接入规范 ...

  3. 分布式定时任务框架——python定时任务框架APScheduler扩展

    http://bbs.7boo.org/forum.php?mod=viewthread&tid=14546 如果将定时任务部署在一台服务器上,那么这个定时任务就是整个系统的单点,这台服务器出 ...

  4. 带你手写基于 Spring 的可插拔式 RPC 框架(一)介绍

    概述 首先这篇文章是要带大家来实现一个框架,听到框架大家可能会觉得非常高大上,其实这和我们平时写业务员代码没什么区别,但是框架是要给别人使用的,所以我们要换位思考,怎么才能让别人用着舒服,怎么样才能让 ...

  5. 3分钟掌握Quartz.net分布式定时任务的姿势

    引言 长话短说,今天聊一聊分布式定时任务,我的流水账笔记: ASP.NET Core+Quartz.Net实现web定时任务 AspNetCore结合Redis实践消息队列 细心朋友稍一分析,就知道还 ...

  6. Swift:一个基于.NET Core的分布式批处理框架

    Swift是什么 从文章的标题可知:此Swift非Apple那个Swift,只是考虑这个词的含义比较适合. Swift是一个基于.NET Core的分布式批处理框架,支持将作业分割后分发到多台服务器并 ...

  7. 基于redis的简易分布式爬虫框架

    代码地址如下:http://www.demodashi.com/demo/13338.html 开发环境 Python 3.6 Requests Redis 3.2.100 Pycharm(非必需,但 ...

  8. Spring自带的定时任务框架Schedule的优缺点及使用

    spring自带的定时任务框架的有点:简单,拆箱即用 spring自带的定时任务框架的缺点: 不支持集群:为避免重复执行的问题 不支持生命周期统一管理:不重启服务情况下关闭,启动任务 不支持分片任务: ...

  9. Quartz小记(一):Elastic-Job - 分布式定时任务框架

    Elastic-Job是ddframe中dd-job的作业模块中分离出来的分布式弹性作业框架.去掉了和dd-job中的监控和ddframe接入规范部分.该项目基于成熟的开源产品Quartz和Zooke ...

随机推荐

  1. [LintCode] Majority Number 求众数

    Given an array of integers, the majority number is the number that occurs more than half of the size ...

  2. ArcEngine 岛状多边形内部环的获取

    ArcEngine岛状多边形获取其内部环 查阅了帮助文档相关接口,内部环的获方法get_InteriorRingBag() 需要外部环作为参数.而外部环可以直接通过ExteriorRingBag属性获 ...

  3. memcache 笔记

    在windows下安装memecache,进入到安装目录,执行帮助命令可以查看到相关帮助 启动memcache,相关命令如上,比如-p就是端口号,memcache默认端口号是11211 语法:增加ad ...

  4. source insight使用

    左键选中后 ctrl+ 跳转到定义 alt+,返回到调用者的程序

  5. PHP 生成PDF

    一个项目中需要用到网页生成PDF,就是将整个网页生成一个PDF文件, 以前也用过HTML2PDF,只能生成一些简单的HTML代码,复杂的HTML + css 生成的效果惨不忍睹, 百度了一下,发现有个 ...

  6. x86架构手机跑安卓好吗?(脑补)

    华硕低价位手机ZenFone一推出就掀起市场话题,许多人也对ZenFone所采用的Intel Atom处理器有所意见,深怕其相容性问题无法正确执行应用程式App,这究竟是怎么回事呢? Intel近几年 ...

  7. WebApi接口传参不再困惑(4):传参详解(转载)

    WebApi接口传参不再困惑(4):传参详解   前言:还记得刚使用WebApi那会儿,被它的传参机制折腾了好久,查阅了半天资料.如今,使用WebApi也有段时间了,今天就记录下API接口传参的一些方 ...

  8. ASP.NET MVC 项目中 一般处理程序ashx 获取Session

    1-在 aspx和aspx.cs中,都是以Session["xxx"]="aaa"和aaa=Session["xxx"].ToString( ...

  9. winform 获取当前程序运行根目录

    // 获取程序的基目录. System.AppDomain.CurrentDomain.BaseDirectory // 获取模块的完整路径. System.Diagnostics.Process.G ...

  10. 1.Linux是什么?

    UNIX设计理念: 所有的程序或系统装置都是文件. 不管构建编辑器还是附属文件,所写的程序只有一个目的,就是有效地完成目标 操作系统:应用程序->系统调用->内核->硬件.其中系统调 ...