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集群配置总结的更多相关文章

  1. Spring+quartz集群配置,Spring定时任务集群,quartz定时任务集群

    Spring+quartz集群配置,Spring定时任务集群,quartz定时任务集群 >>>>>>>>>>>>>> ...

  2. Spring集成Redis集群(含spring集成redis代码)

    代码地址如下:http://www.demodashi.com/demo/11458.html 一.准备工作 安装 Redis 集群 安装参考: http://blog.csdn.net/zk6738 ...

  3. Spring Cloud Eureka集群配置及注意事项(Greenwich版本)

    Spring Cloud Eureka集群配置及注意事项(Greenwich版本) 一·概述 Spring Cloud Netflix Eureka 是一个提供服务注册与发现的套件.服务提供者只需要将 ...

  4. (4) Spring中定时任务Quartz集群配置学习

    原 来配置的Quartz是通过spring配置文件生效的,发现在非集群式的服务器上运行良好,但是将工程部署到水平集群服务器上去后改定时功能不能正常运 行,没有任何错误日志,于是从jar包.JDK版本. ...

  5. Quartz集群配置

    先看看quartz的持久化基本介绍: 引用 1 大家都清楚quartz最基本的概念就是job,在job内调用具体service完成具体功能,quartz需要把每个job存储起来,方便调度,quartz ...

  6. spring boot + quartz 集群

    spring boot bean配置: @Configuration public class QuartzConfig { @Value("${quartz.scheduler.insta ...

  7. Springboot2.X集成Quartz集群

    为什么要使用Quzrtz集群 在项目进行集群部署时,如果业务在执行中存在互斥关系,没有对定时任务进行统一管理,就会引起业务的多次执行,不能满足业务要求.这时就需要对任务进行管理,要保证一笔业务在所有的 ...

  8. spring 使用redis集群配置

    上面两篇介绍了redis集群的配置合一些基本的概念,所以接下来当然是要在项目中使用咯,redis的java支持已经做的非常好了,所以我们来试着使用这些api来进行redis的操作,首先我们需要操作re ...

  9. 搭建高可用rabbitmq集群及spring boot实现集群配置

    java spring boot配置: //具体参看了配置的源码 org.springframework.boot.autoconfigure.amqp.RabbitProperties //Rabb ...

随机推荐

  1. HDU 5195 DZY Loves Topological Sorting 拓扑排序

    题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5195 bc(中文):http://bestcoder.hdu.edu.cn/contests ...

  2. 多线程PV

    #include <STDIO.H> #include <windows.h> //#include "stdafx.h" #include <pro ...

  3. vue shorthands

    vue shorthands : & @ https://vuejs.org/v2/guide/syntax.html#Shorthands v-for https://vuejs.org/v ...

  4. 【bzoj3130】[Sdoi2013]费用流 二分+网络流最大流

    题目描述 Alice和Bob做游戏,给出一张有向图表示运输网络,Alice先给Bob一种最大流方案,然后Bob在所有边上分配总和等于P的非负费用.Alice希望总费用尽量小,而Bob希望总费用尽量大. ...

  5. 多线程---handlerthread

    当我们需要工作线程来操作的时候,很多时候会有同步问题,UI更新问题. Handle机制就是为了解决这个问题而产生的. android允许每个线程都有自己的消息队列,同时也可以是主线程消息队列. 但是很 ...

  6. sql语句左链接left join--3张表关联

    表A---------------------------------关联第一张表B-----------------------关联第二张表c select * fomr 表名A left join ...

  7. 洛谷 Roy&October之取石子

    题目背景 Roy和October两人在玩一个取石子的游戏. 题目描述 游戏规则是这样的:共有n个石子,两人每次都只能取pk 个(p为质数,k为自然数,且pk小于等于当前剩余石子数),谁取走最后一个石子 ...

  8. P2891 [USACO07OPEN]吃饭Dining(最大流+拆点)

    题目描述 Cows are such finicky eaters. Each cow has a preference for certain foods and drinks, and she w ...

  9. Ulipad和有道词典冲突解决方法

    问题现象 Ulipad和目前版本的有道词典有冲突,表现为先开有道词典,Ulipad就无法运行. 解决方法 找到Ulipad安装目录下的config.ini,添加以下两行: [server] port= ...

  10. 【Luogu1912】【NOI2009】诗人小G(动态规划)

    [Luogu1912][NOI2009]诗人小G(动态规划) 题面 洛谷 题解 原来\(NOI\)这么多神仙题... 考虑一个极其明显的\(dp\) 设\(f[i]\)表示前\(i\)个句子产生的最小 ...