quartz集群配置

quartz有两种注入方式,MethodInvokingJobDetailFactoryBean和JobDetailBean。

这里我用的是JobDetailBean。(MethodInvokingJobDetailFactoryBean也试了下,无奈不成功,看网上有人说重写两个类文件,试了下也不行,只好用JobDetailBean了)

1.下载quartz-1.8.6包,包的的docs文件夹里有数据库建表sql,quartz集群需要将任务信息实例化到数据库中,然后各个节点从库中读取任务信息。

2.在src中添加quartz.properties文件。
org.quartz.scheduler.instanceName = DefaultQuartzScheduler
org.quartz.scheduler.instanceId = AUTO 

org.quartz.scheduler.rmi.export = false
org.quartz.scheduler.rmi.proxy = false
org.quartz.scheduler.wrapJobExecutionInUserTransaction = false

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

org.quartz.jobStore.misfireThreshold = 60000

org.quartz.jobStore.selectWithLockSQL=SELECT * FROM {0}LOCKS UPDLOCK WHERE LOCK_NAME \= ? 
org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX
org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.StdJDBCDelegate
org.quartz.jobStore.useProperties = false
org.quartz.jobStore.dataSource = quartzdataSource
org.quartz.jobStore.tablePrefix = QRTZ_

org.quartz.jobStore.isClustered = true
org.quartz.jobStore.clusterCheckinInterval = 20000

3.src中添加applicationContext-Quartz.xml文件,内容如下,quartz自带的连接池是DBCP,这个连接池问题很多性能也不好,所以改成了c3p0
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-2.5.xsd
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
        http://www.springframework.org/schema/context  
        http://www.springframework.org/schema/context/spring-context.xsd">

    <bean id="syncSpOriinfoService" class="com.jxet.quartz.service.sms.SyncSpOriinfoService" />
    
    <bean id="syncSpOriinfoBean" class="org.springframework.scheduling.quartz.JobDetailBean">
        <property name="jobClass">
            <value>com.jxet.quartz.client.SyncSpOriinfoServiceClient
            </value>
        </property>
        <!--采用jobDataAsMap方式进行quartzService注入 -->
        <property name="jobDataAsMap">
            <map>
                <entry key="targetObject" value="syncSpOriinfoService" />
                <entry key="targetMethod" value="syncSpOriinfo" />
            </map>
        </property>
    </bean>
    
    <bean id="syncSpOriinfoTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
        <property name="jobDetail">
            <ref bean="syncSpOriinfoBean" />
        </property>
        <!-- 程序启动10秒后运行 -->
        <property name="startDelay">
            <value>10000</value>
        </property>
        <!-- 1分钟启动一次 -->
        <property name="repeatInterval">
            <value>60000</value>
        </property>
    </bean>


    <bean id="quartzdataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
    destroy-method="close">
        <!-- c3p0连接线程池配置文件 -->        <property name="driverClass" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
        <property name="jdbcUrl" value="jdbc:sqlserver://192.168.32.160:1433;databaseName=quartz" />
        <property name="user" value="sa" />
        <property name="password" value="123" />
    </bean>
    <bean id="timerFactoryBean"
        class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="dataSource">    
            <ref bean="quartzdataSource" /> <!--Spring中对于的数据源-->   
        </property> 
        <property name="configLocation" value="classpath:quartz.properties"/> 
        <property name="triggers">
            <list>
                <ref bean="syncSpOriinfoTrigger" />  
            </list>
        </property>
        <property name="applicationContextSchedulerContextKey" value="applicationContext" />
    </bean>

</beans> 

4.根据 QuartzJobBean 来重写一个自己的类,然后使用 SPRING 把这个重写的类注入 appContext 中后,再使用 AOP 技术反射出原有的 quartzJobx( 就是开发人员原来已经做好的用于执行 QUARTZ 的 JOB 的执行类 ) 。
 public class SyncSpOriinfoServiceClient extends QuartzJobBean {

    private final Log log = LogFactory.getLog(SyncSpOriinfoServiceClient.class);

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

            log.info("execute [" + targetObject + "] at once>>>>>>");
            ApplicationContext ctx =  new ClassPathXmlApplicationContext(new String[]{"classpath:applicationContext.xml","classpath:applicationContext-quartz.xml"}); 
            Object otargetObject = ctx.getBean(targetObject);
            Method m = null;
            try {
                m = otargetObject.getClass().getMethod(targetMethod, new Class[]{});

                m.invoke(otargetObject, new Object[]{});
            }
            catch (SecurityException e) {
                log.error(e);
            }
            catch (NoSuchMethodException e) {
                log.error(e);
            }

        }
        catch (Exception e) {
            throw new JobExecutionException(e);
        }
    }

    private String targetObject;
    private String targetMethod;

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

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

} 

5.写业务service(syncSpOriinfoService),然后启动就ok了


需要注意的地方是,程序第一次启动时,会将任务信息实例化到数据库中,以后修改任务信息必须修改数据库中的任务,直接修改项目中的信息是没有用的。看来以后还得整个项目来维护了.

你可能感兴趣的:(quartz)