spring|spring quartz2.2.1 cluster

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实现代码 1.新建quartz相关表 链接:http://download.csdn.net/detail/fmynjy/9705319 2.quartz.properties

#============================================================== #Configure Main Scheduler Properties #============================================================== org.quartz.scheduler.instanceName = DefaultQuartzScheduler org.quartz.scheduler.instanceId = AUTO#============================================================== #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.quartz.impl.jdbcjobstore.JobStoreCMT org.quartz.jobStore.tablePrefix = QRTZ_ org.quartz.jobStore.maxMisfiresToHandleAtATime=10 org.quartz.jobStore.isClustered = true org.quartz.jobStore.clusterCheckinInterval = 20000


3.spring-scheduler.xml:

使用Spring的 Scheduled的定时任务配置 com.test.util.schedule.MyDetailQuartzJobBean 【spring|spring quartz2.2.1 cluster】 */30 * * * * ?





4.MyDetailQuartzJobBean.java

package com.test.util.schedule; 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; /** * Spring调度任务 * @author admin * */ public class MyDetailQuartzJobBean extends QuartzJobBean { /** * 日志打印 */ private static final Log log = LogFactory.getLog(MyDetailQuartzJobBean.class); private String targetObject; private String targetMethod; private ApplicationContext applicationContext; @Override protected void executeInternal(JobExecutionContext context) throws JobExecutionException { try { Object otargetObject = applicationContext.getBean(targetObject); Method m = null; try { m = otargetObject.getClass().getMethod(targetMethod, new Class[] {JobExecutionContext.class}); m.invoke(otargetObject, new Object[] {context}); } catch (SecurityException e) { log.info(e.getMessage()); } catch (NoSuchMethodException e) { log.info(e.getMessage()); } } catch (Exception e) { throw new JobExecutionException(e); } } public void setApplicationContext(ApplicationContext applicationContext) { this.applicationContext = applicationContext; } public void setTargetObject(String targetObject) { this.targetObject = targetObject; }public void setTargetMethod(String targetMethod) { this.targetMethod = targetMethod; } }


5. SpringCronJob.java

package com.test.util.schedule; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.quartz.JobExecutionContext; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import com.test.service.PMenuService; @Component public class SpringCronJob{ private static final Log log = LogFactory.getLog(SpringCronJob.class); @Autowired private PMenuService pmService; /** * 定时打印 */ public void pmenuCount(JobExecutionContext context){ log.info("执行结果为:" + pmService.count()); } }




注:
本文实现基于spirng4.0以上版本,quartz-2.2.1版本,低版本的quartz需要的spring版本也不同,否则会出现各种错误






    推荐阅读