Quartz在Spring中如何动态配置时间

在项目中有一个需求,需要灵活配置调度任务时间,并能自由启动或停止调度。
有关调度的实现我就第一就想到了Quartz这个开源调度组件,因为很多项目使用过,Spring结合Quartz静态配置调度任务时间,非常easy。 比如:每天凌晨几点定时运行一个程序,这只要在工程中的spring配置文件中配置好spring整合quartz的几个属性就好。

Spring配置文件

引用


<bean id="jobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="simpleService" />
<property name="targetMethod" value="test" />
</bean>
<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="jobDetail" />
<property name="cronExpression" value="0 0/50 * ? * * *" />
</bean>
<bean  id="schedulerTrigger" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="cronTrigger"/>     
</list>
</property>
</bean>



这种配置就是对quartz的一种简单的使用了,调度任务会在spring启动的时候加载到内存中,按照cronTrigger中定义的 cronExpression定义的时间按时触发调度任务。但是这是quartz使用“内存”方式的一种配置,也比较常见,当然对于不使用spring的 项目,也可以单独整合quartz。方法也比较简单,可以从quartz的doc中找到配置方式,或者看一下《Quartz Job Scheduling Framework 》。

但是对于想持久化调度任务的状态,并且灵活调整调度时间的方式来说,上面的内存方式就不能满足要求了,正如本文开始我遇到的情况,需要采用数据库方式集成 Quartz,这部分集成其实在《Quartz Job Scheduling Framework 》中也有较为详细的介绍,当然doc文档中也有,但是缺乏和spring集成的实例。

一、需要构建Quartz数据库表,建表脚本在Quartz发行包的docs\dbTables目录,里面有各种数据库建表脚本,我采用的Quartz 1.6.5版本,总共12张表,不同版本,表个数可能不同。我用mysql数据库,执行了Quartz发行包的docs\dbTables\tables_mysql_innodb.sql建表。

二、建立java project,完成后目录如下 


project,完成后目录如下 
Quartz在Spring中如何动态配置时间 

 

三、配置数据库连接池
配置jdbc.properties文件

引用


jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/quartz?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true
jdbc.username=root
jdbc.password=kfs
cpool.checkoutTimeout=5000
cpool.minPoolSize=10
cpool.maxPoolSize=25
cpool.maxIdleTime=7200
cpool.acquireIncrement=5
cpool.autoCommitOnClose=true



配置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"
    xmlns:context="http://www.springframework.org/schema/context"
     xmlns:jee="http://www.springframework.org/schema/jee"
    xsi:schemaLocation="
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
     http://www.springframework.org/schema/jee
       http://www.springframework.org/schema/jee/spring-jee-2.5.xsd"  >
 
   <context:component-scan base-package="com.sundoctor"/>

<!-- 属性文件读入 -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:jdbc.properties</value>
</list>
</property>
</bean>

<!-- 数据源定义,使用c3p0 连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="${jdbc.driverClassName}" />
<property name="jdbcUrl" value="${jdbc.url}" />
<property name="user" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="initialPoolSize" value="${cpool.minPoolSize}"/>
<property name="minPoolSize" value="${cpool.minPoolSize}" />
<property name="maxPoolSize" value="${cpool.maxPoolSize}" />
<property name="acquireIncrement" value="${cpool.acquireIncrement}" />
    <property name="maxIdleTime" value="${cpool.maxIdleTime}"/>  
</bean>

</beans>


这里只是配置了数据连接池,我使用c3p0 连接池,还没有涉及到Quartx有关配置,下面且听我慢慢道来。

四、实现动态定时任务
  什么是动态定时任务:是由客户制定生成的,服务端只知道该去执行什么任务,但任务的定时是不确定的(是由客户制定)。
这样总不能修改配置文件每定制个定时任务就增加一个trigger吧,即便允许客户修改配置文件,但总需要重新启动web服务啊,研究了下Quartz在Spring中的动态定时,发现

引用


<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean" >
         <property name="jobDetail" ref="schedulerJobDetail"/>
         <property name="cronExpression">
             <value>0/10 * * * * ?</value>
         </property>


