Spring集成quartz集群配置总结

时间:2024-01-08 13:04:14

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;
}
}