Spring配置集羣定時任務

正常配置定時任務的時候配置定時任務調度工廠的代碼以下java

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

當項目部署到集羣的時候會出現定時任務屢次執行的狀況spring

這時候須要用到Spring定時任務的集羣功能,代碼以下sql

<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 文件示例app

#==============================================================  
#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的集羣sqlserver

<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集羣后能夠保證項目在集羣環境下定時任務的正常執行spa

相關文章
相關標籤/搜索