一.spring集成quartz组件

一.准备

1.spring3.x相关包

2.quartz1.8.5

具体需要的就是一些常用jar包,如果不知道,可以启动时报错的提示加入相关包即可。

二.建好一个相应的工程

具体省略

三.将org.springframework.context.support-3.0.0.RELEASE.jar和quartz-1.8.5.jar两个必须jar包放进lib中,其他例如日志包log4j,slf4j等根据需要添加

四.将quartz.properties文件放到src类路径下,具体文件可从quartz.jar解压后得到,或者从官方提供的例子程序中找

文件中相关的主要配置(只配置这些即可正常运行)

 

org.quartz.scheduler.instanceName = DefaultQuartzScheduler
org.quartz.scheduler.rmi.export = false
org.quartz.scheduler.rmi.proxy = false
org.quartz.scheduler.wrapJobExecutionInUserTransaction = false

org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount = 10
org.quartz.threadPool.threadPriority = 5
org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread = true

org.quartz.jobStore.misfireThreshold = 60000

org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore

 

五.建调度程序的applicationContext-quratz.xml文件,并配置applicationContext.xml在启动时加载

1.applicationContext-quratz.xml文件的内容

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans default-lazy-init="false">
 <bean name="webserviceScheduler"
  class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
  <property name="triggers">
   <list>
    <ref bean="testTrigger"/>     

    <ref bean="testCornTrigger"/>    

</list>
  </property>
  <property name="configLocation" value="classpath:quartz.properties" />
        <property name="autoStartup" value="true" />
        <property name="schedulerName" value="webserviceScheduler" />
 </bean>
  <!-- 通过表达式定时触发器 -->
 <bean id="testCornTrigger"
  class="org.springframework.scheduling.quartz.CronTriggerBean">
  <property name="jobDetail">
   <bean class="frameworkx.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
    <property name="targetObject">
     <ref bean="testJob" /><!-- 引用要定时触发的目标类 -->
    </property>
    <property name="targetMethod">
     <value>testCorn</value><!-- 引用要定时触发的目标类中的方法名 -->
    </property>
    <property name="concurrent">
     <value>false</value><!-- 默认为true,一般设置成false,是否允许多个worker执行同一个job -->
    </property>
   </bean>
  </property>

  <property name="cronExpression">
   <value>0 51 12 4 7 ? 2013</value><!-- 定时时间:秒,分,时,月中那一天,月,周中那一天,年 -->
  </property>
 </bean>
  <!-- 简单的重复执行触发器 -->
 <bean id="testTrigger"
  class="org.springframework.scheduling.quartz.SimpleTriggerBean">
  <property name="jobDetail">
   <bean class="frameworkx.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
    <property name="targetObject">
     <ref bean="testJob" /><!-- 引用要定时触发的目标类 -->
    </property>
    <property name="targetMethod">
     <value>test</value><!-- 引用要定时触发的目标类中的方法名 -->
    </property>
    <property name="concurrent">
     <value>false</value><!-- 默认为true,一般设置成false,避免数据存储问题 -->
    </property>
   </bean>
  </property>
  <property name="repeatInterval">
   <!-- 多久重复执行一次,以毫秒为单位 -->
   <value>60000</value>
  </property>
 </bean>
 
</beans>

2.applicationContext.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:aop="http://www.springframework.org/schema/aop"
 xmlns:tx="http://www.springframework.org/schema/tx"
 xsi:schemaLocation="
      http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsd
      http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

 <!-- 注解配置切面,代理类 -->
 <aop:aspectj-autoproxy></aop:aspectj-autoproxy>

 <!-- 数据源 -->
 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
  <property name="driverClass" value="oracle.jdbc.driver.OracleDriver" />
  <property name="jdbcUrl" value="jdbc:oracle:thin:@192.168.111.231:1521:HWAJ" />
  <property name="user" value="webservice" />
  <property name="password" value="1" />
 </bean>

 <!-- 事务管理  -->
 <bean id="transactionManager"
  class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  <property name="dataSource" ref="dataSource"></property>
 </bean>

 <!-- 事务切面 -->
 <tx:advice id="txAdvice" transaction-manager="transactionManager">
  <tx:attributes>
   <tx:method name="insert*" propagation="REQUIRED" rollback-for="Exception"/>
   <tx:method name="del*" propagation="REQUIRED" rollback-for="Exception"/>
   <tx:method name="modify*" propagation="REQUIRED" rollback-for="Exception"/>
   <tx:method name="edit*" propagation="REQUIRED" rollback-for="Exception"/>   
   <tx:method name="update*" propagation="REQUIRED" rollback-for="Exception"/> 
   <tx:method name="save*" propagation="REQUIRED" rollback-for="Exception"/>  
   <tx:method name="*" read-only="true"/>
  </tx:attributes>
 </tx:advice>
 <aop:config proxy-target-class="true">
  <aop:pointcut id="allManageMethod"
   expression="execution(* hw.webservice.service.*.*(..))" />
  <aop:advisor advice-ref="txAdvice" pointcut-ref="allManageMethod" />
 </aop:config>
 

 <!--注入jdbcTemplate -->
 <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
  <property name="dataSource" ref="dataSource"></property>
 </bean>

  <!-- BaseDao -->
 <bean id="baseDao" class="hw.webservice.base.impl.BaseDaoImpl">
  <property name="jdbcTemplate" ref="jdbcTemplate"></property>
 </bean>
 
 <!-- BaseService -->
 <bean id="baseService" class="hw.webservice.base.impl.BaseServiceImpl">
  <property name="baseDao" ref="baseDao"></property>
 </bean>
 
 <bean class="hw.webservice.util.BeanUtil"></bean>

  <!-- 任务调度器配置文件 -->
 <import resource="classpath*:config/quartz/applicationContext-quratz.xml"/>

