一、Quartz集群如何工作
一个 Quartz 集群中的每个节点是一个独立的 Quartz 应用,它又管理着其他的节点。意思是你必须对每个节点分别启动或停止。不像许多应用服务器的集群,独立的 Quartz 节点并不与另一其的节点或是管理节点通信。Quartz 应用是通过数据库表来感知到另一应用的。
图:表示了每个节点直接与数据库通信,若离开数据库将对其他节点一无所知
quartzCluster.png
二、如何搭建分布式Quartz
【step1:创建quartz DB】
下载quartz-2.2.3-distribution.tar,解压,找到quartz-2.2.3-distribution\quartz-2.2.3\docs\tables_mysql_innodb.sql,并执行
【step2:配置文件】
1、quartz.properties
#============================================================================
# Configure JobStore
# Using Spring datasource in quartzJobsConfig.xml
# Spring uses LocalDataSourceJobStore extension of JobStoreCMT
#============================================================================
org.quartz.jobStore.useProperties=true
org.quartz.jobStore.tablePrefix = QRTZ_
org.quartz.jobStore.isClustered = true
org.quartz.jobStore.clusterCheckinInterval = 20000
org.quartz.jobStore.misfireThreshold = 60000
org.quartz.jobStore.txIsolationLevelReadCommitted = true
# Change this to match your DB vendor
org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX
org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.StdJDBCDelegate
#============================================================================
# Configure Main Scheduler Properties
# Needed to manage cluster instances
#============================================================================
org.quartz.scheduler.instanceId=AUTO
org.quartz.scheduler.instanceName=MY_CLUSTERED_JOB_SCHEDULER
org.quartz.scheduler.rmi.export = false
org.quartz.scheduler.rmi.proxy = false
#============================================================================
# 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
2、jdbc.properties
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://x.x.x.x:3306/matchdb?zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useOldAliasMetadataBehavior=true
jdbc.username=root
jdbc.password=latch
jdbc.maxConnectionCount=150
jdbc.minConnectionCount=5
jdbc.acquire.increment=5
3、spring-quartz-cluster.xml
<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"
default-autowire="byName" default-lazy-init="true">
<context:component-scan base-package="com.latech.matchData"/>
<!-- 分布式事务配置 start -->
<!-- 配置任务并发执行线程池 -->
<bean id="executor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
<property name="corePoolSize" value="3" />
<property name="maxPoolSize" value="5" />
<property name="queueCapacity" value="10" />
</bean>
<bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 配置调度任务-->
<bean name="quartzScheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="configLocation" value="classpath:quartz.properties"/>
<property name="dataSource" ref="dataSource"/>
<property name="applicationContextSchedulerContextKey" value="applicationContextKey" />
<!-- <property name="transactionManager" ref="transactionManager"/> -->
<!-- 任务唯一的名称,将会持久化到数据库-->
<property name="schedulerName" value="getMatchDataJobsScheduler"/>
<!-- 每台集群机器部署应用的时候会更新触发器-->
<property name="overwriteExistingJobs" value="true"/>
<property name="jobFactory">
<bean class="com.latech.matchData.impl.AutowiringSpringBeanJobFactory"/>
</property>
<property name="triggers">
<list>
<ref bean="getMatchDataJobsScheduler"/>
<ref bean="getMatchResultJobsScheduler"/>
</list>
</property>
</bean>
<!-- 配置Job详情 -->
<bean name="getMatchDataJobs" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
<property name="jobClass" value="com.latech.matchData.impl.GetMatchDataJobs"/>
<property name="durability" value="true"/>
<property name="requestsRecovery" value="true"/>
</bean>
<!-- 配置触发时间 -->
<bean name="getMatchDataJobsScheduler" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean">
<property name="jobDetail" ref="getMatchDataJobs"/>
<property name="startDelay" value="0" />
<property name="repeatInterval" value="10000" />
</bean>
</beans>
4、spring-service-context.xml
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
<!-- 采用注释的方式配置bean -->
<context:annotation-config />
<!-- 配置要扫描的包 -->
<context:component-scan base-package="com.latech" />
<!--定义切面 自动代理 -->
<aop:aspectj-autoproxy />
<!-- 读入配置属性文件 -->
<context:property-placeholder location="classpath*:jdbc.properties"
ignore-unresolvable="true" />
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<!--maxActive: 最大连接数量 -->
<property name="maxActive" value="${jdbc.maxConnectionCount}" />
<!--minIdle: 最小空闲连接 -->
<property name="minIdle" value="${jdbc.minConnectionCount}" />
<!--maxIdle: 最大空闲连接 -->
<property name="maxIdle" value="20" />
<!--initialSize: 初始化连接 -->
<property name="initialSize" value="30" />
<!-- 连接被泄露时是否打印 -->
<property name="logAbandoned" value="true" />
<!--removeAbandoned: 是否自动回收超时连接 -->
<property name="removeAbandoned" value="true" />
<!--removeAbandonedTimeout: 超时时间(以秒数为单位) -->
<property name="removeAbandonedTimeout" value="10" />
<!--maxWait: 超时等待时间以毫秒为单位 1000等于60秒 -->
<property name="maxWait" value="1000" />
<!-- 在空闲连接回收器线程运行期间休眠的时间值,以毫秒为单位. -->
<property name="timeBetweenEvictionRunsMillis" value="10000" />
<!-- 在每次空闲连接回收器线程(如果有)运行时检查的连接数量 -->
<property name="numTestsPerEvictionRun" value="10" />
<!-- 1000 * 60 * 30 连接在池中保持空闲而不被空闲连接回收器线程 -->
<property name="minEvictableIdleTimeMillis" value="10000" />
<property name="validationQuery" value="SELECT NOW() FROM DUAL" />
</bean>
<!-- 统一异常处理配置 -->
<bean id="exceptionHandler" class="com.latech.ExceptionAdvisor"></bean>
<!-- AOP throwing管理 -->
<bean id="exceptionHandlerPointCut"
class="org.springframework.aop.aspectj.AspectJExpressionPointcut">
<property name="expression"
value="execution(* com.latech.*.impl.*.*(..)) or execution(* latech.*.impl.*.*(..))"></property>
</bean>
<bean id="exceptionHandlerAdvisor" class="org.springframework.aop.support.DefaultPointcutAdvisor">
<property name="pointcut" ref="exceptionHandlerPointCut"></property>
<property name="advice" ref="exceptionHandler"></property>
</bean>
<import resource="classpath*:/spring-service.xml" />
<!--导入分布式配置-->
<import resource="classpath*:/spring-quartz-cluster.xml" />
<!-- dubbo服务 -->
<import resource="classpath*:/dubbo-*-provider.xml" />
<import resource="classpath*:/spring-mybatis.xml" />
</beans>
【step3:创建job】
1、AutowiringSpringBeanJobFactory类是为了可以在scheduler中使用spring注解,如果不使用注解,可以不适用该类,而直接使用
SpringBeanJobFactory
package com.latech.matchData.impl;
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;
/**
* 使job类支持spring的自动注入
* @author Administrator
*
*/
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;
}
}
2、创建具体job
package com.latech.matchData.impl;
import org.quartz.DisallowConcurrentExecution;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.PersistJobDataAfterExecution;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;
import com.latech.matchData.api.IMatchDataService;
@PersistJobDataAfterExecution
@DisallowConcurrentExecution// 不允许并发执行
public class GetMatchDataJobs extends QuartzJobBean{
//这里就是因为有上文中的AutowiringSpringBeanJobFactory才可以使用@Autowired注解,否则只能在配置文件中设置这属性的值
@Autowired
private IMatchDataService matchDataService;
@Override
protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
matchDataService.addMatchInfoToDB();
}
}
网友评论