一 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实现动态配置定时任务