spring quartz 定时器

转自:http://hacker-zxf.iteye.com/blog/296554

首先建立bean.xml文件: 

Java代码    收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">  
  3. <beans>  
  4.     <!--起动Bean-->  
  5.     <bean id="test"  
  6.         class="org.springframework.scheduling.quartz.SchedulerFactoryBean">  
  7.         <property name="triggers">  
  8.             <list>  
  9.                 <ref bean="cronReportTrigger" />  
  10.             </list>  
  11.         </property>  
  12.     </bean>  
  13.     <!--实际的工作Bean-->  
  14.     <bean id="testjobbean"  
  15.         class="com.zhouxf.quartz.TestJobBean">  
  16.     </bean>  
  17.     <!--jobBean用于设定启动时运用的Bean与方法-->  
  18.     <bean id="scheduledReportJobDetail"  
  19.         class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">  
  20.         <property name="targetObject">  
  21.             <ref bean="testjobbean" />  
  22.         </property>  
  23.         <property name="targetMethod">  
  24.             <value>run</value>  
  25.         </property>  
  26.     </bean>  
  27.     <!--定时器设定(0/2 43 12-17 * * ?在121743分,每隔2秒运行一次)-->  
  28.     <bean id="cronReportTrigger"  
  29.         class="org.springframework.scheduling.quartz.CronTriggerBean">  
  30.         <property name="jobDetail">  
  31.             <ref bean="scheduledReportJobDetail" />  
  32.         </property>  
  33.         <property name="cronExpression">  
  34.             <value>0/2 43 12-17 * * ?</value>  
  35.         </property>  
  36.     </bean>  
  37.   
  38. </beans>  




JOB代码: 

Java代码    收藏代码
  1. public class TestJobBean{  
  2.   
  3.     public void run() {  
  4.         System.out.println("run..................");  
  5.     }  
  6. }  



测试代码: 

Java代码    收藏代码
  1. public static void main(String[] args) throws BeansException, FileNotFoundException, InterruptedException {  
  2.        // new LogInit("WEB-INF/classes/com/spring/helloworld/log4j.properties");  
  3.        BeanFactory factory = new XmlBeanFactory(  
  4.                                                 new FileSystemResource(  
  5.                                                                        "///home/zhouxf/work/WebPro/WebContent/WEB-INF/bean.xml"));  
  6.        factory.getBean("test");  
  7.   
  8.    }  




还有另一种配置方式,使用QuartzJobBean,它是JobDetailBean的子类,代码如下: 

Java代码    收藏代码
  1. import org.quartz.JobExecutionContext;  
  2. import org.quartz.JobExecutionException;  
  3. import org.springframework.scheduling.quartz.QuartzJobBean;  
  4.   
  5. public class TestJobBean extends QuartzJobBean {  
  6.   
  7.     @Override  
  8.     protected void executeInternal(JobExecutionContext arg0) throws JobExecutionException {  
  9.         // TODO Auto-generated method stub  
  10.         System.out.println("TestJobBean..run................");  
  11.     }  
  12.   
  13. }  




Java代码    收藏代码
  1. 配置文件中加入:  
  2. <bean id="reportJob"   
  3.        class="org.springframework.scheduling.quartz.JobDetailBean">   
  4.     <property name="jobClass">   
  5.       <value>com.zhouxf.quartz.TestJobBean</value>   
  6.     </property>    
  7.   </bean>   
  8.     
  9.     <!--定时器设定(0/2 43 12-17 * * ?在121743分,每隔2秒运行一次)-->  
  10.     <bean id="cronReportTrigger2"  
  11.         class="org.springframework.scheduling.quartz.CronTriggerBean">  
  12.         <property name="jobDetail">  
  13.             <ref bean="reportJob" />  
  14.         </property>  
  15.         <property name="cronExpression">  
  16.             <value>0/1 31 12-17 * * ?</value>  
  17.         </property>  
  18.     </bean>  




测试代码改为: 

Java代码    收藏代码
  1. public static void main(String[] args) throws BeansException, FileNotFoundException, InterruptedException {  
  2.         // new LogInit("WEB-INF/classes/com/spring/helloworld/log4j.properties");  
  3.         BeanFactory factory = new XmlBeanFactory(  
  4.                                                  new FileSystemResource(  
  5.                                                                         "///home/zhouxf/work/WebPro/WebContent/WEB-INF/bean.xml"));  
  6.         factory.getBean("test");  
  7.         factory.getBean("test2  
  8.     }  





默认情况下,Quartz Jobs是无状态的,可能导致jobs之间互相的影响。如果你为相同的JobDetail指定两个Trigger, 很可能当第一个job完成之前,第二个job就开始了。如果JobDetail对象实现了Stateful接口,就不会发生这样的事情。 第二个job将不会在第一个job完成之前开始。为了使得jobs不并发运行,设置MethodInvokingJobDetailFactoryBean中的concurrent标记为false。 

Java代码    收藏代码
  1. <bean id="scheduledReportJobDetail"  
  2.         class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">  
  3.         <property name="targetObject">  
  4.             <ref bean="simpleJob" />  
  5.         </property>  
  6.         <property name="targetMethod">  
  7.             <value>run</value>  
  8.         </property>  
  9.         <property name="concurrent" value="false" />  
  10. </bean>  


但是第二种方式的,没办法设置这个属性,暂时还不知道怎么去设置这个属性。

猜你喜欢

转载自sagewsg.iteye.com/blog/1502867