cronExpression 是关键,如果可以动态设置cronExpression 的 值,就可以顺利解决问题了。这样我们就不能直接使用 org.springframework.scheduling.quartz.CronTriggerBean,需要自己实现一个动态调度服务类,在其 中构建CronTrigger或SimpleTrigger,动态配置时间。
动态调度服务接口:

Java代码

  1. package  com.sundoctor.quartz.service;   

  2.   

  3. import  java.util.Date;   

  4.   

  5. import  org.quartz.CronExpression;   

  6.   

  7. public   interface  SchedulerService {   

  8.        

  9.      void  schedule(String cronExpression);   

  10.        

  11.        

  12.      void  schedule(String name,String cronExpression);   

  13.        

  14.        

  15.      void  schedule(CronExpression cronExpression);   

  16.        

  17.        

  18.      void  schedule(String name,CronExpression cronExpression);   

  19.        

  20.        

  21.      void  schedule(Date startTime);     

  22.        

  23.        

  24.      void  schedule(String name,Date startTime);   

  25.        

  26.        

  27.      void  schedule(Date startTime,Date endTime);    

  28.        

  29.        

  30.      void  schedule(String name,Date startTime,Date endTime);   

  31.        

  32.        

  33.      void  schedule(Date startTime,Date endTime, int  repeatCount);    

  34.        

  35.        

  36.      void  schedule(String name,Date startTime,Date endTime, int  repeatCount);   

  37.        

  38.        

  39.      void  schedule(Date startTime,Date endTime, int  repeatCount, long  repeatInterval) ;   

  40.        

  41.        

  42.      void  schedule(String name,Date startTime,Date endTime, int  repeatCount, long  repeatInterval);   

  43. }  



动态调度服务实现类:

