1、quartz.properties配置
org.quartz.scheduler.instanceName = MyScheduler
org.quartz.threadPool.threadCount = 20
org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore
2、创建一个配置类,用于QuartZ的启动配置-QuartZConfig:
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.impl.StdSchedulerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @ClassName: QuartZConfig
* @Description:
* @Author jm
* @Date 2022/6/11
* @Version 1.0
*/
@Configuration
public class QuartZConfig {
/**
* quartz 启动配置
*/
@Bean
public SchedulerFactory quartz() throws SchedulerException {
// Creating scheduler factory and scheduler
SchedulerFactory factory = new StdSchedulerFactory("quartz.properties");
Scheduler scheduler = factory.getScheduler();
scheduler.start();
return factory;
}
}
3、QuartzManage 定时任务工具类
import lombok.extern.slf4j.Slf4j;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
/**
* quartz定时任务工具类,对任务进行管理(创建、修改、删除、暂停)
*
* @author lyf
*/
@Slf4j
public class QuartzManage {
private static final SchedulerFactory SCHEDULER_FACTORY = new StdSchedulerFactory();
/**
* 启动调度任务
*
* @param jobDetail 执行任务
* @param trigger 触发器
*/
private static void startScheduler(JobDetail jobDetail, Trigger trigger) {
try {
// 获取调度任务实例
Scheduler scheduler = SCHEDULER_FACTORY.getScheduler();
// 绑定触发器和调度任务
scheduler.scheduleJob(jobDetail, trigger);
// 启动
scheduler.start();
} catch (SchedulerException e) {
String msg = "启动调度任务失败";
log.error(msg, e);
//throw new Exception(msg);
}
}
/**
* 任务执行实例
*
* @param jobName 任务名称
* @param jobGroupName 任务分组名
* @par