Spring集成quartz集群配置总结
1.spring-quartz.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <!-- expiration notice -->
<bean id="expirationNoticeService" class="org.guyezhai.demo.service.scheduler.ExpirationNoticeService"/>
<bean id="expirationNoticeDetail" class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass">
<value>org.guyezhai.modules.quartz.DXQuartzJobBean</value>
</property>
<property name="jobDataMap">
<map>
<entry key="targetObject" value="expirationNoticeService" />
<entry key="targetMethod" value="execute" />
<entry key="concurrent" value="false" />
</map>
</property>
</bean>
<bean id="expirationNoticeTriggers" class="org.guyezhai.modules.quartz.InitCronTrigger">
<constructor-arg value="expirationNoticeTriggers"/>
<property name="jobDetail" ref="expirationNoticeDetail" />
</bean> <bean id="scheduler" lazy-init="true" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="configLocation" value="classpath:quartz.properties"/>
<property name="triggers">
<list>
<ref local="expirationNoticeTriggers"/>
</list>
</property>
<property name="applicationContextSchedulerContextKey" value="applicationContext" />
</bean> </beans>
2.quartz.properties
#============================================================================
# Configure Main Scheduler Properties
#============================================================================
org.quartz.scheduler.instanceName = DEMO
org.quartz.scheduler.instanceId = AUTO #============================================================================
# Configure ThreadPool
#============================================================================
org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount =
org.quartz.threadPool.threadPriority = #============================================================================
# Configure JobStore
#============================================================================
org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX
org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.StdJDBCDelegate
org.quartz.jobStore.useProperties = false
org.quartz.jobStore.dataSource = demo
org.quartz.jobStore.tablePrefix = qrtz_
org.quartz.jobStore.misfireThreshold =
org.quartz.jobStore.dontSetAutoCommitFalse = false
org.quartz.jobStore.isClustered = true
org.quartz.jobStore.clusterCheckinInterval = #============================================================================
# Configure Datasources
#============================================================================
org.quartz.dataSource.deptusercert.driver = com.mysql.jdbc.Driver
org.quartz.dataSource.deptusercert.URL = jdbc\:mysql\://127.0.0.1\:3306/demo
org.quartz.dataSource.deptusercert.user = root
org.quartz.dataSource.deptusercert.password = sa
org.quartz.dataSource.deptusercert.maxConnections =
org.quartz.dataSource.deptusercert.validationQuery = select from dual
3.InitCronTrigger.java
package org.guyezhai.modules.quartz; import org.guyezhai.demo.dao.scheduler.SchedulerInfoDao.SchedulerInfoDao;
import org.guyezhai.demo.entity.po.scheduler.SchedulerInfo.SchedulerInfo;
import org.guyezhai.modules.spring.context.SpringContextHolder;
import org.guyezhai.modules.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean; /**
* 初始化CronTrigger
*/
public class InitCronTrigger extends CronTriggerFactoryBean {
private static Logger logger = LoggerFactory.getLogger(InitCronTrigger.class); private String triggerid;
private SchedulerInfoDao schedulerInfoDao; public InitCronTrigger(String triggerid) {
super();
logger.info("------InitCronTrigger.InitCronTrigger():" + triggerid);
try {
schedulerInfoDao = SpringContextHolder.getBean(SchedulerInfoDao.class); String cronExpression = getCronExpressionFromDB(triggerid);
if (StringUtils.isNotBlank(cronExpression)) {
this.setCronExpression(cronExpression);
// logger.info("------setCronExpression:" + this.getCronExpression());
}
} catch (Exception e) {
e.printStackTrace();
} } /**
* 从数据库中获取cronExpression
*
* @param triggerid
* @return
*/
private String getCronExpressionFromDB(String triggerid) {
SchedulerInfo schedulerInfo = schedulerInfoDao.get(triggerid);
return null == schedulerInfo ? "" : schedulerInfo.getCronExpression();
} public String getTriggerid() {
return triggerid;
} public void setTriggerid(String triggerid) {
this.triggerid = triggerid;
}
}
4.DXQuartzJobBean.java
package org.guyezhai.modules.quartz; import java.lang.reflect.Method; import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.scheduling.quartz.QuartzJobBean; /**
* 解决quartz集群class序列化的问题
*/
public class DXQuartzJobBean extends QuartzJobBean {
private static Logger logger = LoggerFactory.getLogger(DXQuartzJobBean.class); private String targetMethod;
private String targetObject;
private ApplicationContext applicationContext; @Override
protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
try {
logger.warn("INFO: execute [" + targetObject + "] at once...");
Object otargetObject = applicationContext.getBean(targetObject);
Method m = null;
try {
m = otargetObject.getClass().getMethod(targetMethod, new Class[] {});
m.invoke(otargetObject, new Object[] {});
} catch (SecurityException e) {
logger.error("DXQuartzJobBean.executeInternal()", e);
} catch (NoSuchMethodException e) {
logger.error("DXQuartzJobBean.executeInternal()", e);
}
} catch (Exception e) {
// throw new JobExecutionException(e);
logger.error("DXQuartzJobBean.executeInternal()", e);
}
} public void setApplicationContext(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
} public void setTargetObject(String targetObject) {
this.targetObject = targetObject;
} public void setTargetMethod(String targetMethod) {
this.targetMethod = targetMethod;
}
}
Spring集成quartz集群配置总结的更多相关文章
- Spring+quartz集群配置,Spring定时任务集群,quartz定时任务集群
Spring+quartz集群配置,Spring定时任务集群,quartz定时任务集群 >>>>>>>>>>>>>> ...
- Spring集成Redis集群(含spring集成redis代码)
代码地址如下:http://www.demodashi.com/demo/11458.html 一.准备工作 安装 Redis 集群 安装参考: http://blog.csdn.net/zk6738 ...
- Spring Cloud Eureka集群配置及注意事项(Greenwich版本)
Spring Cloud Eureka集群配置及注意事项(Greenwich版本) 一·概述 Spring Cloud Netflix Eureka 是一个提供服务注册与发现的套件.服务提供者只需要将 ...
- (4) Spring中定时任务Quartz集群配置学习
原 来配置的Quartz是通过spring配置文件生效的,发现在非集群式的服务器上运行良好,但是将工程部署到水平集群服务器上去后改定时功能不能正常运 行,没有任何错误日志,于是从jar包.JDK版本. ...
- Quartz集群配置
先看看quartz的持久化基本介绍: 引用 1 大家都清楚quartz最基本的概念就是job,在job内调用具体service完成具体功能,quartz需要把每个job存储起来,方便调度,quartz ...
- spring boot + quartz 集群
spring boot bean配置: @Configuration public class QuartzConfig { @Value("${quartz.scheduler.insta ...
- Springboot2.X集成Quartz集群
为什么要使用Quzrtz集群 在项目进行集群部署时,如果业务在执行中存在互斥关系,没有对定时任务进行统一管理,就会引起业务的多次执行,不能满足业务要求.这时就需要对任务进行管理,要保证一笔业务在所有的 ...
- spring 使用redis集群配置
上面两篇介绍了redis集群的配置合一些基本的概念,所以接下来当然是要在项目中使用咯,redis的java支持已经做的非常好了,所以我们来试着使用这些api来进行redis的操作,首先我们需要操作re ...
- 搭建高可用rabbitmq集群及spring boot实现集群配置
java spring boot配置: //具体参看了配置的源码 org.springframework.boot.autoconfigure.amqp.RabbitProperties //Rabb ...
随机推荐
- Numpy and Pandas
安装 视频链接:https://morvanzhou.github.io/tutorials/data-manipulation/np-pd/ pip install numpy pip instal ...
- MySql 8 命令
1-创建用户 create user 用户名@'%' identified by '密码'; create user 用户名@'localhost' identified by '密码'; 2-授 ...
- Hash开散列 拉链法
#include<iostream> #include<cstdio> using namespace std; const int maxn=1000007; struct ...
- HTML5 <meta> 标签属性,所有meta用法
基本标签 声明文档使用的字符编码:<meta charset="utf-8" /> 声明文档的兼容模式:<meta http-equiv="X-UA-C ...
- MMU 和 MPU的区别
S3C2440里面带的是MMU,而现在流行的Cortex-M3/4 里面带的是MPU. MMU vs MPU 内存是现代计算机最重要的组件之一.因此,它的内容不能被任何错误的应用所篡改.这个功能可以通 ...
- Java7 Fork-Join 框架:任务切分,并行处理
概要 现代的计算机已经向多CPU方向发展,即使是普通的PC,甚至现在的智能手机.多核处理器已被广泛应用.在未来,处理器的核心数将会发展的越来越多.虽然硬件上的多核CPU已经十分成熟,但是很多应用程序并 ...
- C++中关于new及动态内存分配的思考
如何实现一个malloc? malloc_tutorial.pdf ———————————————————————————————————— 我们知道,使用malloc/calloc等分配内存的函数时 ...
- Ajax+Js局部刷新
通过 AJAX,JavaScript 可使用 JavaScript 的 XMLHttpRequest 对象来直接与服务器进行通信.通过这个对象, JavaScript 可在不重载页面的情况与 Web ...
- 【移动支付】.NET微信扫码支付接入(模式二-NATIVE)
一.前言 经过两三天的琢磨总算完成了微信扫码支付功能,不得不感叹几句: 微信提供的DEMO不错,直接复制粘贴就可以跑起来了: 微信的配置平台我真是服了.公众平台.商户平台.开放平台,一个平 ...
- BZOJ3524 & LOJ2432:[POI2014]代理商Couriers——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=3524 https://loj.ac/problem/2432 给一个长度为n的序列a.1≤a[i] ...