quartz集成spring下的集群配置

来源:互联网 发布:java绝对路径 编辑:程序博客网 时间:2024/04/30 14:52

写在前头

    spring3.0以后就开始支持quartz2.x,因为org.quartz.CronTrigger在2.0从class变成了一个interface,Spring4.0.6配置文件中使用CronTriggerFactoryBean来集成quartz2.x,使用CronTriggerBean来集成quartz1.8.x及以前版本.

准备环境

我用的是spring4.0.6 + quartz 2.1.7
1. quartz官网:http://www.quartz-scheduler.org/
2. spring集成环境

<dependency>    <groupId>org.quartz-scheduler</groupId>    <artifactId>quartz</artifactId>    <version>2.1.7</version></dependency><dependency>    <groupId>org.quartz-scheduler</groupId>    <artifactId>quartz-oracle</artifactId>    <version>2.1.7</version></dependency><dependency>    <groupId>org.quartz-scheduler</groupId>    <artifactId>quartz-weblogic</artifactId>    <version>2.1.7</version></dependency><dependency>    <groupId>org.quartz-scheduler</groupId>    <artifactId>quartz-jboss</artifactId>    <version>2.1.7</version></dependency>

搭建环境

创建数据库表结构

文件路径:quartz-2.1.7\docs\dbTables\*.sql
选择对应数据库的脚本,然后执行。我选择的ORACLE.


创建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 

创建spring配置文件applicationContext-job.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:mvc="http://www.springframework.org/schema/mvc"xmlns:p="http://www.springframework.org/schema/p"xmlns:util="http://www.springframework.org/schema/util"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            http://www.springframework.org/schema/mvc             http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd         http://www.springframework.org/schema/util         http://www.springframework.org/schema/util/spring-util-3.2.xsd">            <bean id="jobService" class="com.test.service.TestJobservice"/>      <!-- 定义任务 -->    <bean id="vcaEventJobTask" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">        <property name="jobClass">            <!-- 上面的任务代理类 -->            <value>com.test.service.DetailQuartzJobBean</value>        </property>        <property name="jobDataAsMap">            <map>                <!-- 实际的任务的Bean name,填上EventMonitorService的Bean name -->                <entry key="targetObject" value="jobService" />                <!-- 执行Bean中的哪个方法 -->                <entry key="targetMethod" value="executeTask" />            </map>        </property>        <property name="durability" value="true"></property>     </bean>     <!-- 任务触发器 -->    <bean id="eventTaskTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">        <property name="jobDetail">            <!-- 任务代理Bean name -->            <ref bean="vcaEventJobTask" />        </property>        <property name="cronExpression">            <!-- 配置表达式,这里表示每五分钟执行一次 -->            <value>0 0/1 * * * ?</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 bean="eventTaskTrigger"/>              </list>        </property>        <property name="configLocation" value="classpath:quartz.properties" />        <property name="applicationContextSchedulerContextKey" value="applicationContext" />        <property name="startupDelay" value="30" />        <property name="autoStartup" value="true" />        <property name="overwriteExistingJobs" value="true" />    </bean></beans>

创建任务类

代理类DetailQuartzJobBean.java
package com.test.service;import java.lang.reflect.Method;import org.quartz.JobExecutionContext;import org.quartz.JobExecutionException;import org.springframework.context.ApplicationContext;import org.springframework.scheduling.quartz.QuartzJobBean;public class DetailQuartzJobBean extends QuartzJobBean { private ApplicationContext applicationContext;    private String targetObject; private String targetMethod;      public String getTargetObject() {return targetObject;}public void setTargetObject(String targetObject) {this.targetObject = targetObject;}public String getTargetMethod() {return targetMethod;}public void setTargetMethod(String targetMethod) {this.targetMethod = targetMethod;}public ApplicationContext getApplicationContext() {return applicationContext;}       /**    * 从SchedulerFactoryBean注入的applicationContext.    */       public void setApplicationContext(ApplicationContext applicationContext) {          this.applicationContext = applicationContext;      }        @Override  protected void executeInternal(JobExecutionContext ctx)throws JobExecutionException {try {// logger.info("execute [" + targetObject + "] at once>>>>>>");Object otargetObject = applicationContext.getBean(targetObject);Method m = null;try {m = otargetObject.getClass().getMethod(targetMethod,new Class[] {});m.invoke(otargetObject, new Object[] {});} catch (SecurityException e) {// logger.error(e);} catch (NoSuchMethodException e) {// logger.error(e);}} catch (Exception e) {throw new JobExecutionException(e);}}}


目标类TestJobservice.java
package com.test.service;import java.text.SimpleDateFormat;import java.util.Date;import org.slf4j.Logger;import org.slf4j.LoggerFactory;public class TestJobservice {private static final transient Logger logger = LoggerFactory.getLogger(TestJobservice.class);  public void executeTask() {logger.info("executing at " + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));}}


配置以上操作,即可完成集群下quartz的功能。
0 0