<!-- 任务调度器所需service配置文件 -->
 <import resource="classpath*:config/spring_config/applicationContext-service-quartz.xml"/>

<!-- 任务调度器所需job配置文件 -->
 <import resource="classpath*:config/spring_config/applicationContext-job-service.xml"/>

 </beans>

3.applicationContext-job-service.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:aop="http://www.springframework.org/schema/aop"
 xmlns:tx="http://www.springframework.org/schema/tx"
 xsi:schemaLocation="
      http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsd
      http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
     
 <bean id="testJob" class="hw.webservice.job.TestJob"/>
 <bean id="testDynamicJob" class="hw.webservice.job.TestDynamicJob"/>
</beans>

4.applicationContext-service-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:aop="http://www.springframework.org/schema/aop"
 xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
 xmlns:context="http://www.springframework.org/schema/context"
 xsi:schemaLocation="
   http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
   http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
   http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd">
 
 <bean class="frameworkx.springframework.scheduling.quartz.BeanInvokingJobDetailFactoryBean"></bean>
 <bean class="frameworkx.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"></bean>
 
 <!-- 动态quartz -->
 <bean id="schedulerManageService" class="hw.webservice.quartz.SchedulerManageServiceImpl" />
</beans>

5.两个例子:TestJob和TestDynamicJob

TestJob.java 包含表达式触发器和simple触发器两种


 
package hw.webservice.job;

import hw.webservice.quartz.ISchedulerManageService;
import hw.webservice.util.BeanUtil;
import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;



public class TestJob implements Serializable{
 
   
  private static final long serialVersionUID = 1L;
  private static ISchedulerManageService schedulerManageService;

  static{
    schedulerManageService = (ISchedulerManageService)BeanUtil.getBean("schedulerManageService");
  }
  public void test(){
    System.out.println("测试触发器,1m执行一次");
//    addJob();
  }
  public void testCorn() throws Exception{
    System.out.println("表达式触发器");
    addJob();
    addSimpleJob();
  }
 
  public void addJob() throws Exception{
   
    Map map = new HashMap();
    map.put("key", "测试动态job");
    Map JobMap = new HashMap();
    JobMap.put("jobGroup", "testJobGroup");
    JobMap.put("triggeGroup", "testTriggeGroup");
    JobMap.putAll(map);
    Date date = null;
   
    try {
      date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2013-07-04 12:52:00");
    } catch (ParseException e) {
      e.printStackTrace();
    }
   
   
   
    //开始考试任务
    JobMap.put("job", "testJob");
    JobMap.put("trigger", "tetsTrigger");
    JobMap.put("cronExpression", schedulerManageService.getCronexpression_r(date));
    JobMap.put("data", "测试");
   
    schedulerManageService.addCronTrigger(JobMap, TestDynamicJob.class);
  }
 
  public void addSimpleJob() throws Exception{
   
    Map map = new HashMap();
    map.put("key", "测试动态simplejob");
    Map JobMap = new HashMap();
    JobMap.put("jobGroup", "testJobGroup");
    JobMap.put("triggeGroup", "testTriggeGroup");
    //开始考试任务
    JobMap.put("job", "testJob");
    JobMap.put("trigger", "tetsTrigger");
    long time = System.currentTimeMillis();
    Date date = new Date(time);
    JobMap.put("startTime", date);
    JobMap.put("endTime", new Date(time+60000));
    JobMap.put("repeatCount", 10);
    JobMap.put("repeatInterval", 5000);
   
    JobMap.put("data", "测试simplejob");
    JobMap.putAll(map);
   
   
    schedulerManageService.addSimpleTrigger(JobMap, TestDynamicJob.class);
  }
}

以上是spring集成quartz1.8.5的完整步骤,以上例子可以实现通过xml文件配置静态的simple触发器和表达式触发器。

弊端:通过xml配置的触发器,没有实现数据的持久哈,数据是保存在内存中的,当服务重启时添加的动态触发器就会丢失。

关于动态触发器的使用和触发器数据持久化,将在接下来的两篇博客中讲述。

 

你可能感兴趣的:(spring,quartz)