如何解决quartz在集群下出现的资源抢夺现象
Quartz是一个开源的作业调度框架,它完全由Java写成,并设计用于J2SE和J2EE应用中。它提供了巨大的灵活性而不牺牲简单性。你能够用它来为执行一个作业而创建简单的或复杂的调度,简单的说就是可以实现java的定时任务。
一、问题描述
但是,当在集群环境下,每一台服务器上都有这段定时发送信息的代码,多个服务器下如何用quartz协调处理自动化JOB。
如果现在有A,B,C三台机器同时作为集群服务器对外统一提供服务:
A、B、C三台机器上各有一个QUARTZ,他们会按照即定的SCHEDULE自动执行各自的任务。这样的架构其实有点像多线程,可能产生脏读,脏写。因为三台APP SERVER里都有QUARTZ,因此会存在重复处理TASK的现象。
一般有三种解决方案:
1.一般外面的解决方案是只在一台 APP 上装 QUARTZ,其它两台不装,这样集群就形同虚设了;
2.另一种解决方案是动代码,这样就要影响到原来已经写好的QUARTZ JOB的代码了,这对程序开发人员来说比较痛苦;
3.quartz自身提供了解决集群环境下配置的接口,通过重写类并进行配置即可。
下面,就详细介绍一下quartz在集群环境下如何配置。
二、quartz在集群下的配置
1.首先在quartz官网上下载相应的jar包。我下载的是quartz1-1.5.2.zip。
2.解压后在docs\dbTables目录下找到相应的数据库创建表的sql语句,并在数据库中执行该sql语句,执行后会创建十二张表。这里我使用的是mysql数据库,所以用的是tables_mysql.sql文件。
3.导入jar包quartz-all-1.5.2.jar,并Build Path。
注意:spring3.0不支持quartz2.0以上的版本,且quartz1.6.0版本有些问题,最好不要使用。
4.因为quartz在集群下配置依赖于将节点信息存放到数据库中,而org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean不支持序列化,不能将信息存放到数据库中,所以需要重写QuartzJobBean类。
import java.lang.reflect.Method;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.context.ApplicationContext;
import org.springframework.scheduling.quartz.QuartzJobBean; public class MyDetailQuartzJobBean extends QuartzJobBean {
protected final Log logger = LogFactory.getLog(getClass());
private String targetObject;
private String targetMethod;
private ApplicationContext ctx; @Override
protected void executeInternal(JobExecutionContext context)
throws JobExecutionException {
try {
logger.info("execute [" + targetObject + "] at once>>>>>>");
Object otargetObject = ctx.getBean(targetObject);
Method m = null; try {
m = otargetObject.getClass().getMethod(targetMethod, new Class[] {JobExecutionContext.class});
m.invoke(otargetObject, new Object[] {context});
} catch (SecurityException e) {
logger.error(e);
} catch (NoSuchMethodException e) {
logger.error(e);
}
} catch (Exception e) {
throw new JobExecutionException(e);
}
} public void setApplicationContext(ApplicationContext applicationContext) {
this.ctx = applicationContext;
} public void setTargetObject(String targetObject) {
this.targetObject = targetObject;
} public void setTargetMethod(String targetMethod) {
this.targetMethod = targetMethod;
}
}
5.编写要定时执行的代码,详见StudentTimer.java。
import java.util.Date; import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.stereotype.Service; @Service
public class StudentTimer implements Job { @Override
public void execute(JobExecutionContext arg0) throws JobExecutionException {
// TODO Auto-generated method stub
System.out.println(new Date()+"Student中的execute正在执行...");
} }
注意:如果没有实现Job接口,则方法参数必须为JobExecutionContext arg0,如下:
import java.util.Date; import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.stereotype.Service; @Service
public class StudentTimer { public void update(JobExecutionContext arg0) throws JobExecutionException {
// TODO Auto-generated method stub
System.out.println(new Date()+"Student中的update正在执行...");
} }
6.在application-context.xml中进行配置,详见timerTask.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:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd"
> <bean id="doJob" class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass">
<value>com.core.Util.MyDetailQuartzJobBean</value>
</property>
<property name="jobDataAsMap">
<map>
<entry key="targetObject" value="studentTimer" />
<entry key="targetMethod" value="execute" />
</map>
</property>
</bean>
<!-- 触发器的bean的设置,在这里我们设置了我们要触发的jobDetail是哪个。这里我们定义了要触发的jobDetail是searchEngerneTask,即触发器去触发哪个bean..并且我们还定义了触发的时间 -->
<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail">
<ref bean="doJob" />
</property>
<property name="cronExpression">
<!-- 关键在配置此表达式,时间设置这里表示20秒触发一次,具体可以自己去找资料看 -->
<value>0/20 * * * * ?</value>
</property>
</bean>
<!-- 管理触发器的总设置,管理我们的触发器列表,可以在bean的list中放置多个触发器。 -->
<bean autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="cronTrigger" />
</list>
</property>
<property name="applicationContextSchedulerContextKey" value="applicationContext" />
<property name="configLocation" value="classpath:com/core/Config/quartz.properties" />
</bean>
</beans>
7.编写quartz.properties文件。
#==============================================================
#Configure Main Scheduler Properties
#==============================================================
org.quartz.scheduler.instanceName = quartzScheduler
org.quartz.scheduler.instanceId = AUTO
#org.quartz.scheduler.wrapJobExecutionInUserTransaction = true
#==============================================================
#Configure JobStore
#==============================================================
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.isClustered = true
org.quartz.jobStore.clusterCheckinInterval =
org.quartz.jobStore.dataSource = myDS #==============================================================
#Configure DataSource
#==============================================================
org.quartz.dataSource.myDS.driver = com.mysql.jdbc.Driver
org.quartz.dataSource.myDS.URL = jdbc\:mysql\://localhost\:/quartz?useUnicode\=true&characterEncoding\=UTF-
org.quartz.dataSource.myDS.user = root
org.quartz.dataSource.myDS.password =
org.quartz.dataSource.myDS.maxConnections =
#org.quartz.dataSource.myDS.connectionProvider.class = org.quartz.utils.PoolingConnectionProvider #==============================================================
#Configure ThreadPool
#==============================================================
org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount =
org.quartz.threadPool.threadPriority =
org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread =true
如何解决quartz在集群下出现的资源抢夺现象的更多相关文章
- quartz在集群环境下的最终解决方案
在集群环境下,大家会碰到一直困扰的问题,即多个 APP 下如何用 quartz 协调处理自动化 JOB . 大家想象一下,现在有 A , B , C3 台机器同时作为集群服务器对外统一提供 SERVI ...
- 序列化人人网框架下的DAO?也就是在Spring下序列化DAO的问题(spring+quartz集群下)
人人网框架地址:http://code.google.com/p/paoding-rose/ 问题发生: 用Quartz作集群时用JobDataMap传递DAO,提示DAO未序列化,可框架的DAO为接 ...
- 【淘淘】Quartz之集群利弊
一.前言: 虽然单个Quartz实例能给予我们很好的任务job调度能力,但它不能满足典型的企业需求,如可伸缩性.高可靠性满足.假如你需要故障转移的能力并能运行日益增多的 Job,Quartz集群势必成 ...
- Java应用集群下的定时任务处理方案(mysql)
Java应用集群下的定时任务处理方案(mysql) 因为自己有csdn和博客园两个博客, 所以两边都会发一下. csdn地址: http://blog.csdn.net/u012881584/ar ...
- Spring Boot Quartz 分布式集群任务调度实现
Spring Boot Quartz 主要内容 Spring Scheduler 框架 Quartz 框架,功能强大,配置灵活 Quartz 集群 mysql 持久化定时任务脚本(tables_mys ...
- 定时组件quartz系列<二>quartz的集群原理
1.基本信息: Quartz是一个开源的作业调度框架,它完全由java写成,并设计用于J2Se和J2EE应用中.它提供了巨大的灵活性而不牺牲简单性.你能够用它 来为执行一个作业而创建简单的或 ...
- 浅析Quartz的集群配置
浅析Quartz的集群配置(一) 收藏人:Rozdy 2015-01-13 | 阅:1 转:22 | 来源 | 分享 1 基本信息 摘要:Quar ...
- Redis集群下过期key监听
1. 前言 在使用redis集群时,发现过期key始终监听不到.网上也没有现成的解决方案.于是想,既然不能监听集群,那我可以建立多个redis连接,分别对每个redis的key过期进行监听.以上做法可 ...
- 解决应用服务器变为集群后的Session问题
2.2.4.2 解决应用服务器变为集群后的Session问题 先来看一下什么是Session. 用户使用网站的服务,基本上需要浏览器与Web 服务器的多次交互.HTTP 协议本身是无状态的,需要基于H ...
随机推荐
- 谈一下思考,关于mybatis中<foreach collection="list">中list得来的原因 没看到官方说明
<foreach> 是在sql语句中进行多个id查询 时用到的,因为mybatis代替jdbc和hibernate, 使用 在xml文件中编写sql语句,这是一个标签文件.然后在 dao层 ...
- 【tensorflow:Google】二、Tensorflow环境搭建
2.1 Tensorflow 主要依赖包 2.1.1 Protocol Buffer 结构化数据序列化的过程,另外的工具:XML, JSON, 区别:二进制(不可读):先定义数据格式,还原的时候将需要 ...
- H264的编解码流程?
- Drools7在Intellij IDEA下的引入静态方法错误提示
问题 在Intellij IDEA 2016下,默认安装了Drools的插件,但使用Drools7(其他版本应该也有问题)时发现,在DRL文件中引入的静态方法IDEA会提示"Cannot r ...
- 日常生活小技巧 -- 惠普 Windows10 进入安全模式
今天手贱,是真的很贱.将用户模式从管理员组改为标准用户 方法是:WIN+R 打开 control userpasswords2 然后出现了用户账户控制,你要允许此应用对你的设备进行更改吗?最关键的是没 ...
- Ubuntu使用PlayOnLinux笔记
playonlinux官网:https://www.playonlinux.com/en/ 帮助文档:https://www.playonlinux.com/en/documentation.html ...
- 第二章 Linux目录介绍
一级目录 /bin (普通用户)二进制命令所在目录 备注 /boot LINUX内核及系统引导程序所需的文件目录 常见分区:128M swap内存的1.5倍 /dev 设备文件的目录 比如声卡.磁盘. ...
- GIL、死锁与递归锁
一.互斥锁 用互斥锁,目的:局部串行(保护自己的数据 进程之间数据不共享,但是共享同一套文件系统,所以访问同一个文件,或同一个打印终端,是没有问题的,竞争带来的结果就是错乱,如何控制,就是加锁处理(即 ...
- python学习之多线程(一)
引入线程包或者命名空间import threading 一:建立一个简单的线程程序 import time, threading def test(): print('thread %s is ...
- admins.py总结比较,转
转:http://blog.csdn.net/pipisorry/article/details/46764495