Java代码

  1. package  com.sundoctor.quartz.service;   

  2.   

  3. import  java.text.ParseException;   

  4. import  java.util.Date;   

  5. import  java.util.UUID;   

  6.   

  7. import  org.quartz.CronExpression;   

  8. import  org.quartz.CronTrigger;   

  9. import  org.quartz.JobDetail;   

  10. import  org.quartz.Scheduler;   

  11. import  org.quartz.SchedulerException;   

  12. import  org.quartz.SimpleTrigger;   

  13. import  org.springframework.beans.factory.annotation.Autowired;   

  14. import  org.springframework.beans.factory.annotation.Qualifier;   

  15. import  org.springframework.stereotype.Service;   

  16.   

  17. @Service  ( "schedulerService" )   

  18. public   class  SchedulerServiceImpl  implements  SchedulerService {   

  19.   

  20.      private  Scheduler scheduler;   

  21.      private  JobDetail jobDetail;   

  22.   

  23.      @Autowired   

  24.      public   void  setJobDetail( @Qualifier ( "jobDetail" ) JobDetail jobDetail) {   

  25.          this .jobDetail = jobDetail;   

  26.     }   

  27.   

  28.      @Autowired   

  29.      public   void  setScheduler( @Qualifier ( "quartzScheduler" ) Scheduler scheduler) {   

  30.          this .scheduler = scheduler;   

  31.     }   

  32.   

  33.      @Override   

  34.      public   void  schedule(String cronExpression) {   

  35.         schedule( null , cronExpression);   

  36.     }   

  37.   

  38.      @Override   

  39.      public   void  schedule(String name, String cronExpression) {   

  40.          try  {   

  41.             schedule(name,  new  Cronexpression_r(cronExpression));   

  42.         }  catch  (ParseException e) {   

  43.              throw   new  RuntimeException(e);   

  44.         }   

  45.     }   

  46.   

  47.      @Override   

  48.      public   void  schedule(CronExpression cronExpression) {   

  49.         schedule( null , cronExpression);   

  50.     }   

  51.   

  52.      @Override   

  53.      public   void  schedule(String name, CronExpression cronExpression) {   

  54.          if  (name ==  null  || name.trim().equals( "" )) {   

  55.             name = UUID.randomUUID().toString();   

  56.         }   

  57.   

  58.          try  {   

  59.             scheduler.addJob(jobDetail,  true );   

  60.   

  61.             CronTrigger cronTrigger =  new CronTrigger(name, Scheduler.DEFAULT_GROUP, jobDetail.getName(),   

  62.                     Scheduler.DEFAULT_GROUP);   

  63.             cronTrigger.setCronexpression_r(cronExpression);   

  64.             scheduler.scheduleJob(cronTrigger);   

  65.             scheduler.rescheduleJob(name, Scheduler.DEFAULT_GROUP, cronTrigger);   

  66.         }  catch  (SchedulerException e) {   

  67.              throw   new  RuntimeException(e);   

  68.         }   

  69.     }   

  70.   

  71.      @Override   

  72.      public   void  schedule(Date startTime) {   

  73.         schedule(startTime,  null );   

  74.     }   

  75.   

  76.      @Override   

  77.      public   void  schedule(String name, Date startTime) {   

  78.         schedule(name, startTime,  null );   

  79.     }   

  80.   

  81.      @Override   

  82.      public   void  schedule(Date startTime, Date endTime) {   

  83.         schedule(startTime, endTime,  0 );   

  84.     }   

  85.   

  86.      @Override   

  87.      public   void  schedule(String name, Date startTime, Date endTime) {   

  88.         schedule(name, startTime, endTime,  0 );   

  89.     }   

  90.   

  91.      @Override   

  92.      public   void  schedule(Date startTime, Date endTime,  int  repeatCount) {   

  93.         schedule( null , startTime, endTime,  0 );   

  94.     }   

  95.   

  96.      @Override   

  97.      public   void  schedule(String name, Date startTime, Date endTime,  int  repeatCount) {   

  98.         schedule(name, startTime, endTime,  0 , 0L);   

  99.     }   

  100.   

  101.      @Override   

  102.      public   void  schedule(Date startTime, Date endTime,  int  repeatCount,  long  repeatInterval) {   

  103.         schedule( null , startTime, endTime, repeatCount, repeatInterval);   

  104.     }   

  105.   

  106.      @Override   

  107.      public   void  schedule(String name, Date startTime, Date endTime,  int  repeatCount,  long repeatInterval) {   

  108.          if  (name ==  null  || name.trim().equals( "" )) {   

  109.             name = UUID.randomUUID().toString();   

  110.         }   

  111.   

  112.          try  {   

  113.             scheduler.addJob(jobDetail,  true );   

  114.   

  115.             SimpleTrigger SimpleTrigger =  new SimpleTrigger(name, Scheduler.DEFAULT_GROUP, jobDetail.getName(),   

  116.                     Scheduler.DEFAULT_GROUP, startTime, endTime, repeatCount, repeatInterval);   

  117.             scheduler.scheduleJob(SimpleTrigger);   

  118.             scheduler.rescheduleJob(name, Scheduler.DEFAULT_GROUP, SimpleTrigger);   

  119.   

  120.         }  catch  (SchedulerException e) {   

  121.              throw   new  RuntimeException(e);   

  122.         }   

  123.     }   

  124. }  


SchedulerService 只有一个多态方法schedule,SchedulerServiceImpl实现SchedulerService接口,注入 org.quartz.Schedulert和org.quartz.JobDetail,schedule方法可以动态配置 org.quartz.CronExpression或org.quartz.SimpleTrigger调度时间。

五、实现自己的org.quartz.JobDetail
在上一步中SchedulerServiceImpl需要注入org.quartz.JobDetail,在以前的静态配置中

引用


<bean id="jobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="simpleService" />
<property name="targetMethod" value="testMethod" />
</bean>


中使用org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean。 在这里使用org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean。 会报

引用



Caused by: java.io.NotSerializableException: Unable to serialize JobDataMap for insertion into database because the value of property 'methodInvoker' is not serializable: org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean
at org.quartz.impl.jdbcjobstore.StdJDBCDelegate.serializeJobData(StdJDBCDelegate.java:3358)
at org.quartz.impl.jdbcjobstore.StdJDBCDelegate.insertJobDetail(StdJDBCDelegate.java:515)
at org.quartz.impl.jdbcjobstore.JobStoreSupport.storeJob(JobStoreSupport.java:1102)
... 11 more


