spring+quartz+terracotta集群

使用terracotta3.7.5,里面包含了quartz2.1.7。
需要将quartz-2.1.7.jar和terracotta-toolkit-1.6-runtime-5.5.0.jar这两个jar包放到项目的lib下。也将spring的包放到项目的lib下。
配置quartz.properties文件

#============================================================================   
# Configure Main Scheduler Properties     
#============================================================================
org.quartz.scheduler.instanceName = DefaultQuartzScheduler   
#可为任何值,用在 JDBC JobStore 中来唯一标识实例,但是所有集群节点中必须相同。
org.quartz.scheduler.instanceId = AUTO  
#AUTO即可,基于主机名和时间戳来产生实例 ID
org.quartz.scheduler.skipUpdateCheck = true
#不检查更新
#============================================================================   
# 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 
#============================================================================   
# Configure JobStore     
#============================================================================
org.quartz.jobStore.misfireThreshold = 60000
org.quartz.jobStore.class = org.terracotta.quartz.TerracottaJobStore
#使用terracotta集群,就要用org.terracotta.quartz.TerracottaJobStore
org.quartz.jobStore.tcConfigUrl = 192.168.1.91:9510
#terracotta服务器的ip:端口

配置quartz的spring-quartz.xml文件



    
    
        
        
        
            
                
                
            
        
    
    
        
        
        
            
                
                
            
        
    
    
        
        
        
    
    
        
        
        
            
                
                
            
        
    
    
        
        
    

创建Job测试服务类

package com.hao.service;
import java.io.Serializable;
public class SimpleService implements Serializable {
    private static final long serialVersionUID = 1L;
    public void testMethod1() {
        //这里执行定时调度业务  
        System.out.println("testMethod1.......一");
    }
    public void testMethod2() {
        System.err.println("testMethod2.......二");
    }
}

SimpleService要实现Serializable接口
重写QuartzJobBean,因为MethodInvokingJobDetailFactoryBean不支持序列化

package com.hao;
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 DetailQuartzJobBean extends QuartzJobBean {
    private String targetObject;
    private String targetMethod;
    private ApplicationContext cxt;
    @Override
    protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
        try {
            //targetObject和targetMethod是spring-quartz.xml文件中jobDataAsMap属性的key
            this.setTargetObject((String) context.getMergedJobDataMap().get("targetObject"));
            this.setTargetMethod((String) context.getMergedJobDataMap().get("targetMethod"));
            //applicationContextKey是spring-quartz.xml文件中applicationContextSchedulerContextKey属性的value
            cxt = (ApplicationContext) context.getScheduler().getContext().get("applicationContextKey");
            Object otargetObject = cxt.getBean(targetObject);
            Method m = otargetObject.getClass().getMethod(targetMethod, new Class[] {});
            m.invoke(otargetObject, new Object[] {});
        } catch (Exception e) {
            e.printStackTrace();
            throw new JobExecutionException(e);
        }
    }
    public void setTargetObject(String targetObject) {
        this.targetObject = targetObject;
    }
    public void setTargetMethod(String targetMethod) {
        this.targetMethod = targetMethod;
    }
    public void setCxt(ApplicationContext cxt) {
        this.cxt = cxt;
    }
}

运行Quartz集群

package com.hao;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
    /**
     * @param args
     */
    public static void main(String[] args) {
        new ClassPathXmlApplicationContext(new String[] {"classpath:config/spring/spring-quartz.xml" });
    }
}

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