spring4与quartz2.2集群配置

1.引入quartz2.2的maven依赖

		
			org.quartz-scheduler
			quartz
			2.2.3
		
		
			org.quartz-scheduler
			quartz-jobs
			2.2.3
		

2.在src/main/resources目录下新建quartz.properties文件

#============================================================== 
#Configure Main Scheduler Properties 
#==============================================================
org.quartz.scheduler.instanceName = quartzScheduler
org.quartz.scheduler.instanceId = AUTO

#============================================================== 
#Configure JobStore 
#==============================================================
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.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

3.去官网下载quartz-2.2.3-distribution.tar.gz,解压后找到quartz-2.2.3/docs/dbTables/tables_mysql.sql,在数据库中执行它

4.新建两个java类:JobDetailBean和DemoJob

package org.webbase.common.quartz;

import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;

import java.lang.reflect.Method;

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.context.ApplicationContext;
import org.springframework.scheduling.quartz.QuartzJobBean;

public class JobDetailBean extends QuartzJobBean {

	private static final Logger logger = LogManager.getLogger(JobDetailBean.class.getName());

	private String targetObject;
	private String targetMethod;
	private ApplicationContext ctx;

	@Override
	protected void executeInternal(JobExecutionContext context) throws JobExecutionException {

		try {
			Object bean = ctx.getBean(targetObject);
			Method m = bean.getClass().getMethod(targetMethod);
			m.invoke(bean, null);
		} catch (Exception e) {
			logger.error(e);
		}
	}

	public void setApplicationContext(ApplicationContext applicationContext) {
		this.ctx = applicationContext;
	}

	public void setTargetObject(String targetObject) {
		this.targetObject = targetObject;
	}

	public void setTargetMethod(String targetMethod) {
		this.targetMethod = targetMethod;
	}
}

package org.webbase.demo.job;

import org.springframework.stereotype.Component;

@Component
public class DemoJob {

	public void execute() {
		System.out.println("demo job!");
	}
}

5.在spring配置文件中增加quartz的bean配置

	
        
		
		
        
            
                
                
            
        
    

    
		
		
	
	
	
	
        
		
	    
		
	    
	    
	     
		
	    
	        
	        	
	        
	    
	

配置完毕

你可能感兴趣的:(spring,spring,quartz,集群)