异常,google了一下,没有找到解决方法。所以在这里不能使用org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean。 ,不能pojo了,需要使用org.springframework.scheduling.quartz.JobDetailBean和org.springframework.scheduling.quartz.QuartzJobBean实现自己的QuartzJobBean,如下:

Java代码

  1. package  com.sundoctor.example.service;   

  2.   

  3. import  org.quartz.JobExecutionContext;   

  4. import  org.quartz.JobExecutionException;   

  5. import  org.quartz.Trigger;   

  6. import  org.springframework.scheduling.quartz.QuartzJobBean;   

  7.   

  8. public   class  MyQuartzJobBean  extends  QuartzJobBean {   

  9.   

  10.      private  SimpleService simpleService;   

  11.        

  12.      public   void  setSimpleService(SimpleService simpleService) {   

  13.          this .simpleService = simpleService;   

  14.     }   

  15.   

  16.      @Override   

  17.      protected   void  executeInternal(JobExecutionContext jobexecutioncontext)  throws JobExecutionException {   

  18.         Trigger trigger = jobexecutioncontext.getTrigger();   

  19.         String triggerName = trigger.getName();        

  20.         simpleService.testMethod(triggerName);   

  21.     }   

  22.   

  23. }  



MyQuartzJobBean继承org.springframework.scheduling.quartz.QuartzJobBean ,注入的SimpleService如下:

Java代码

  1. package  com.sundoctor.example.service;   

  2.   

  3. import  java.io.Serializable;   

  4.   

  5. import  org.slf4j.Logger;   

  6. import  org.slf4j.LoggerFactory;   

  7. import  org.springframework.stereotype.Service;   

  8.   

  9. @Service ( "simpleService" )   

  10. public   class  SimpleService  implements  Serializable{   

  11.        

  12.      private   static   final   long  serialVersionUID = 122323233244334343L;   

  13.      private   static   final  Logger logger = LoggerFactory.getLogger(SimpleService. class );   

  14.        

  15.      public   void  testMethod(String triggerName){   

  16.          //这里执行定时调度业务   

  17.         logger.info(triggerName);   

  18.     }   

  19.        

  20.      public   void  testMethod2(){   

  21.         logger.info( "testMethod2" );   

  22.     }   

  23. }  


SimpleService主要执行定时调度业务,在这里我只是简单打印一下log日志。SimpleService需要实现java.io.Serializable接口,否则会报

引用

Caused by: java.io.InvalidClassException: com.sundoctor.example.service.SimpleService; class invalid for deserialization
at java.io.ObjectStreamClass.initNonProxy(ObjectStreamClass.java:587)
at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1583)
at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1496)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1732)
... 64 more


异常。

配置applicationContext-quartz.xml文件:

引用


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">

<beans>
<bean name="quartzScheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="dataSource"> 
<ref bean="dataSource" /> 
</property>
<property name="applicationContextSchedulerContextKey"  value="applicationContextKey" />
<property name="configLocation" value="classpath:quartz.properties"/>
</bean>

<bean id="jobDetail" class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass">
<value>com.sundoctor.example.service.MyQuartzJobBean</value>
</property>

<property name="jobDataAsMap">
<map>
<entry key="simpleService">
<ref bean="simpleService" />
</entry>
</map>
</property>

</bean>
</beans>



quartzScheduler中没有了

引用


<property name="triggers">
<list>
...     
</list>
/property>


配置,通过SchedulerService动态加入CronTrigger或SimpleTrigger。

在红色的

引用



<property name="jobDataAsMap">
<map>
<entry key="simpleService">
<ref bean="simpleService" />
</entry>
</map>
</property>


中需要注入调度业务类,否则会报空指指错误。

