java资源争夺_如何解决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文件。

f9880c101d3695e63742114031ebfff8.png

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类。

importjava.lang.reflect.Method;importorg.apache.commons.logging.Log;importorg.apache.commons.logging.LogFactory;importorg.quartz.JobExecutionContext;importorg.quartz.JobExecutionException;importorg.springframework.context.ApplicationContext;importorg.springframework.scheduling.quartz.QuartzJobBean;public class MyDetailQuartzJobBean extendsQuartzJobBean {protected final Log logger =LogFactory.getLog(getClass());privateString targetObject;privateString targetMethod;privateApplicationContext ctx;

@Overrideprotected voidexecuteInternal(JobExecutionContext context)throwsJobExecutionException {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,newObject[] {context});

}catch(SecurityException e) {

logger.error(e);

}catch(NoSuchMethodException e) {

logger.error(e);

}

}catch(Exception e) {throw newJobExecutionException(e);

}

}public voidsetApplicationContext(ApplicationContext applicationContext) {this.ctx =applicationContext;

}public voidsetTargetObject(String targetObject) {this.targetObject =targetObject;

}public voidsetTargetMethod(String targetMethod) {this.targetMethod =targetMethod;

}

}

5.编写要定时执行的代码,详见StudentTimer.java。

importjava.util.Date;importorg.quartz.Job;importorg.quartz.JobExecutionContext;importorg.quartz.JobExecutionException;importorg.springframework.stereotype.Service;

@Servicepublic class StudentTimer implementsJob {

@Overridepublic void execute(JobExecutionContext arg0) throwsJobExecutionException {//TODO Auto-generated method stub

System.out.println(new Date()+"Student中的execute正在执行...");

}

}

注意:如果没有实现Job接口,则方法参数必须为JobExecutionContext arg0,如下:

importjava.util.Date;importorg.quartz.Job;importorg.quartz.JobExecutionContext;importorg.quartz.JobExecutionException;importorg.springframework.stereotype.Service;

@Servicepublic classStudentTimer {public void update(JobExecutionContext arg0) throwsJobExecutionException {//TODO Auto-generated method stub

System.out.println(new Date()+"Student中的update正在执行...");

}

}

6.在application-context.xml中进行配置,详见timerTask.xml。

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"

>

com.core.Util.MyDetailQuartzJobBean

0/20 * * * * ?

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 = 20000org.quartz.jobStore.dataSource =myDS#==============================================================

#Configure DataSource

#==============================================================

org.quartz.dataSource.myDS.driver = com.mysql.jdbc.Driver

org.quartz.dataSource.myDS.URL = jdbc\:mysql\://localhost\:3306/quartz?useUnicode\=true&characterEncoding\=UTF-8org.quartz.dataSource.myDS.user =root

org.quartz.dataSource.myDS.password = 123org.quartz.dataSource.myDS.maxConnections = 30

#org.quartz.dataSource.myDS.connectionProvider.class = org.quartz.utils.PoolingConnectionProvider

#==============================================================

#Configure ThreadPool

#==============================================================

org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool

org.quartz.threadPool.threadCount = 10org.quartz.threadPool.threadPriority = 5org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread =true

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值