spring-定时器-QuartzJobBean

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. }  

  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>  
  19. <!-- 启动定时器 -->
  20. <bean name="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
  21. <property name="applicationContextSchedulerContextKey">  
  22.             <value>applicationContext</value>    
  23.         </property>
  24. <property name="triggers">   
  25.         <list><ref bean="cronReportTrigger2"/></list>   
  26.     </property> 
  27. </bean>

测试代码:

  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.     }  

猜你喜欢

转载自jwfdp.iteye.com/blog/1741311