dataSource:项目中用到的数据源,里面包含了quartz用到的12张数据库表;
applicationContextSchedulerContextKey: 是org.springframework.scheduling.quartz.SchedulerFactoryBean这个类中把spring上下 文以key/value的方式存放在了quartz的上下文中了,可以用applicationContextSchedulerContextKey所定义的key得到对应的spring上下文;
configLocation:用于指明quartz的配置文件的位置,如果不用spring配置quartz的话,本身quartz是通过一个配置文件 进行配置的,默认名称是quartz.properties,里面配置的参数在quartz的doc文档中都有介绍,可以调整quartz,我在项目中也 用这个文件部分的配置了一些属性,代码如下:

引用


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

org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX
#org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.HSQLDBDelegate
org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.StdJDBCDelegate
#org.quartz.jobStore.useProperties = true
org.quartz.jobStore.tablePrefix = QRTZ_ 
org.quartz.jobStore.isClustered = false 
org.quartz.jobStore.maxMisfiresToHandleAtATime=1


这里面没有数据源相关的配置部分,采用spring注入datasource的方式已经进行了配置。

六、测试
运行如下测试类

Java代码

  1. package  com.sundoctor.example.test;   

  2.   

  3. import  java.text.ParseException;   

  4. import  java.text.SimpleDateFormat;   

  5. import  java.util.Date;   

  6.   

  7. import  org.springframework.context.ApplicationContext;   

  8. import  org.springframework.context.support.ClassPathXmlApplicationContext;   

  9.   

  10. import  com.sundoctor.quartz.service.SchedulerService;   

  11.   

  12. public   class  MainTest {   

  13.   

  14.        

  15.      public   static   void  main(String[] args) {   

  16.         ApplicationContext springContext =  new  ClassPathXmlApplicationContext( new  String[]{"classpath:applicationContext.xml" , "classpath:applicationContext-quartz.xml" });   

  17.         SchedulerService schedulerService = (SchedulerService)springContext.getBean( "schedulerService");   

  18.            

  19.          //执行业务逻辑...   

  20.            

  21.          //设置调度任务   

  22.          //每10秒中执行调试一次   

  23.         schedulerService.schedule( "0/10 * * ? * * *" );    

  24.            

  25.         Date startTime = parse( "2009-06-01 22:16:00" );   

  26.         Date endTime =  parse( "2009-06-01 22:20:00" );   

  27.            

  28.          //2009-06-01 21:50:00开始执行调度   

  29.         schedulerService.schedule(startTime);   

  30.   

  31.          //2009-06-01 21:50:00开始执行调度,2009-06-01 21:55:00结束执行调试   

  32.          //schedulerService.schedule(startTime,endTime);   

  33.            

  34.          //2009-06-01 21:50:00开始执行调度,执行5次结束   

  35.          //schedulerService.schedule(startTime,null,5);   

  36.   

  37.          //2009-06-01 21:50:00开始执行调度,每隔20秒执行一次,执行5次结束   

  38.          //schedulerService.schedule(startTime,null,5,20);   

  39.            

  40.          //等等,查看com.sundoctor.quartz.service.SchedulerService           

  41.     }   

  42.        

  43.      private   static  Date parse(String dateStr){   

  44.         SimpleDateFormat format =  new  SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );   

  45.          try  {   

  46.              return  format.parse(dateStr);   

  47.         }  catch  (ParseException e) {   

  48.              throw   new  RuntimeException(e);   

  49.         }   

  50.     }   

  51.   

  52. }  


输出

引用


[2009-06-02 00:08:50]INFO  com.sundoctor.example.service.SimpleService(line:17) -2059c26f-9462-49fe-b4ce-be7e7a29459f
[2009-06-02 00:10:20]INFO  com.sundoctor.example.service.SimpleService(line:17) -2059c26f-9462-49fe-b4ce-be7e7a29459f
[2009-06-02 00:10:30]INFO  com.sundoctor.example.service.SimpleService(line:17) -2059c26f-9462-49fe-b4ce-be7e7a29459f
[2009-06-02 00:10:40]INFO  com.sundoctor.example.service.SimpleService(line:17) -2059c26f-9462-49fe-b4ce-be7e7a29459f
[2009-06-02 00:10:50]INFO  com.sundoctor.example.service.SimpleService(line:17) -2059c26f-9462-49fe-b4ce-be7e7a29459f
[2009-06-02 00:11:00]INFO  com.sundoctor.example.service.SimpleService(line:17) -2059c26f-9462-49fe-b4ce-be7e7a29459f
[2009-06-02 00:11:10]INFO  com.sundoctor.example.service.SimpleService(line:17) -2059c26f-9462-49fe-b4ce-be7e7a29459f


