• Quartz集群配置


    先看看quartz的持久化基本介绍:

    引用
    1 大家都清楚quartz最基本的概念就是job,在job内调用具体service完成具体功能,quartz需要把每个job存储起来,方便调度,quartz存储job方式就分三种,我们最常用的也是quartz默认的是RAMJobStore,RAMJobStore顾名思义就是把job的相关信息存储在内存里,如果用spring配置quartz的job信息的话,所有信息是配置在xml里,当spirng context启动的时候就把xml里的job信息装入内存。这一性质就决定了一旦JVM挂掉或者容器挂掉,内存中的job信息就随之消失,无法持久化。另外两种方式是JobStoreTX和JobStoreCMT,暂时不讨论这两者的区别,使用这两种JobStore,quartz就会通过jdbc直连或者应用服务器jndi连接数据库,读取配置在数据库里的job初始化信息,并且把job通过java序列化到数据库里,这样就使得每个job信息得到了持久化,即使在jvm或者容器挂掉的情况下,也能通过数据库感知到其他job的状态和信息。      2 quartz集群各节点之间是通过同一个数据库实例(准确的说是同一个数据库实例的同一套表)来感知彼此的。 

    由上可见,我们需要创建quartz要用的数据库表,此sql文件在:quartz-1.8.6docsdbTables。此文件夹下有各个数据库的sql文件,mysql选择tables_mysql.sql。创建相应表。
    接下来新建quartz.properties来覆盖jar包中的此文件,新的properties文件放在src的根目录下即可。下面是文件内容:

    Java代码 复制代码 收藏代码
    1. #==============================================================    
    2. #Configure Main Scheduler Properties    
    3. #==============================================================     
    4. org.quartz.scheduler.instanceName = quartzScheduler  
    5. org.quartz.scheduler.instanceId = AUTO  
    6.   
    7. #==============================================================    
    8. #Configure JobStore    
    9. #==============================================================   
    10. org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX  
    11. org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.StdJDBCDelegate  
    12. org.quartz.jobStore.tablePrefix = QRTZ_  
    13. org.quartz.jobStore.isClustered = true  
    14. org.quartz.jobStore.clusterCheckinInterval = 20000    
    15. org.quartz.jobStore.dataSource = myDS  
    16.    
    17. #==============================================================    
    18. #Configure DataSource    
    19. #==============================================================   
    20. org.quartz.dataSource.myDS.driver = com.mysql.jdbc.Driver  
    21. org.quartz.dataSource.myDS.URL = jdbc:mysql://192.168.20.195:3306/database?useUnicode=true&characterEncoding=UTF-8  
    22. org.quartz.dataSource.myDS.user = root  
    23. org.quartz.dataSource.myDS.password = 123456  
    24. org.quartz.dataSource.myDS.maxConnections = 30  
    25.   
    26. #==============================================================    
    27. #Configure ThreadPool    
    28. #==============================================================   
    29. org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool  
    30. org.quartz.threadPool.threadCount = 10  
    31. org.quartz.threadPool.threadPriority = 5  
    32. org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread = true  
    #==============================================================  
    #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  
    org.quartz.jobStore.dataSource = myDS
     
    #==============================================================  
    #Configure DataSource  
    #============================================================== 
    org.quartz.dataSource.myDS.driver = com.mysql.jdbc.Driver
    org.quartz.dataSource.myDS.URL = jdbc:mysql://192.168.20.195:3306/database?useUnicode=true&characterEncoding=UTF-8
    org.quartz.dataSource.myDS.user = root
    org.quartz.dataSource.myDS.password = 123456
    org.quartz.dataSource.myDS.maxConnections = 30
    
    #==============================================================  
    #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
    

    可以看到除了数据源、线程池等配置外,我们指定了一个scheduler实例,实例ID为自动分配。

    Java代码 复制代码 收藏代码
    1. #==============================================================    
    2. #Configure Main Scheduler Properties    
    3. #==============================================================     
    4. org.quartz.scheduler.instanceName = quartzScheduler  
    5. org.quartz.scheduler.instanceId = AUTO  
    #==============================================================  
    #Configure Main Scheduler Properties  
    #==============================================================   
    org.quartz.scheduler.instanceName = quartzScheduler
    org.quartz.scheduler.instanceId = AUTO
    

    此外,指定了集群相应配置,检查间隔为20s:

    Java代码 复制代码 收藏代码
    1. org.quartz.jobStore.isClustered = true  
    2. org.quartz.jobStore.clusterCheckinInterval = 20000    
    org.quartz.jobStore.isClustered = true
    org.quartz.jobStore.clusterCheckinInterval = 20000  
    

    最后配置applicant-context.xml文件。这里特别要注意一点:

    引用
    MethodInvokingJobDetailFactoryBean 类中的 methodInvoking 方法,是不支持序列化的,因此在把 QUARTZ 的 TASK 序列化进入数据库时就会抛错。

    所以我们要自己实现MethodInvokingJobDetailFactoryBean 的功能,这里用MyDetailQuartzJobBean 替换。

    Java代码 复制代码 收藏代码
    1. import java.lang.reflect.Method;  
    2.   
    3. import org.apache.commons.logging.Log;  
    4. import org.apache.commons.logging.LogFactory;  
    5. import org.quartz.JobExecutionContext;  
    6. import org.quartz.JobExecutionException;  
    7. import org.springframework.context.ApplicationContext;  
    8. import org.springframework.scheduling.quartz.QuartzJobBean;  
    9.   
    10. public class MyDetailQuartzJobBean extends QuartzJobBean {  
    11.     protected final Log logger = LogFactory.getLog(getClass());  
    12.     private String targetObject;  
    13.     private String targetMethod;  
    14.     private ApplicationContext ctx;  
    15.   
    16.     @Override  
    17.     protected void executeInternal(JobExecutionContext context)  
    18.             throws JobExecutionException {  
    19.         try {  
    20.             logger.info("execute [" + targetObject + "] at once>>>>>>");  
    21.             Object otargetObject = ctx.getBean(targetObject);  
    22.             Method m = null;  
    23.   
    24.             try {  
    25.                 m = otargetObject.getClass().getMethod(targetMethod, new Class[] {JobExecutionContext.class});  
    26.                 m.invoke(otargetObject, new Object[] {context});  
    27.             } catch (SecurityException e) {  
    28.                 logger.error(e);  
    29.             } catch (NoSuchMethodException e) {  
    30.                 logger.error(e);  
    31.             }  
    32.         } catch (Exception e) {  
    33.             throw new JobExecutionException(e);  
    34.         }  
    35.     }  
    36.   
    37.     public void setApplicationContext(ApplicationContext applicationContext) {  
    38.         this.ctx = applicationContext;  
    39.     }  
    40.   
    41.     public void setTargetObject(String targetObject) {  
    42.         this.targetObject = targetObject;  
    43.     }  
    44.   
    45.     public void setTargetMethod(String targetMethod) {  
    46.         this.targetMethod = targetMethod;  
    47.     }  
    import java.lang.reflect.Method;
    
    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    import org.quartz.JobExecutionContext;
    import org.quartz.JobExecutionException;
    import org.springframework.context.ApplicationContext;
    import org.springframework.scheduling.quartz.QuartzJobBean;
    
    public class MyDetailQuartzJobBean extends QuartzJobBean {
    	protected final Log logger = LogFactory.getLog(getClass());
    	private String targetObject;
    	private String targetMethod;
    	private ApplicationContext ctx;
    
    	@Override
    	protected void executeInternal(JobExecutionContext context)
    			throws JobExecutionException {
    		try {
    			logger.info("execute [" + targetObject + "] at once>>>>>>");
    			Object otargetObject = ctx.getBean(targetObject);
    			Method m = null;
    
    			try {
    				m = otargetObject.getClass().getMethod(targetMethod, new Class[] {JobExecutionContext.class});
    				m.invoke(otargetObject, new Object[] {context});
    			} catch (SecurityException e) {
    				logger.error(e);
    			} catch (NoSuchMethodException e) {
    				logger.error(e);
    			}
    		} catch (Exception e) {
    			throw new JobExecutionException(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;
    	}
    
    

    终于到配置spring文件这步了

    Java代码 复制代码 收藏代码
    1. <bean id="mapScheduler" lazy-init="false" autowire="no"  
    2.         class="org.springframework.scheduling.quartz.SchedulerFactoryBean">  
    3.         <property name="triggers">  
    4.             <list>  
    5.                 <ref bean="dailyTrigger" />  
    6.                 <ref bean="billCountTrigger" />  
    7.                 <ref bean="userAcctTrigger" />  
    8.             </list>  
    9.         </property>  
    10.         <property name="applicationContextSchedulerContextKey" value="applicationContext" />  
    11.         <property name="configLocation" value="classpath:quartz.properties" />  
    12.     </bean>  
    13.   
    14.   
    15.     <bean id="dailyBillJob" class="com.***.job.DailyBillJob" />  
    16.   
    17.     <bean id="dailyBillJobDetail" class="org.springframework.scheduling.quartz.JobDetailBean">  
    18.         <property name="jobClass">  
    19.             <value>com.autelan.auteview.lib.util.MyDetailQuartzJobBean  
    20.             </value>  
    21.         </property>  
    22.         <property name="jobDataAsMap">  
    23.             <map>  
    24.                 <entry key="targetObject" value="dailyBillJob" />  
    25.                 <entry key="targetMethod" value="execute" />  
    26.             </map>  
    27.         </property>  
    28.     </bean>  
    29.   
    30.     <bean id="dailyTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">  
    31.         <property name="jobDetail">  
    32.             <ref bean="dailyBillJobDetail" />  
    33.         </property>  
    34.         <property name="cronExpression">  
    35.             <value>11 11 11 * * ?</value>  
    36.         </property>  
    37.     </bean>  
    38. // 转载请注明出处http://forhope.iteye.com/blog/1398990  
    <bean id="mapScheduler" lazy-init="false" autowire="no"
    		class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
    		<property name="triggers">
    			<list>
    				<ref bean="dailyTrigger" />
    				<ref bean="billCountTrigger" />
    				<ref bean="userAcctTrigger" />
    			</list>
    		</property>
    		<property name="applicationContextSchedulerContextKey" value="applicationContext" />
    		<property name="configLocation" value="classpath:quartz.properties" />
    	</bean>
    
    
    	<bean id="dailyBillJob" class="com.***.job.DailyBillJob" />
    
    	<bean id="dailyBillJobDetail" class="org.springframework.scheduling.quartz.JobDetailBean">
    		<property name="jobClass">
    			<value>com.autelan.auteview.lib.util.MyDetailQuartzJobBean
    			</value>
    		</property>
    		<property name="jobDataAsMap">
    			<map>
    				<entry key="targetObject" value="dailyBillJob" />
    				<entry key="targetMethod" value="execute" />
    			</map>
    		</property>
    	</bean>
    
    	<bean id="dailyTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
    		<property name="jobDetail">
    			<ref bean="dailyBillJobDetail" />
    		</property>
    		<property name="cronExpression">
    			<value>11 11 11 * * ?</value>
    		</property>
    	</bean>
    
    
  • 相关阅读:
    ES elasticsearch 段合并
    xtrabackup VS clone plugin
    MySQL排序揭秘
    PostgreSQL体系结构和基本操作
    ES elasticsearch refresh和flush的区别
    MySQLMGR(一)
    redis一键创建脚本
    vs2010 安装无法打开deffactory.dat解决办法
    FeatureLayer,FeatureDataset,FeatureClass,Feature几个概念总结
    http://www.cnblogs.com/secenes/archive/2008/05/15/1198933.html
  • 原文地址:https://www.cnblogs.com/justuntil/p/5533012.html
Copyright © 2020-2023  润新知