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 ...
随机推荐
- lintcode-401-排序矩阵中的从小到大第k个数
401-排序矩阵中的从小到大第k个数 在一个排序矩阵中找从小到大的第 k 个整数. 排序矩阵的定义为:每一行递增,每一列也递增. 样例 给出 k = 4 和一个排序矩阵: [ [1 ,5 ,7], [ ...
- 如何在一台 web 服务器上注册CA证书
试验环境介绍(CA的主机为192.168.23.10.httpd的主机为:192.168.23.11) 1:新建一台web服务器,主机名为www yum install -y httpd 2:生成 ...
- Struts2(二)
以下内容是基于导入struts2-2.3.32.jar包来讲的 1.关于StrutsPrepareAndExecuteFilter 启动StrutsPrepareAndExecuteFilter时加载 ...
- String、Date、Calendar之间的转换
1.String.Date.Calendar之间的转换 要用到格式化类SimpleDateFormat package com.rong.se; import java.text.ParseExcep ...
- CCF——数列分段201509-1
问题描述 给定一个整数数列,数列中连续相同的最长整数序列算成一段,问数列中共有多少段? 输入格式 输入的第一行包含一个整数n,表示数列中整数的个数. 第二行包含n个整数a1, a2, …, an,表示 ...
- 上传web端——个人项目
我用visual studio新建了一个web窗口,如图: 然后这里是系统自带的代码: [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile ...
- 【前端学习笔记05】JavaScript数据存储Cookie相关方法封装
//Cookie设置 //设置新cookie function setCookie(name,value,duration){ var date = new Date(); date.setTime( ...
- 【Python】Python流程控制
1)if条件测试 Python的比较操作 所有的Python对象都支持比较操作 测试操作符('=='操作符测试值的相等性: 'is'表达式测试对象的一致性) Python中不同类型的比较方法 数字:通 ...
- C++解析(27):数组、智能指针与单例类模板
0.目录 1.数组类模板 1.1 类模板高效率求和 1.2 数组类模板 1.3 堆数组类模板 2.智能指针类模板 2.1 使用智能指针 2.2 智能指针类模板 3.单例类模板 3.1 实现单例模式 3 ...
- 3294 [SCOI2016]背单词
题目描述 Lweb 面对如山的英语单词,陷入了深深的沉思,”我怎么样才能快点学完,然后去玩三国杀呢?“.这时候睿智的凤老师从远处飘来,他送给了 Lweb 一本计划册和一大缸泡椒,他的计划册是长这样的: ...