这样只是简单的将quartz trigger名称打印出来。

这样通过SchedulerService就可以动态配置调度时间。其实SchedulerService 还可扩展,比如可以注入多个JobDetail,调度不同的JobDetail。 

首先实现多个JobDeatail并注册,比如:  

引用


<bean id="jobDetail1" class="org.springframework.scheduling.quartz.JobDetailBean">  
<property name="jobClass">  
<value>com.sundoctor.example.service.MyQuartzJobBean1</value>  
</property>  

<bean id="jobDetail2" class="org.springframework.scheduling.quartz.JobDetailBean">  
<property name="jobClass">  
<value>com.sundoctor.example.service.MyQuartzJobBean2</value>  
</property>  


<bean id="jobDetail3" class="org.springframework.scheduling.quartz.JobDetailBean">  
<property name="jobClass">  
<value>com.sundoctor.example.service.MyQuartzJobBean3</value>  
</property>  
...  


其次将多个JobDeatail放到一个HashMap中  

引用


<util:map id = "jobDeatailMap" map-class="java.util.HashMap" key-type="java.lang.String" value-type="org.springframework.scheduling.quartz.JobDetailBean">  
<entry key="jobDetail1" ref="jobDetail1"/>  
<entry key="jobDetail2" ref="jobDetail2"/>  
<entry key="jobDetail3" ref="jobDetail3"/>  
</util:map>



然后在SchedulerService 注入jobDeatailMap  

