Spring配置Quartz實現定時調度任務

一 Quartz

一個開源的作業調度框架,配置執行定時任務

二 配置

1 依賴

<dependency>
    <groupId>org.quartz-scheduler</groupId>
    <artifactId>quartz</artifactId>
    <version>2.2.3</version>
  </dependency>
  <dependency>
    <groupId>org.quartz-scheduler</groupId>
    <artifactId>quartz-jobs</artifactId>
    <version>2.2.3</version>
 </dependency>

2 web.xml

<listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            classpath:spring-quartz.xml
        </param-value>
    </context-param>

3.spring-quartz.xml

<?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:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="  
        http://www.springframework.org/schema/aop   
        http://www.springframework.org/schema/aop/spring-aop.xsd  
        http://www.springframework.org/schema/beans   
        http://www.springframework.org/schema/beans/spring-beans.xsd  
        http://www.springframework.org/schema/tx   
        http://www.springframework.org/schema/tx/spring-tx.xsd  
        http://www.springframework.org/schema/context   
        http://www.springframework.org/schema/context/spring-context.xsd
        "
    default-autowire="byType">
<!-- ======================== 任務 Task配置 ======================== --> 
<!--由MethodInvokingJobDetailFactoryBean實現-->
   <bean id="importOneJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <property name="targetObject" ref="importOneTable" />          //執行類的實例
        <property name="targetMethod" value="run" />                   //執行方法
        <property name="concurrent" value="false" />
        <property name="arguments">
            <list></list>
        </property>
    </bean> 

<!-- ======================== 配置定時調度 觸發器 ======================== -->
<!--由CronTriggerFactoryBean實現-->
   <bean id="cronTrigger2" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> 
        <property name="jobDetail" ref="importOneJob" />                 //上面任務的Task配置bean
        <property name="cronExpression" value="0 */1 * * * ?" />         //觸發時機表達式  cron表達式在文章的最末尾會說
    </bean> 
 <!-- ======================== 調度工廠(中心調度器) ======================== -->
     <bean id="schedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"
        autowire="no">
        <property name="triggers">
            <list>
                <ref bean="cronTrigger2" />                           //上面配置的觸發器
            </list>
        </property>
    </bean> 
</beans>  

上面的觸發器配置中可配置CronTriggerBean實現,若報錯:

Caused by: java.lang.IncompatibleClassChangeError:class org.springframework.scheduling.quartz.SimpleTriggerBean has interface org.quartz.SimpleTrigger as super class。

是因爲Quartz2修改了部分API
將CronTriggerBean改爲CronTriggerFactoryBean,JobDetailBean改爲JobDetailFactoryBean即可

4 執行類

package com.xxxx.xx.job;
import com.xxxx.xx.util.Conn;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;


@Component("importOneTable")
public class importOneTable implements Runnable {
    private final static Logger logger = LoggerFactory.getLogger(ImportOneTable.class);

    //上面Task配置中   執行的方法
    @Override
    public void run() {
        try {
            System.out.println("importOneTable任務開始執行");
            //此處執行具體的importOneTable方法
            System.out.println("importOneTable任務執行結束");
        } finally {
            Conn.close();
        }
    }
}

三 定時任務和Cron表達式

Cron表達式多用在調度任務,用來表示時間例如每個一週,每隔一個小時,具體用法在我的另一片博客中有詳細介紹
點擊:SpringBoot定時任務@EnableScheduling和cron表達式

定時任務的方法也很多,上面鏈接那篇博客中介紹了@EnableScheduling實現定時任務
文章基於SpringBoot,以註解的形式配置,那同樣quartz是否可以在SpringBoot那種簡化配置文件的風格中,以註解來配置呢?答案是可以的,大家可以參考這篇文章:
springboot整合Quartz實現動態配置定時任務

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章