Spring配置集群定时任务

时间:2021-09-24 14:59:35

正常配置定时任务的时候配置定时任务调度工厂的代码如下

<bean id="" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="" />
<ref bean=""/>
</list>
</property>
</bean>

当项目部署到集群的时候会出现定时任务多次执行的情况

这时候需要用到Spring定时任务的集群功能,代码如下

<bean id="" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="applicationContextSchedulerContextKey" value="applicationContext" />
<property name="configLocation" value="classpath://quartz.properties"></property>
<property name="overwriteExistingJobs" value="true"/>
<property name="triggers">
<list>
<ref bean="" />
<ref bean=""/>
</list>
</property>
</bean>

集群实例的 quartz.properties 文件示例

#==============================================================
#Configure Main Scheduler Properties
#==============================================================
org.quartz.scheduler.instanceName = TestScheduler1
org.quartz.scheduler.instanceId = instance_one
#==============================================================
#Configure ThreadPool
#==============================================================
org.quartz.threadPool.class = org.quartz.simpl.Simple ThreadPool
org.quartz.threadPool.threadCount = 5
org.quartz.threadPool.threadPriority = 5
#==============================================================
#Configure JobStore
#==============================================================
org.quartz.jobStore.misfireThreshold = 60000
org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX
org.quartz.jobStore.driverDelegateClass =
org.quartz.impl.jdbcjobstore.MSSQLDelegate
org.quartz.jobStore.tablePrefix = QRTZ_
org.quartz.jobStore.dataSource = myDS org.quartz.jobStore.isClustered = true
org.quartz.jobStore.clusterCheckinInterval = 20000
#==============================================================
#Non-Managed Configure Datasource
#==============================================================
org.quartz.dataSource.myDS.driver = net.sourceforge.jtds.jdbc.Driver
org.quartz.dataSource.myDS.URL = jdbc:jtds:sqlserver://localhost:1433/quartz
org.quartz.dataSource.myDS.user = admin
org.quartz.dataSource.myDS.password = admin
org.quartz.dataSource.myDS.maxConnections = 10

也可以使用重写SchedulerFactoryBean类的方法来实现Quarz的集群

<bean id="" class="framework.base.SchedulerFactoryBean">
<property name="applicationContextSchedulerContextKey">
<value>applicationContext</value>
</property>
<property name="triggers">
<list>
<ref bean="extractTrigger"/>
</list>
</property>
</bean>
package framework.scheduling;

import java.io.InputStream;
import java.io.Serializable; import org.springframework.core.io.InputStreamResource;
import org.springframework.core.io.Resource; public class SchedulerFactoryBean extends org.springframework.scheduling.quartz.SchedulerFactoryBean implements Serializable{ private static final long serialVersionUID = -1160717800136961172L; public SchedulerFactoryBean() {
super();
InputStream in = getClass().getClassLoader().getResourceAsStream("scheduling/quartz.properties");
if(in!=null){
Resource resource = new InputStreamResource(in);
setConfigLocation(resource);
}else{
logger.warn("没有发现集群的quartz.properties配置文件。");
}
setOverwriteExistingJobs(true);
}
}

配置好Quarz集群后可以保证项目在集群环境下定时任务的正常执行