Spring3+Quarz實現定時任務

1、首先引入quarz所須要的jar包,以下:java

    <dependency>
            <groupId>org.quartz-scheduler</groupId>
            <artifactId>quartz</artifactId>
            <version>1.8.6</version>
       </dependency>web

2、編寫定時任務spring

定義一個普通的JAVA類,不須要繼承或者實現其它的父類app

package com.liyi.test.quarz;

import java.util.Date;

public class SpringQuarz {
    public void test(){
        System.out.println(new Date()+"觸發一次定時任務");
    }
}

3、spring配置定時任務,我是本身在web-inf下面新建了一個文件夾,專門放置quarz定時任務的配置文件,在主的spring配置文件applicationContent.xml中import單個的定時任務。code

    一、在applicationContext.xml中,引入定時任務xml

<!-- 定時任務 -->
    <import resource="spring/spring_quarz_springquarz.xml"/>

二、定時任務詳細配置(必定要記得加入對應的命名空間)繼承

    如下是我我的理解,你要配置一個定時任務,須要定義四部分主要內容:get

    a)你須要把你的定時任務類加入到spring 容器 中,你能夠用註解,也能夠用配置bean的方式。io

    b)org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean ,配置一個詳細調度任務的配置。class

        裏面有兩個參數,一個是 targetObject:引用你的定時任務類  一個是targetMethod :定時任務類裏面的執行方法

    c) org.springframework.scheduling.quartz.CronTriggerBean,調度器的配置,說白了就是指定任務的調度時間裏面也

        有兩個參數,一個是jobDetail:對應你配置詳細調度任務的bean 一個是 cronExpression 執行時間

    d)org.springframework.scheduling.quartz.SchedulerFactoryBean 觸發任務的工廠。裏面也有兩個參數一個是

        jobDetails:對應的是你的任務調度詳細配置,一個是triggers就是調度器的時間,這兩個就是指向你剛纔配的兩個bean

        就對了。

<?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:context="http://www.springframework.org/schema/context"
    xmlns:lang="http://www.springframework.org/schema/lang"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:task="http://www.springframework.org/schema/task"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.0.xsd
        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
    <!-- 把定時任務的類注入到spring 中 -->
    <bean id="springQuarz" class="com.liyi.test.quarz.SpringQuarz"></bean>
    
    <!-- 配置定時任務的做業類 -->
    <bean id="springQuarzDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <property name="targetObject">
            <ref bean="springQuarz"></ref>
        </property>
        <property name="targetMethod">
            <value>test</value>
        </property>
    </bean>
    <!-- 配置定時任務的調度器 -->
      <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
         <property name="jobDetail" ref="springQuarzDetail" /> 
         <property name="cronExpression" value="0/5 * * * * ?" /> 
     </bean> 

<!--      <bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">  -->
<!--           <property name="jobDetail" ref="springQuarzDetail" /> -->
<!--              <property name="repeatInterval" value="5000" /> -->
<!--           <property name="startDelay" value="1000" /> -->
<!--      </bean>  -->
    
     <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="jobDetails">
           <list>
              <ref bean="springQuarzDetail" />
           </list>
        </property>
     
        <property name="triggers">
            <list>
            <ref bean="cronTrigger" />
            </list>
        </property>
    </bean>

</beans>

啓動項目,項目就會每隔5秒執行一下你的定時任務。

相關文章
相關標籤/搜索