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那種簡化配置檔案的風格中,以註解來配置呢?答案是可以的,大家可以參考這篇文章: --------------------- 作者:TransientBa 來源:CSDN 原文:https://blog.csdn.net/zyb2017/article/details/78997853 版權宣告:本文為博主原創文章,轉載請附上博文連結!