quartz(3)--spring整合quartz入门案例

第一步:导入jar


 

<!-- quartz -->
<dependency>
    <groupId>org.quartz-scheduler</groupId>
    <artifactId>quartz</artifactId>
    <version>2.1.7</version>
</dependency>

 

第二步:测试Task


 

package com.org.qin.quartz.task;
import java.util.Date;
public class TestTask {

    public void execute() {
        System.out.println("TestTask执行一次,时间:" + new Date());
    }
}

 

第三步:spring配置文件


 

<?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:mvc="http://www.springframework.org/schema/mvc"
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd 
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd ">
    
     
     <!-- 测试Task -->
     <bean id="testTask" class="com.org.qin.quartz.task.TestTask"></bean> 
     
     <!-- 配置JobDetail -->
     <bean id="testJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> 
         <property name="targetObject" ref="testTask"></property> 
         <property name="targetMethod"> 
             <value>execute</value> 
         </property> 
     </bean> 
     <!-- 配置Trigger -->
     <bean id="testJobTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> 
         <property name="jobDetail" ref="testJob" ></property>
         <property name="cronExpression"> 
             <value>* */1 * * * ?</value> 
         </property> 
      </bean> 

    <!-- 配置scheduler -->
    <bean id="scheduler" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
          <list> <ref bean = "testJobTrigger" /> </list>
        </property>
    </bean>
</beans>

 

第四步:测试


 

 tomcat启动项目,查看执行情况,一秒钟执行一次

 

posted on 2017-03-09 15:41  芹derella  阅读(1664)  评论(2编辑  收藏  举报