《pro Spring》学习笔记之使用Spring配置Quartz

Spring可以用IOC的方式配置Quartz,就如同配置TimerTask一样方便

job:

 

package  ch14.SpringAndQuartz;

import  org.quartz.Job;
import  org.quartz.JobExecutionContext;
import  org.quartz.JobExecutionException;

public   class  QuartzJob  implements  Job  {

    
public void execute(JobExecutionContext context) throws JobExecutionException {
        System.out.println(context.getFireTime());
        System.out.println(
"this is a message");
        
    }

 
}

 配置文件:

 

<? xml version="1.0" encoding="UTF-8" ?>
< beans
    
xmlns ="http://www.springframework.org/schema/beans"
    xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation
="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd" >

< bean  id ="job"  class ="org.springframework.scheduling.quartz.JobDetailBean" >
   
< property  name ="jobClass" > <!--  此处需要一个string类型的类名  -->
       
< value > ch14.SpringAndQuartz.QuartzJob </ value >
   
</ property >
   
<!--  如果存在JobDataMap,则进行如下配置 
   <property name="jobDataAsMap">
     <map>
       <entry key="message">
         <value>this is a message</value>
       </entry>
     </map>
   </property>
   
-->
   
</ bean >

< bean  id ="trigger"  class ="org.springframework.scheduling.quartz.CronTriggerBean" >
  
< property  name ="jobDetail" >
    
< ref  bean ="job" />
  
</ property >
  
< property   name ="cronExpression" >
   
< value > 2/5 * 23 ? 9 3 </ value >
  
</ property >
</ bean >

< bean  id ="schedulerFactory"  class ="org.springframework.scheduling.quartz.SchedulerFactoryBean" >
  
< property  name ="triggers" >
    
< ref  bean ="trigger" />
  
</ property >
</ bean >

< bean  id ="jobClass"  class ="ch14.SpringAndQuartz.QuartzJob" ></ bean >
</ beans >

 

 

package  ch14.SpringAndQuartz;

import  java.util.Date;

import  org.quartz.CronTrigger;
import  org.quartz.JobDetail;
import  org.quartz.Scheduler;
import  org.quartz.Trigger;
import  org.quartz.impl.StdSchedulerFactory;
import  org.springframework.context.ApplicationContext;
import  org.springframework.context.support.ClassPathXmlApplicationContext;

public   class  TestQuartz  {

    
public static void main(String[] args) throws Exception {
        
        ApplicationContext context
=new ClassPathXmlApplicationContext("ch14/SpringAndQuartz/applicationContext.xml");
        
        System.in.read();
    }


}

 

运行结果

Tue Sep 11 23:19:02 CST 2007
this is a message
Tue Sep 11 23:19:07 CST 2007
this is a message

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值