01、可能是目前最简单,最详细的Quartz集群搭建了

                                               ========= 本文,只涉及到quartz集群搭建应用 ==========

1,下载quartz集群数据库表压缩包。

下载地址:链接: https://pan.baidu.com/s/1y6rOUfUuXg189Z4Fta3tyg 密码: j29f

解压打开文件夹,选择对应的数据库表sql脚本:

01、可能是目前最简单,最详细的Quartz集群搭建了_第1张图片

2,选择对应的sql脚本。到数据库里面执行创建表。我这里是mysql数据库。

01、可能是目前最简单,最详细的Quartz集群搭建了_第2张图片

3,编写job类中可以使用Spring自动注入

package com.cong.quartz;

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;

/**
 * @author 
 * @description job类支持spring的自动注入
 */
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;
    }
}

3.1,job类

package com.cong.quartz;

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

import com.cong.service.GoodsOrderService;

public class GoodsOrderStatusQuartz extends QuartzJobBean{

	@Autowired
	private GoodsOrderService goodsOrderService;
	
	@Override
	protected void executeInternal(JobExecutionContext context)throws JobExecutionException {
		try {	
			goodsOrderService.synOrderStatus();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
}

4,配置quartz.properties文件

#==============================================================    
#Configure Main Scheduler Properties    
#==============================================================     
org.quartz.scheduler.instanceName = quartzScheduler
org.quartz.scheduler.instanceId = AUTO
  
#==============================================================    
#Configure JobStore    
#==============================================================   
org.quartz.jobStore.misfireThreshold = 60000
org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX
org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.StdJDBCDelegate
org.quartz.jobStore.tablePrefix = QRTZ_
org.quartz.jobStore.maxMisfiresToHandleAtATime=10
org.quartz.jobStore.isClustered = true 
org.quartz.jobStore.clusterCheckinInterval = 20000
   
#==============================================================    
#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

5,整合spring与quartz的xml配置文件



	
    
	  
        
        
        
        
        
        
           
          
        
         
        
            
                        
        
              
                
            
          
      
	
	
    
	
				
		
	
	
		
		
	
    
    

到此,quartz的集群就搭建完了。是不是很简单呢?

可能遇到的问题:

        数据库版本可能与驱动包不匹配。导致quartz抛此异常:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'OPTION SQL_SELECT_LIMIT=DEFAULT' at line 1

解决方案:

   把驱动包换成对应的版本。如:

你可能感兴趣的:(可能是最简单)