`
sundoctor
  • 浏览: 323675 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Quartz在Spring中集群

阅读更多

概述

虽然单个Quartz实例能给予你很好的Job调度能力,但它不能满足典型的企业需求,如可伸缩性、高可靠性满足。假如你需要故障转移的能力并能运行日益增多的 Job,Quartz集群势必成为你应用的一部分了。使用 Quartz 的集群能力可以更好的支持你的业务需求,并且即使是其中一台机器在最糟的时间崩溃了也能确保所有的 Job 得到执行。

Quartz 中集群如何工作

一个 Quartz 集群中的每个节点是一个独立的 Quartz 应用,它又管理着其他的节点。意思是你必须对每个节点分别启动或停止。不像许多应用服务器的集群,独立的 Quartz 节点并不与另一其的节点或是管理节点通信。Quartz 应用是通过数据库表来感知到另一应用的。

图:表示了每个节点直接与数据库通信,若离开数据库将对其他节点一无所知


创建Quartz数据库表

因为Quartz 集群依赖于数据库,所以必须首先创建Quartz数据库表。Quartz 包括了所有被支持的数据库平台的 SQL 脚本。在 <quartz_home>/docs/dbTables 目录下找到那些 SQL 脚本,这里的 <quartz_home> 是解压 Quartz 分发包后的目录。
这里采用的Quartz 2.2.1版本,总共11张表,不同版本,表个数可能不同。数据库为mysql,用tables_mysql_innodb.sql创建数据库表。

配置数据库连接池

1.配置jdbc.properties文件

 

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/quartz?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true
jdbc.username=root
jdbc.password=kfs

 
2.配置applicationContext.xml文件

 

 

<?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:jdbc="http://www.springframework.org/schema/jdbc"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="   
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
   http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
   http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
   ">

	<context:component-scan base-package="com.sundoctor" />

	<!-- 属性文件读入 -->
	<bean id="propertyConfigurer"
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<list>
				<value>classpath:jdbc.properties</value>
			</list>
		</property>
	</bean>


	<!-- 数据源定义,使用c3p0 连接池 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
		destroy-method="close">
		<property name="driverClass" value="${jdbc.driverClassName}" />
		<property name="jdbcUrl" value="${jdbc.url}" />
		<property name="user" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
		<property name="initialPoolSize" value="${cpool.minPoolSize}" />
		<property name="minPoolSize" value="${cpool.minPoolSize}" />
		<property name="maxPoolSize" value="${cpool.maxPoolSize}" />
		<property name="acquireIncrement" value="${cpool.acquireIncrement}" />
		<property name="maxIdleTime" value="${cpool.maxIdleTime}" />
	</bean>
</beans>

 

 

 

创建Job测试服务类

package com.sundoctor.quartz.cluster.example;

import java.io.Serializable;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;


@Service("simpleService")
public class SimpleService {
	
	private static final long serialVersionUID = 122323233244334343L;
	private static final Logger logger = LoggerFactory.getLogger(SimpleService.class);
	
	public void testMethod1(){
		//这里执行定时调度业务
		logger.info("testMethod1.......1");
	}
	
	public void testMethod2(){
		logger.info("testMethod2.......2");	
	}
}

 

 

 

创建两个Job类MyQuartzJobBean1、MyQuartzJobBean2

 

package com.sundoctor.quartz.cluster.example;

import org.quartz.DisallowConcurrentExecution;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.PersistJobDataAfterExecution;
import org.quartz.SchedulerException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.scheduling.quartz.QuartzJobBean;

@PersistJobDataAfterExecution
@DisallowConcurrentExecution// 不允许并发执行
public class MyQuartzJobBean1 extends QuartzJobBean {

	private static final Logger logger = LoggerFactory.getLogger(MyQuartzJobBean1.class);

	@Override
	protected void executeInternal(JobExecutionContext jobexecutioncontext) throws JobExecutionException {

		SimpleService simpleService = getApplicationContext(jobexecutioncontext).getBean("simpleService",
				SimpleService.class);
		simpleService.testMethod1();

	}

	private ApplicationContext getApplicationContext(final JobExecutionContext jobexecutioncontext) {
		try {
			return (ApplicationContext) jobexecutioncontext.getScheduler().getContext().get("applicationContextKey");
		} catch (SchedulerException e) {
			logger.error("jobexecutioncontext.getScheduler().getContext() error!", e);
			throw new RuntimeException(e);
		}
	}

}

 



配置 Quartz 使用集群

1.配置节点的 quartz.properties 文件

引用

org.quartz.scheduler.instanceName = TestScheduler1  
org.quartz.scheduler.instanceId = AUTO 

org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount = 10
org.quartz.threadPool.threadPriority = 5
org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread = true

org.quartz.jobStore.misfireThreshold = 60000
org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX
org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.StdJDBCDelegate
org.quartz.jobStore.tablePrefix = QRTZ_
org.quartz.jobStore.maxMisfiresToHandleAtATime=10
org.quartz.jobStore.isClustered = true 
org.quartz.jobStore.clusterCheckinInterval = 20000


org.quartz.scheduler.instanceName属性可为任何值,用在 JDBC JobStore 中来唯一标识实例,但是所有集群节点中必须相同。

org.quartz.scheduler.instanceId 属性为 AUTO即可,基于主机名和时间戳来产生实例 ID。

org.quartz.jobStore.class属性为 JobStoreTX,将任务持久化到数据中。因为集群中节点依赖于数据库来传播 Scheduler 实例的状态,你只能在使用 JDBC JobStore 时应用 Quartz 集群。这意味着你必须使用 JobStoreTX 或是 JobStoreCMT 作为 Job 存储;你不能在集群中使用 RAMJobStore。

org.quartz.jobStore.isClustered 属性为 true,你就告诉了 Scheduler 实例要它参与到一个集群当中。这一属性会贯穿于调度框架的始终,用于修改集群环境中操作的默认行为。

org.quartz.jobStore.clusterCheckinInterval 属性定义了Scheduler 实例检入到数据库中的频率(单位:毫秒)。Scheduler 检查是否其他的实例到了它们应当检入的时候未检入;这能指出一个失败的 Scheduler 实例,且当前 Scheduler 会以此来接管任何执行失败并可恢复的 Job。通过检入操作,Scheduler 也会更新自身的状态记录。clusterChedkinInterval 越小,Scheduler 节点检查失败的 Scheduler 实例就越频繁。默认值是 15000 (即15 秒)。

2.配置applicationContext-quartz.xml文件

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

	<bean name="quartzScheduler"
		class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
		<property name="dataSource">
			<ref bean="dataSource" />
		</property>
		<property name="applicationContextSchedulerContextKey" value="applicationContextKey" />
		<property name="configLocation" value="classpath:quartz.properties" />		
		<property name="triggers">
			<list>
				<ref bean="trigger1" />
				<ref bean="trigger2" />
			</list>
		</property>
	</bean>

	<bean id="jobDetail1" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
		<property name="jobClass">
			<value>com.sundoctor.quartz.cluster.example.MyQuartzJobBean1</value>
		</property>	
		<property name="durability" value="true" />	
		<property name="requestsRecovery" value="true" />		
	</bean>
	<bean id="trigger1" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
		<property name="jobDetail" ref="jobDetail1" />
		<property name="cronExpression" value="0/30 * * ? * * *" />
	</bean>

	<bean id="jobDetail2" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
		<property name="jobClass">
			<value>com.sundoctor.quartz.cluster.example.MyQuartzJobBean2</value>
		</property>	
		<property name="durability" value="true" />	
		<property name="requestsRecovery" value="true" />		
	</bean>
	<bean id="trigger2" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
		<property name="jobDetail" ref="jobDetail2" />
		<property name="cronExpression" value="0/10 * * ? * * *" />
	</bean>	

</beans>

 

 


dataSource:项目中用到的数据源,里面包含了quartz用到的11张数据库表;

applicationContextSchedulerContextKey: 是org.springframework.scheduling.quartz.SchedulerFactoryBean这个类中把spring上下 文以key/value的方式存放在了SchedulerContext中了,可以用applicationContextSchedulerContextKey所 定义的key得到对应spring 的ApplicationContext;

configLocation:用于指明quartz的配置文件的位置

requestsRecovery
requestsRecovery属性必须设置为 true,当Quartz服务被中止后,再次启动或集群中其他机器接手任务时会尝试恢复执行之前未完成的所有任务。

运行Quartz集群

在相同或不同的机器上运行com.sundoctor.quartz.cluster.example.test.MainTest进行测试,在本例中只是简单打印一下日志。

package com.sundoctor.quartz.cluster.example.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		ApplicationContext springContext = new ClassPathXmlApplicationContext(new String[]{"classpath:applicationContext.xml","classpath:applicationContext-quartz.xml"});
	}

}



Quartz 实际并不关心你是在相同的还是不同的机器上运行节点。当集群是放置在不同的机器上时,通常称之为水平集群。节点是跑在同一台机器是,称之为垂直集群。对于垂直集群,存在着单点故障的问题。这对高可用性的应用来说是个坏消息,因为一旦机器崩溃了,所有的节点也就被有效的终止了。

当你运行水平集群时,时钟应当要同步,以免出现离奇且不可预知的行为。假如时钟没能够同步,Scheduler 实例将对其他节点的状态产生混乱。有几种简单的方法来保证时钟何持同步,而且也没有理由不这么做。最简单的同步计算机时钟的方式是使用某一个 Internet 时间服务器(Internet Time Server ITS)。

没什么会阻止你在相同环境中使用集群的和非集群的 Quartz 应用。唯一要注意的是这两个环境不要混用在相同的数据库表。意思是非集群环境不要使用与集群应用相同的一套数据库表;否则将得到希奇古怪的结果,集群和非集群的 Job 都会遇到问题。

假如你让一个非集群的 Quartz 应用与集群节点并行着运行,设法使用 JobInitializationPlugin和 RAMJobStore。

 

分享到:
评论
26 楼 href2008 2016-11-29  
你好,请教下,如果不同应用,用一个quartz库可以吗?

是不是只要不同应用的org.quartz.scheduler.instanceName不一样就可以了?
25 楼 u010354462 2016-01-06  
有2个tomcat,在A停掉之后,B开始任务;会出现执行多次的情况


INFO [quartzScheduler_Worker-1] cn.com.zhulong.app.service.SimpleService.testMethod1(18) | testMethod1.......1 ***时间:2016-01-06 17:41:07
INFO [quartzScheduler_Worker-2] cn.com.zhulong.app.service.SimpleService.testMethod1(18) | testMethod1.......1 ***时间:2016-01-06 17:41:07
INFO [quartzScheduler_Worker-3] cn.com.zhulong.app.service.SimpleService.testMethod1(18) | testMethod1.......1 ***时间:2016-01-06 17:41:07
INFO [quartzScheduler_Worker-4] cn.com.zhulong.app.service.SimpleService.testMethod1(18) | testMethod1.......1 ***时间:2016-01-06 17:41:10
INFO [quartzScheduler_Worker-5] cn.com.zhulong.app.service.SimpleService.testMethod1(18) | testMethod1.......1 ***时间:2016-01-06 17:41:20
INFO [quartzScheduler_Worker-6] cn.com.zhulong.app.service.SimpleService.testMethod1(18) | testMethod1.......1 ***时间:2016-01-06 17:41:30
INFO [quartzScheduler_Worker-7] cn.com.zhulong.app.service.SimpleService.testMethod1(18) | testMethod1.......1 ***时间:2016-01-06 17:41:40
INFO [quartzScheduler_Worker-8] cn.com.zhulong.app.service.SimpleService.testMethod1(18) | testMethod1.......1 ***时间:2016-01-06 17:41:50
INFO [quartzScheduler_Worker-9] cn.com.zhulong.app.service.SimpleService.testMethod1(18) | testMethod1.......1 ***时间:2016-01-06 17:42:00
24 楼 binyun530 2015-02-05  
binyun530 写道
我遇到一个问题
我有2个tomcat,在A停掉之后,B开始任务;但是会出现执行多次的情况

2015-02-05 14:53:54 INFO 127.0.0.1 CommonTaskImpl.java 38 CommonTaskImpl execute start.
2015-02-05 14:53:54 INFO 127.0.0.1 CommonTaskImpl.java 46 CommonTaskImpl execute end. total time spent:0 ms
2015-02-05 14:53:54 INFO 127.0.0.1 CommonTaskImpl.java 38 CommonTaskImpl execute start.
2015-02-05 14:53:54 INFO 127.0.0.1 CommonTaskImpl.java 46 CommonTaskImpl execute end. total time spent:0 ms
2015-02-05 14:53:54 INFO 127.0.0.1 CommonTaskImpl.java 38 CommonTaskImpl execute start.
2015-02-05 14:53:54 INFO 127.0.0.1 CommonTaskImpl.java 46 CommonTaskImpl execute end. total time spent:0 ms
2015-02-05 14:53:54 INFO 127.0.0.1 CommonTaskImpl.java 38 CommonTaskImpl execute start.
2015-02-05 14:53:54 INFO 127.0.0.1 CommonTaskImpl.java 46 CommonTaskImpl execute end. total time spent:0 ms
2015-02-05 14:53:54 INFO 127.0.0.1 CommonTaskImpl.java 38 CommonTaskImpl execute start.
2015-02-05 14:53:54 INFO 127.0.0.1 CommonTaskImpl.java 46 CommonTaskImpl execute end. total time spent:0 ms
23 楼 binyun530 2015-02-05  
我遇到一个问题
我有2个tomcat,在A停掉之后,B开始任务;但是会出现执行多次的情况
22 楼 sundoctor 2014-08-21  
踏雁寻花 写道
楼主你好,请教您一个问题:
在集群的时候,如何实现动态的指定 触发器 在哪个数据库服务器上面执行job?


这个好象还真不行,如果真要做,只能自己扩展实现。
21 楼 sundoctor 2014-08-21  
guan2468 写道
按照群主你的Quartz任务监控管理 的配值,如何让任务变成有状态的任务,不能并发,就是加上<property name="concurrent" value="false" />



我已经可以动态加载,但是任务还是并发,把这句代码放在那里,可以让他不并发。



底下是我要完成的要求

在quartz和sping整合中动态执行加载的定时任务,定时任务是有状态的,不能并发。


在Job上标注@DisallowConcurrentExecution就可以让任务变成有状态,防止并发
@PersistJobDataAfterExecution
@DisallowConcurrentExecution// 不允许并发执行
public class MyQuartzJobBean1 extends QuartzJobBean {

20 楼 踏雁寻花 2014-03-05  
楼主你好,请教您一个问题:
在集群的时候,如何实现动态的指定 触发器 在哪个数据库服务器上面执行job?
19 楼 guan2468 2013-12-05  
按照群主你的Quartz任务监控管理 的配值,如何让任务变成有状态的任务,不能并发,就是加上<property name="concurrent" value="false" />



我已经可以动态加载,但是任务还是并发,把这句代码放在那里,可以让他不并发。



底下是我要完成的要求

在quartz和sping整合中动态执行加载的定时任务,定时任务是有状态的,不能并发。
18 楼 guan2468 2013-12-05  
按照群主你的Quartz任务监控管理 的配值,如何让任务变成有状态的任务,不能并发,就是加上<property name="concurrent" value="false" />



我已经可以动态加载,但是任务还是并发,把这句代码放在那里,可以让他不并发。



底下是我要完成的要求

在quartz和sping整合中动态执行加载的定时任务,定时任务是有状态的,不能并发。
17 楼 vivixun 2010-09-13  
请问SimpleService要怎么取到Spring定义的bean?
16 楼 sundoctor 2010-07-02  
wlwolf 写道
sundoctor 写道
putonyuer 写道
为了不让各集群上的quartz 发生紊乱, 那就是说 , 还要quartz节点间还需要时钟和由哪一个节点来执行的信息通信咯?


每个Quartz节点都是直接与数据库通信,并不与任何其它的节点通信。需要所有节点服务器时钟同步,所有节点共享一套数据库表,每个节点是通过数据库表记录的时间来知道当前那些Trigger在执行。



真是这样的吗,你看过这部分源码?真的是取系统的时间?我觉得quartz不会这么。。。,这只是你的猜想吧,一般这种集群要去数据库时间的,这样就不用保证每个node时间同步了,由数据库去保证


实践是检验真理的唯一标准,是不是这样的,最好自己试试。
15 楼 wlwolf 2010-07-01  
sundoctor 写道
putonyuer 写道
为了不让各集群上的quartz 发生紊乱, 那就是说 , 还要quartz节点间还需要时钟和由哪一个节点来执行的信息通信咯?


每个Quartz节点都是直接与数据库通信,并不与任何其它的节点通信。需要所有节点服务器时钟同步,所有节点共享一套数据库表,每个节点是通过数据库表记录的时间来知道当前那些Trigger在执行。



真是这样的吗,你看过这部分源码?真的是取系统的时间?我觉得quartz不会这么。。。,这只是你的猜想吧,一般这种集群要去数据库时间的,这样就不用保证每个node时间同步了,由数据库去保证
14 楼 cydia 2010-06-30  
不错的文章,正好有这方面的需求
13 楼 onelikejava 2010-05-21  
楼主我现在在was 集群11个实例上部署我的应用,应用的后台用了quartz 定时器做短信定时发送等很多定时器任务,目前由于无法再was 集群保证quartz 正常运行,最终只能把定时器单独抽取剥离这个系统做单机部署。 看了你的文章,quartz 才有数据库表来维护并且要维护10几张表达到集群,这样的改造方案费时并且不太好维护,由于我们做产品,这样的添加在其他场合应用也会受影响。望楼主能给予些指点!
12 楼 tanyun1111 2010-03-09  
有个问题请教,我配置了好几个任务,每个任务相隔几分钟,总是会出现 org.quartz.JobPersistenceException: Couldn't update trigger state(s): 事务(进程 ID  54)与另一个进程已被死锁在  lock 资源上,且该事务已被选作死锁牺牲品。请重新运行该事务。 环境是:tomcat6 quartz1.6.6 数据库sql 2000,tomcat和sql在同台机子上。
11 楼 sundoctor 2010-01-12  
我还真没办法在was上跑过,没有was环境呀。
10 楼 lixw 2010-01-12  
按照您的指导和代码,我的应用在Tomcat上跑的很正常,但是同样的应用发布到WAS上就报错了,错误如下:
00000238 JobRunShell   I org.quartz.core.JobRunShell run Job DEFAULT.job threw a JobExecutionException:
org.quartz.JobExecutionException: java.lang.reflect.InvocationTargetException [See nested exception: java.lang.reflect.InvocationTargetException]
at frameworkx.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean$MethodInvokingJob.execute(MethodInvokingJobDetailFactoryBean.java:558)

麻烦看看什么问题,谢谢!
9 楼 jelly 2009-12-31  
这篇文章讲的很好,谢谢!

Spring 的配置文件里面配置了Job, 然后Job又被存储到数据库,那么Job的定义将存在于两个地方,
1. Spring的配置文件
2. 数据库

那么就出现令我疑惑的地方:
1. 是否每次都会再加载一次spring中配置的Job
2. 如果在spring中修改某个Job,重新启动之后,
将如何处理?同步数据库还是重新生成一个job,还是会报错?
8 楼 sundoctor 2009-10-15  
引用
请让我钻个牛角尖 , 你还得对这个库设置隔离级别为1 ,或者你的程序读数据时用的排他锁方式读,否则容易死锁或者脏数据导致的quartz重复执行!


数据库隔离级别采用默认值即可,不用特别为quartz而设置。至于锁,quartz是通过数据库记录的时间来知道其它应用的存在,只要所有节点时间同步,就不是会出重复执行。所以在quartz集群中时间同步特别重要,时间不同步可能会造成重复执行,由此才可能会出现死锁或者脏数据,后果不可以预料。

7 楼 putonyuer 2009-10-15  
sundoctor 写道
putonyuer 写道
为了不让各集群上的quartz 发生紊乱, 那就是说 , 还要quartz节点间还需要时钟和由哪一个节点来执行的信息通信咯?


每个Quartz节点都是直接与数据库通信,并不与任何其它的节点通信。需要所有节点服务器时钟同步,所有节点共享一套数据库表,每个节点是通过数据库表记录的时间来知道当前那些Trigger在执行。



谢谢  明白了

jdbc.url=jdbc:mysql://localhost:3306/quartz   看 你的配置 , 是有个库!

请让我钻个牛角尖 , 你还得对这个库设置隔离级别为1 ,或者你的程序读数据时用的排他锁方式读,否则容易死锁或者脏数据导致的quartz重复执行!

相关推荐

Global site tag (gtag.js) - Google Analytics