作者:wyzf88_987 | 来源:互联网 | 2023-08-17 20:01
一 Quartz
一个开源的作业调度框架,配置执行定时任务
二 配置
1 依赖
<dependency>
<groupId>org.quartz-schedulergroupId>
<artifactId>quartzartifactId>
<version>2.2.3version>
dependency>
<dependency>
<groupId>org.quartz-schedulergroupId>
<artifactId>quartz-jobsartifactId>
<version>2.2.3version>
dependency>
2 web.xml
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
listener>
<context-param>
<param-name>contextConfigLocationparam-name>
<param-value>
classpath:spring-quartz.xml
param-value>
context-param>
3.spring-quartz.xml
<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">
<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>
<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);
@Override
public void run() {
try {
System.out.println("importOneTable任务开始执行");
System.out.println("importOneTable任务执行结束");
} finally {
Conn.close();
}
}
}
三 定时任务和Cron表达式
Cron表达式多用在调度任务,用来表示时间例如每个一周,每隔一个小时,具体用法在我的另一片博客中有详细介绍
点击:SpringBoot定时任务@EnableScheduling和cron表达式
定时任务的方法也很多,上面链接那篇博客中介绍了@EnableScheduling实现定时任务
文章基于SpringBoot,以注解的形式配置,那同样quartz是否可以在SpringBoot那种简化配置文件的风格中,以注解来配置呢?答案是可以的,大家可以参考这篇文章:
springboot整合Quartz实现动态配置定时任务