分布式spring-quartz

一、Quartz集群如何工作

一个 Quartz 集群中的每个节点是一个独立的 Quartz 应用,它又管理着其他的节点。意思是你必须对每个节点分别启动或停止。不像许多应用服务器的集群,独立的 Quartz 节点并不与另一其的节点或是管理节点通信。Quartz 应用是通过数据库表来感知到另一应用的。

图:表示了每个节点直接与数据库通信,若离开数据库将对其他节点一无所知


quartzCluster.png

二、如何搭建分布式Quartz

【step1:创建quartz DB】

下载quartz-2.2.3-distribution.tar,解压,找到quartz-2.2.3-distribution\quartz-2.2.3\docs\tables_mysql_innodb.sql,并执行

【step2:配置文件】

1、quartz.properties

#============================================================================
# Configure JobStore
# Using Spring datasource in quartzJobsConfig.xml
# Spring uses LocalDataSourceJobStore extension of JobStoreCMT
#============================================================================
org.quartz.jobStore.useProperties=true
org.quartz.jobStore.tablePrefix = QRTZ_
org.quartz.jobStore.isClustered = true
org.quartz.jobStore.clusterCheckinInterval = 20000
org.quartz.jobStore.misfireThreshold = 60000
org.quartz.jobStore.txIsolationLevelReadCommitted = true

# Change this to match your DB vendor
org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX
org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.StdJDBCDelegate


#============================================================================
# Configure Main Scheduler Properties
# Needed to manage cluster instances
#============================================================================
org.quartz.scheduler.instanceId=AUTO
org.quartz.scheduler.instanceName=MY_CLUSTERED_JOB_SCHEDULER
org.quartz.scheduler.rmi.export = false
org.quartz.scheduler.rmi.proxy = false


#============================================================================
# Configure ThreadPool
#============================================================================
org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount = 10
org.quartz.threadPool.threadPriority = 5
org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread = true

2、jdbc.properties

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://x.x.x.x:3306/matchdb?zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useOldAliasMetadataBehavior=true
jdbc.username=root
jdbc.password=latch
jdbc.maxConnectionCount=150
jdbc.minConnectionCount=5
jdbc.acquire.increment=5

3、spring-quartz-cluster.xml




    
    
    
    
    
        
        
        
    
    
    
        
    
    
    
    
        
        
        
     
         
        

        
        
    
        
            
        
        
            
                
                
            
        
    
   
    
    
        
        
        
    
  
     
    
        
        
        
     
    

4、spring-service-context.xml




    
    
    
    
    
    
    
    

    
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
     
    
    
    
    
        
    
    
        
        
    
    

     
    
    
    

  
【step3:创建job】

1、AutowiringSpringBeanJobFactory类是为了可以在scheduler中使用spring注解,如果不使用注解,可以不适用该类,而直接使用
SpringBeanJobFactory

package com.latech.matchData.impl;

import org.quartz.spi.TriggerFiredBundle;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.scheduling.quartz.SpringBeanJobFactory;
/**
 * 使job类支持spring的自动注入
 * @author Administrator
 *
 */
public class AutowiringSpringBeanJobFactory extends SpringBeanJobFactory implements ApplicationContextAware
{
    private transient AutowireCapableBeanFactory beanFactory;

    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException
    {
        beanFactory = applicationContext.getAutowireCapableBeanFactory();
    }


    @Override
    protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception
    {
        Object job = super.createJobInstance(bundle);
        beanFactory.autowireBean(job);
        return job;
    }
}

2、创建具体job

package com.latech.matchData.impl;

import org.quartz.DisallowConcurrentExecution;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.PersistJobDataAfterExecution;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;

import com.latech.matchData.api.IMatchDataService;

@PersistJobDataAfterExecution
@DisallowConcurrentExecution// 不允许并发执行
public class GetMatchDataJobs extends QuartzJobBean{
 //这里就是因为有上文中的AutowiringSpringBeanJobFactory才可以使用@Autowired注解,否则只能在配置文件中设置这属性的值
    @Autowired
    private IMatchDataService matchDataService;
    
    @Override
    protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
        matchDataService.addMatchInfoToDB();
    }

}

你可能感兴趣的:(分布式spring-quartz)