Java代码 

  1. @Service ( "schedulerService" )    

  2. public   class  SchedulerServiceImpl  implements  SchedulerService {    

  3.      

  4.      private  Scheduler scheduler;    

  5.      private  Map<String,JobDetailBean> jobDeatailMap;    

  6.      

  7.      @Autowired     

  8.      public   void  setJobDeatailMap( @Qualifier ( "jobDeatailMap") Map<String,JobDetailBean> jobDeatailMap) {    

  9.          this .jobDeatailMap = jobDeatailMap;    

  10.      }    

  11.      @Autowired     

  12.      public   void  setScheduler( @Qualifier ( "quartzScheduler" ) Scheduler scheduler) {    

  13.          this .scheduler = scheduler;    

  14.      }   

  15. ...  



最后,修改SchedulerServiceImpl中的schedule方法,增加以jobDeatailMap KEY名字为参数:  

Java代码 

  1.       @Override     

  2.      public   void  schedule(String jobDetailName,String name, CronExpression cronExpression) {    

  3.          if  (name ==  null  || name.trim().equals( "" )) {    

  4.              name = UUID.randomUUID().toString();    

  5.          }    

  6.      

  7.          //这个时候JobDetail根据jobDetailName从jobDeatailMap获取   

  8.          JobDetail jobDetail = jobDeatailMap.get(jobDetailName);  

  9.          try  {    

  10.              scheduler.addJob(jobDetail, true );    

  11.      

  12.              CronTrigger cronTrigger = new CronTrigger(name, Scheduler.DEFAULT_GROUP, jobDetail.getName(),    

  13.                      Scheduler.DEFAULT_GROUP);    

  14.              cronTrigger.setCronexpression_r(cronExpression);    

  15.              scheduler.scheduleJob(cronTrigger);    

  16.              scheduler.rescheduleJob(name, Scheduler.DEFAULT_GROUP, cronTrigger);    

  17.          } catch  (SchedulerException e) {    

  18.              throw   new  RuntimeException(e);    

  19.          }    

  20.      }  

  21. 其它多态方法一样修改,增加jobDetailName参数。      


调用时,传不同的jobDetailName参数就可以调用不用的JobDetail。  

Java代码 

  1. SchedulerService schedulerService = (SchedulerService)springContext.getBean( "schedulerService" );   

  2.   

  3. schedulerService.schedule("jobDetail1" , "审计任务" , "0/10 * * ? * * *" );     

  4.   

  5. schedulerService.schedule("jobDetail2" , "发放任务" , "0/10 * * ? * * *" );   

  6.   

  7. schedulerService.schedule("jobDetail3" , "AAA任务" , "0/10 * * ? * * *" );   

 

其实很多时候只需要一个JobDetail就可以了,也可以达到多个JobDetail一样的效果,一个JobDetail的时候可以在Trigger名称上做扩展,可以在调度任务时给Trigger名称加上不同的前缀或后缀,比如Trigger名称增加一个前缀参数,  

Java代码 

  1. @Override     

  2. public   void  schedule(String name, String prefix ,CronExpression cronExpression) {    

  3.     if  (name ==  null  || name.trim().equals( "" )) {    

  4.         name = UUID.randomUUID().toString();    

  5.     }    

  6.   

  7.     try  {    

  8.         scheduler.addJob(jobDetail, true );    

  9.           

  10.         //给Trigger名秒加上前缀   

  11.         name = prefix + name;  

  12.   

  13.         CronTrigger cronTrigger = new CronTrigger(name, Scheduler.DEFAULT_GROUP, jobDetail.getName(),    

  14.                 Scheduler.DEFAULT_GROUP);    

  15.         cronTrigger.setCronexpression_r(cronExpression);    

  16.         scheduler.scheduleJob(cronTrigger);    

  17.         scheduler.rescheduleJob(name, Scheduler.DEFAULT_GROUP, cronTrigger);    

  18.     } catch  (SchedulerException e) {    

  19.         throw   new  RuntimeException(e);    

  20.     }    

  21. }    



然后在QuartzJobBean中的executeInternal方法取到Trigger名秒,然后根据其前缀或后缀调用不同的业务逻辑 

Java代码 

  1. public   class  MyQuartzJobBean  extends  QuartzJobBean {    

  2.     

  3.     private  SimpleService simpleService;    

  4.         

  5.     public   void  setSimpleService(SimpleService simpleService) {    

  6.         this .simpleService = simpleService;    

  7.     }    

  8.     

  9.     @Override     

  10.     protected   void  executeInternal(JobExecutionContext jobexecutioncontext)  throws JobExecutionException {    

  11.         Trigger trigger = jobexecutioncontext.getTrigger();   

  12.         //取得Trigger名称,判断名称前缀或后缀调用不同的业务逻辑   

  13.         String triggerName = trigger.getName();    

  14.         if (tirggerName ...){  

  15.             simpleService.testMethod(triggerName);    

  16.         }else   if (tirggerName ...){  

  17.             simpleService.testMethod2(triggerName);   

  18.         }else {  

  19.             ...  

  20.         }  

  21.     }    

  22.     

  23. }  

在 simpleService里面注入一个继承HibernateDaoSupport的类,这个继承HibernateDaoSupport的类也必须实 现序列化接口,simpleService类被序列化保存到数据库表 qrtz_job_details的job_class_name字段中,quartz在运行时会读取qrtz_job_details表中的 job_class_name将其反序列化。这也是为什么simpleService和其中注入各属性需要实现Serializable序列化接口的原 因,所以你每次修改simpleService类或者其中的继承HibernateDaoSupport的类都要删除 qrtz_job_details表对应的job记录,否则可能会出现空指针异常,因为你如果你没有删除qrtz_job_details表中的记录,你 修改的东东并不会自动更新到qrtz_job_details中,你用的还是原来旧版本的simpleService类。 

在 simpleService里面注入一个继承HibernateDaoSupport的类,这个继承HibernateDaoSupport的类也必须实 现序列化接口,simpleService类被序列化保存到数据库表 qrtz_job_details的job_class_name字段中,quartz在运行时会读取qrtz_job_details表中的 job_class_name将其反序列化。这也是为什么simpleService和其中注入各属性需要实现Serializable序列化接口的原 因,所以你每次修改simpleService类或者其中的继承HibernateDaoSupport的类都要删除 qrtz_job_details表对应的job记录,否则可能会出现空指针异常,因为你如果你没有删除qrtz_job_details表中的记录,你 修改的东东并不会自动更新到qrtz_job_details中,你用的还是原来旧版本的simpleService类。  

你的这个问题在我另一篇文章《Quartz任务监控管理》有人提到过,你也可以到http://www.iteye.com/topic/441951?page=1看看。 

要 做的并不是简单的从数据库读取cronExpression,而是能够通过前端(比如Web页面)的修改并且不需要重启服务的情况下就可以动态修改配置任 务调度时间,并且对于quartx的数据持久化是透明的,只需在数据库增加12张表,修改一下quartx.properties文件的配置,其它并不需 要你做些什么额外的斯工作。  

JdbcPlaceholderConfigurer继承自PropertyPlaceholderConfigurer  
, 将原来配置在一个properties文件中的内容转移到数据库而己。JdbcPlaceholderConfigurer只是应用启动时简单的将 cronExpression从数据库读取出来,每次修改完数据库后就都需要重启服务,新的修改才会生效。其实 JdbcPlaceholderConfigurer还是一种静态配置,只是将原来写在  

引用

<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">  
<property name="jobDetail" ref="jobDetail" />  
<property name="cronExpression" value="0 0/50 * ? * * *" />   
</bean>


中的cronExpression写到另外一个地方:数据库。 

simpleService 和其中注入各属性需要实现Serializable序列化接口,你的BakcupDao继承自HibernateDaoSupport虽然也实现了序列化 接口,但是HibernateDaoSupport里的HibernateTemplate并没有实现序列化接口,所以你取得的 HibernateTemplate永远为null。因此获取HibernateTemplate必须换一种方式,你的BakcupDao不能继承自 HibernateDaoSupport。HibernateTemplate没有实现序列化接口,而SessionFactory是实现序列化接口的, 在bakcupDao注入SessionFactory,通过SessionFactory获取HibernateTemplate。  

你的bakcupDao可以这样写  

Java代码 

  1. import  java.io.Serializable;  

  2.   

  3. import  org.hibernate.SessionFactory;  

  4. import  org.slf4j.Logger;  

  5. import  org.slf4j.LoggerFactory;  

  6. import  org.springframework.beans.factory.annotation.Autowired;  

  7. import  org.springframework.stereotype.Repository;  

  8.   

  9. import  com.sundoctor.example.service.SimpleService;  

  10.   

  11. @Repository ( "bakcupDao" )  

  12. public   class  BakcupDao  implements  Serializable {  

  13.   

  14.       

  15.     private   static   final   long  serialVersionUID = 1L;      

  16.     private  SessionFactory sessionFactory;    

  17.   

  18.     @Autowired   

  19.     public   void  setSessionFactory(SessionFactory sessionFactory) {  

  20.         this .sessionFactory = sessionFactory;         

  21.     }  

  22.   

  23.   

  24.     public   boolean  backupDateabase(String dbname, String bfname) {  

  25.         final  String dbName = dbname;  

  26.         final  String bfname1 = bfname;  

  27.             HibernateTemplate  hibernateTemplate = new  HibernateTemplate(sessionFactory);  

  28.         return  (Boolean)hibernateTemplate.execute( new  HibernateCallback() {  

  29.             public  Object doInHibernate(Session session) {  

  30.                 boolean  flag =  true ;  

  31.                 PreparedStatement pstmt = null ;  

  32.   

  33.                 try  {  

  34.                     pstmt = session.connection().prepareStatement("{call p_Backup_Or_Restore(?,?,?)}");  

  35.                     pstmt.setString(1 , bfname1);  

  36.                     pstmt.setString(2 , dbName);  

  37.                     pstmt.setInt(3 ,  1 );  

  38.                     pstmt.execute();  

  39.                     System.out.println("数据库已备份" );  

  40.                 } catch  (Exception e) {  

  41.                     flag = false ;  

  42.                     e.printStackTrace();  

  43.                 }  

  44.                 return  flag;  

  45.             }  

  46.         });  

  47.     }  

  48. }   


转载于:https://my.oschina.net/xiehongfei/blog/346977

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值