Spring整合Quartz的方法


一、增加所依赖的JAR包

1、增加Spring的Maven依赖

Xml代码
  1. <dependency>  
  2. <groupId>org.springframework</groupId>  
  3. <artifactId>spring-webmvc</artifactId>  
  4. <version>3.0.5.RELEASE</version>  
  5. </dependency>  

2、增加Quartz的Maven依赖

Xml代码
  1. < dependency >  
  2.     < groupId > org.quartz-scheduler </ groupId >    
  3.     < artifactId > quartz </ artifactId >    
  4.     < version > 1.8.4 </ version >  
  5.  </ dependency >  

二、Spring整合Quartz的两种方法

    1、使用JobDetailBean

       创建job,继承QuartzJobBean ,实现 executeInternal(JobExecutionContext jobexecutioncontext)方法。这种方法和在普通的Quartz编程中是一样的。

       JobDetail对象包含运行一个job的所有信息。 Spring Framework提供了一个JobDetailBean,包含运行一个job的所有信息。

 

Xml代码
  1. <bean name="exampleJob" class="org.springframework.scheduling.quartz.JobDetailBean">  
  2.   <property name="jobClass" value="example.ExampleJob" />  
  3.   <property name="jobDataAsMap">  
  4.     <map>  
  5.       <entry key="timeout" value="5" />  
  6.     </map>  
  7.   </property>  
  8. </bean> 

job data map(jobDataAsMap)可通过JobExecutionContext (执行时传递)获取。JobDetailBean将 job data map的属性映射到job的属性。如例所示,如果job类中包含一个job data map中属性,JobDetailBean将自动应用,可用于传递参数:

Java代码
  1. public class ExampleJob extends QuartzJobBean{   
  2.   
  3.     private int timeout;   
  4.   
  5.     /**  
  6.      * Setter called after the ExampleJob is instantiated with the value from  
  7.      * the JobDetailBean (5)  
  8.      */  
  9.     public void setTimeout(int timeout) {   
  10.         this.timeout = timeout;   
  11.     }   
  12.   
  13.     /**  
  14.      *   
  15.      * 业务逻辑处理  
  16.      *   
  17.      */  
  18.   
  19.     protected void executeInternal(JobExecutionContext ctx)   
  20.             throws JobExecutionException {   
  21.         // do the actual work   
  22.     }   
  23. }  

 2、使用MethodInvokingJobDetailFactoryBean  

     通过MethodInvokingJobDetailFactoryBean在运行中动态生成,需要配置执行任务的目标类、目标方法。但是这种方法动态生成的JobBean不支持序列号,也就是说Job不能存到持久化。

     通常用于调用特定对象的一个方法。不用创建单独的job对象,只需要建立正常的业务对象,用这样方式去调用其中的一个方法。

Xml代码

  1. <bean id="jobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">  
  2.   <property name="targetObject" ref="exampleBusinessObject" />  
  3.   <property name="targetMethod" value="doIt" />  
  4.   <property name="arguments">  
  5.     <list>  
  6.         <!-- a primitive type (a string) -->  
  7.         <value>1st</value>  
  8.         <!-- an inner object definition is passed as the second argument -->  
  9.         <object type="Whatever.SomeClass, MyAssembly" />  
  10.         <!-- a reference to another objects is passed as the third argument -->  
  11.         <ref object="someOtherObject" />  
  12.         <!-- another list is passed as the fourth argument -->  
  13.         <list>  
  14.             <value>http://www.springframework.net/</value>  
  15.         </list>  
  16.     </list>  
  17.   </property>  
  18.   
  19.   <property name="concurrent" value="false" />  
  20. </bean>  
  • 1
  • 2
  • 下一页

相关内容