美文网首页
spring+quartz+terracotta集群

spring+quartz+terracotta集群

作者: 那脸憔悴 | 来源:发表于2017-03-28 13:19 被阅读0次

使用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文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
                        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
                        http://www.springframework.org/schema/context
                        http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    <bean id="simpleService" class="com.hao.service.SimpleService" />
    <bean name="quartzScheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="applicationContextSchedulerContextKey" value="applicationContextKey" />
        <property name="configLocation" value="classpath:config/quartz.properties" />
        <property name="triggers">
            <list>
                <ref bean="trigger1" />
                <ref bean="trigger2" />
            </list>
        </property>
    </bean>
    <bean id="jobDetail1" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
        <property name="jobClass" value="com.hao.DetailQuartzJobBean" />
        <property name="durability" value="true" />
        <property name="jobDataAsMap">
            <map>
                <entry key="targetObject" value="simpleService" />
                <entry key="targetMethod" value="testMethod1" />
            </map>
        </property>
    </bean>
    <bean id="trigger1" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
        <property name="jobDetail" ref="jobDetail1" />
        <!-- 每分钟执行1次 -->
        <property name="cronExpression" value="0 0/1 * * * ?" />
    </bean>
    <bean id="jobDetail2" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
        <property name="jobClass" value="com.hao.DetailQuartzJobBean" />
        <property name="durability" value="true" />
        <property name="jobDataAsMap">
            <map>
                <entry key="targetObject" value="simpleService" />
                <entry key="targetMethod" value="testMethod2" />
            </map>
        </property>
    </bean>
    <bean id="trigger2" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
        <property name="jobDetail" ref="jobDetail2" />
        <property name="cronExpression" value="0 0/1 * * * ?" />
    </bean>
</beans>

创建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集群

      本文链接:https://www.haomeiwen.com/subject/ghsjottx.html