Spring3.2.0和Quartz1.8.6集群配置

jopen 12年前

最近项目中要添加一个后台任务处理的功能,要求做到集群,然后就调研了很多,网上也有好多资料,但在项目中应用时还是有很多问题,最终也找到了解决办法,以下是完整实现。

首先是任务实现类,一个普通的service或bean:

public class EventMonitorService implements Serializable {   private static final long serialVersionUID = 7026615035981953235L;      private static final Logger LOG = LoggerFactory     .getLogger(EventMonitorService.class);          @Autowired   private EventDao eventDao;          @Autowired   private SessionFactory sessionFactory;     /**    * 定时监控方法    */   public void startMonitorEvent() {    LOG.debug("开始执行定时任务--监控Event超时");    //自己的业务处理   }  }
第二步,创建任务代理类DetailQuartzJobBean
import java.lang.reflect.Method;    import org.quartz.JobExecutionContext;  import org.quartz.JobExecutionException;  import org.slf4j.Logger;  import org.slf4j.LoggerFactory;  import org.springframework.context.ApplicationContext;  import org.springframework.scheduling.quartz.QuartzJobBean;    import com.telematics.tsp.util.SpringContextKit;    public class DetailQuartzJobBean extends QuartzJobBean {   private static final Logger logger = LoggerFactory     .getLogger(DetailQuartzJobBean.class);   private String targetObject;   private String targetMethod;   private final ApplicationContext applicationContext;     public DetailQuartzJobBean() {    this.applicationContext = SpringContextKit.ME.getApplicationContext();   }     @Override   protected void executeInternal(JobExecutionContext context)     throws JobExecutionException {    try {     Object otargetObject = applicationContext.getBean(targetObject);     System.out.println(otargetObject.toString());     Method m = null;     try {      m = otargetObject.getClass().getMethod(targetMethod,        new Class[] {});      m.invoke(otargetObject, new Object[] {});     } catch (SecurityException e) {       } catch (NoSuchMethodException e) {       }    } catch (Exception e) {     throw new JobExecutionException(e);    }   }     public void setTargetObject(String targetObject) {    this.targetObject = targetObject;   }     public void setTargetMethod(String targetMethod) {    this.targetMethod = targetMethod;   }  }

这里在获取applicationContext时视项目框架而定,就是获得应用上下文。

第三步,配置quartz.properties

#============================================================================
# Configure Main Scheduler Properties  
#============================================================================
org.quartz.scheduler.instanceName = EventScheduler   
org.quartz.scheduler.instanceId = AUTO  
 
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.JobStoreTX
# mysql
#org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.StdJDBCDelegate
# Oracle
org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.oracle.OracleDelegate
org.quartz.jobStore.tablePrefix = QRTZ_
org.quartz.jobStore.maxMisfiresToHandleAtATime=10
org.quartz.jobStore.isClustered = true  
org.quartz.jobStore.clusterCheckinInterval = 20000 

第四步,配置quartz.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"   xsi:schemaLocation="http://www.springframework.org/schema/beans      http://www.springframework.org/schema/beans/spring-beans.xsd      http://www.springframework.org/schema/context      http://www.springframework.org/schema/context/spring-context.xsd">   <!-- 要调用的工作类 -->   <bean id="eventMonitorService" class="demo.EventMonitorService"></bean>       <!-- 定义任务 -->   <bean id="vcaEventJobTask" class="org.springframework.scheduling.quartz.JobDetailBean">    <property name="jobClass">     <!-- 上面的任务代理类 -->     <value>com.tservice.vca.jobtask.DetailQuartzJobBean</value>    </property>    <property name="jobDataAsMap">     <map>      <!-- 实际的任务的Bean name,填上EventMonitorService的Bean name -->      <entry key="targetObject" value="eventMonitorService" />      <!-- 执行Bean中的哪个方法 -->      <entry key="targetMethod" value="startMonitorEvent" />     </map>    </property>   </bean>     <!-- 任务触发器 -->   <bean id="eventTaskTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">    <property name="jobDetail">     <!-- 任务代理Bean name -->     <ref bean="vcaEventJobTask" />    </property>    <property name="cronExpression">     <!-- 配置表达式,这里表示每五分钟执行一次 -->     <value>0 0/5 * * * ?</value>    </property>   </bean>   <!-- 任务调度入口 -->   <bean autowire="no"    class="org.springframework.scheduling.quartz.SchedulerFactoryBean">    <property name="dataSource">     <ref bean="dataSource" />    </property>    <!-- 任务列表,可以配置多个任务加入到该List -->    <property name="triggers">     <list>      <ref local="eventTaskTrigger" />     </list>    </property>    <property name="configLocation" value="classpath:quartz.properties" />   </bean>  </beans>
接下来就可以正常启动你的项目了。