Quartz集群配置
先看看quartz的持久化基本介绍:
由上可见,我们需要创建quartz要用的数据库表,此sql文件在:quartz-1.8.6\docs\dbTables。此文件夹下有各个数据库的sql文件,mysql选择tables_mysql.sql。创建相应表。
接下来新建quartz.properties来覆盖jar包中的此文件,新的properties文件放在src的根目录下即可。下面是文件内容:
- #==============================================================
- #Configure Main Scheduler Properties
- #==============================================================
- org.quartz.scheduler.instanceName = quartzScheduler
- org.quartz.scheduler.instanceId = AUTO
- #==============================================================
- #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 = 20000
- org.quartz.jobStore.dataSource = myDS
- #==============================================================
- #Configure DataSource
- #==============================================================
- org.quartz.dataSource.myDS.driver = com.mysql.jdbc.Driver
- org.quartz.dataSource.myDS.URL = jdbc:mysql://192.168.20.195:3306/database?useUnicode=true&characterEncoding=UTF-8
- org.quartz.dataSource.myDS.user = root
- org.quartz.dataSource.myDS.password = 123456
- org.quartz.dataSource.myDS.maxConnections = 30
- #==============================================================
- #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
#==============================================================
#Configure Main Scheduler Properties
#==============================================================
org.quartz.scheduler.instanceName = quartzScheduler
org.quartz.scheduler.instanceId = AUTO #==============================================================
#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 = 20000
org.quartz.jobStore.dataSource = myDS #==============================================================
#Configure DataSource
#==============================================================
org.quartz.dataSource.myDS.driver = com.mysql.jdbc.Driver
org.quartz.dataSource.myDS.URL = jdbc:mysql://192.168.20.195:3306/database?useUnicode=true&characterEncoding=UTF-8
org.quartz.dataSource.myDS.user = root
org.quartz.dataSource.myDS.password = 123456
org.quartz.dataSource.myDS.maxConnections = 30 #==============================================================
#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
可以看到除了数据源、线程池等配置外,我们指定了一个scheduler实例,实例ID为自动分配。
- #==============================================================
- #Configure Main Scheduler Properties
- #==============================================================
- org.quartz.scheduler.instanceName = quartzScheduler
- org.quartz.scheduler.instanceId = AUTO
#==============================================================
#Configure Main Scheduler Properties
#==============================================================
org.quartz.scheduler.instanceName = quartzScheduler
org.quartz.scheduler.instanceId = AUTO
此外,指定了集群相应配置,检查间隔为20s:
- org.quartz.jobStore.isClustered = true
- org.quartz.jobStore.clusterCheckinInterval = 20000
org.quartz.jobStore.isClustered = true
org.quartz.jobStore.clusterCheckinInterval = 20000
最后配置applicant-context.xml文件。这里特别要注意一点:
所以我们要自己实现MethodInvokingJobDetailFactoryBean 的功能,这里用MyDetailQuartzJobBean 替换。
- 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;
- }
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;
}
终于到配置spring文件这步了
- <bean id="mapScheduler" lazy-init="false" autowire="no"
- class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
- <property name="triggers">
- <list>
- <ref bean="dailyTrigger" />
- <ref bean="billCountTrigger" />
- <ref bean="userAcctTrigger" />
- </list>
- </property>
- <property name="applicationContextSchedulerContextKey" value="applicationContext" />
- <property name="configLocation" value="classpath:quartz.properties" />
- </bean>
- <bean id="dailyBillJob" class="com.***.job.DailyBillJob" />
- <bean id="dailyBillJobDetail" class="org.springframework.scheduling.quartz.JobDetailBean">
- <property name="jobClass">
- <value>com.autelan.auteview.lib.util.MyDetailQuartzJobBean
- </value>
- </property>
- <property name="jobDataAsMap">
- <map>
- <entry key="targetObject" value="dailyBillJob" />
- <entry key="targetMethod" value="execute" />
- </map>
- </property>
- </bean>
- <bean id="dailyTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
- <property name="jobDetail">
- <ref bean="dailyBillJobDetail" />
- </property>
- <property name="cronExpression">
- <value>11 11 11 * * ?</value>
- </property>
- </bean>
- // 转载请注明出处http://forhope.iteye.com/blog/1398990
<bean id="mapScheduler" lazy-init="false" autowire="no"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="dailyTrigger" />
<ref bean="billCountTrigger" />
<ref bean="userAcctTrigger" />
</list>
</property>
<property name="applicationContextSchedulerContextKey" value="applicationContext" />
<property name="configLocation" value="classpath:quartz.properties" />
</bean> <bean id="dailyBillJob" class="com.***.job.DailyBillJob" /> <bean id="dailyBillJobDetail" class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass">
<value>com.autelan.auteview.lib.util.MyDetailQuartzJobBean
</value>
</property>
<property name="jobDataAsMap">
<map>
<entry key="targetObject" value="dailyBillJob" />
<entry key="targetMethod" value="execute" />
</map>
</property>
</bean> <bean id="dailyTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail">
<ref bean="dailyBillJobDetail" />
</property>
<property name="cronExpression">
<value>11 11 11 * * ?</value>
</property>
</bean>
Quartz集群配置的更多相关文章
- Spring+quartz集群配置,Spring定时任务集群,quartz定时任务集群
Spring+quartz集群配置,Spring定时任务集群,quartz定时任务集群 >>>>>>>>>>>>>> ...
- (4) Spring中定时任务Quartz集群配置学习
原 来配置的Quartz是通过spring配置文件生效的,发现在非集群式的服务器上运行良好,但是将工程部署到水平集群服务器上去后改定时功能不能正常运 行,没有任何错误日志,于是从jar包.JDK版本. ...
- Spring集成quartz集群配置总结
1.spring-quartz.xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE be ...
- Spring+quartz 实现定时任务job集群配置
为什么要有集群定时任务? 因为如果多server都触发相同任务,又同时执行,那在99%的场景都是不适合的.比如银行每晚24:00都要汇总营业额.像下面3台server同时进行汇总,最终计算结果可能是真 ...
- Spring+quartz 实现定时任务job集群配置【原】
为什么要有集群定时任务? 因为如果多server都触发相同任务,又同时执行,那在99%的场景都是不适合的.比如银行每晚24:00都要汇总营业额.像下面3台server同时进行汇总,最终计算结果可能是真 ...
- Quartz集群原理及配置应用
1.Quartz任务调度的基本实现原理 Quartz是OpenSymphony开源组织在任务调度领域的一个开源项目,完全基于Java实现.作为一个优秀的开源调度框架,Quartz具有以下特点: (1) ...
- 浅析Quartz的集群配置
浅析Quartz的集群配置(一) 收藏人:Rozdy 2015-01-13 | 阅:1 转:22 | 来源 | 分享 1 基本信息 摘要:Quar ...
- 【原理、应用】Quartz集群原理及配置应用
一.Quartz任务调度的基本实现原理 Quartz是OpenSymphony开源组织在任务调度领域的一个开源项目,完全基于Java实现.作为一个优秀的开源调度框架,Quartz具有以下特点: 强大的 ...
- quartz集群 定时任务 改成可配置
前面的博文中提到的quartz集群方式会有以下缺点: 1.假设配置了3个定时任务,job1,job2,job3,这时数据库里会有3条job相关的记录,如果下次上线要停掉一个定时任务job1,那即使定时 ...
随机推荐
- SharePoint 2013 Error - File names can't contain the following characters: & " ? < > # {} % ~ / \.
错误截图: 错误信息: --------------------------- Message from webpage --------------------------- File names ...
- SharePoint 2013 搭建负载均衡(NLB)
服务器架构(三台虚机:AD和Sql在一台,前端两台) DC.Sql Server,其中包括:AD.DNS.DHCP服务(非必须): SPWeb01,其中包括:IIS.SharePoint: SPWeb ...
- openssh/ntp/ftp漏洞
这3种漏洞常规加固都要对应操作系统打官方漏洞升级包.既然这么说那下面就是不常规的: Openssh: 改ssh版本:whereis ssh //查看ssh目录cd 到该目录cp ssh ssh.bak ...
- Android APK的安装
打开packages\apps\PackageInstaller下的清单文件 <?xml version="1.0" encoding="utf-8"?& ...
- xcode7无证书真机调试 Error: An App ID with identifier "*" is not avaliable. Please enter a different string.
1. Error: An App ID with identifier "*" is not avaliable. Please enter a different string. ...
- 高仿精仿手机版QQ空间应用源码
说明:本次QQ空间更新了以前非常基础的代码 更新内容一 更新了登陆界面二 增加了输入时密码时和登陆成功后播放音频的效果三 增加了导航条渐隐的效果(和真实QQ空间的导航条一样,首先透明,当tablev ...
- 自定义button
改变button内部label和imageView的frame - (CGRect)titleRectForContentRect:(CGRect)contentRect - (CGRect)imag ...
- 开启Apache mod_rewrite模块完全解答
启用mod_rewrite模块 在conf目录的httpd.conf文件中找到 LoadModule rewrite_module modules/mod_rewrite.so 将这一行前面的#去掉. ...
- [20140928]创建连接到MySQL的连接服务器
首先要安装 mysql odbc 然后 odbc下创建DSN,并且要在系统DSN下. 最后执行 exec sp_addlinkedserver @server= 'XY', --这是链 ...
- PHP MSSQL 分页实例(刷新)
<?php/* '页面说明:*/ $link=mssql_connect("MYSQL2005","sa","123456") or ...