添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-quartz</artifactId>
<version>2.3.11.RELEASE</version>
</dependency>
创建调度器
import lombok.extern.slf4j.Slf4j;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.impl.StdSchedulerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
@Configuration
@Slf4j
@Order(99)
public class TaskConfig implements CommandLineRunner {
public void init() {
schedulerStart();
}
@Override
public void run(String... args) {
init();
}
public void schedulerStart() {
try {
Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
scheduler.start();
} catch (SchedulerException e) {
log.info("--- 启动Scheduler失败 ---", e);
}
}
}
任务基类
import com.ganguomob.dev.base.infrastructure.exception.CommonBusinessException; import l