博客
关于我
Spring Boot2.0 集成 Quartz
阅读量:494 次
发布时间:2019-03-07

本文共 3155 字,大约阅读时间需要 10 分钟。

在项目开发中,定时任务是常见需求之一。Spring Boot提供了内置的定时方案,但当任务需求复杂化时,经常需要引入Quartz。以下将从简单到复杂,全面解析两种方案。

Spring Boot内置定时方案

pom.xml配置

仅需引入Spring Boot Starter包即可:

org.springframework.boot
spring-boot-starter

启动类配置

在启动类中添加@EnableScheduling注解:

@SpringBootApplication@EnableSchedulingpublic class Application {    public static void main(String[] args) {        SpringApplication.run(Application.class, args);    }}

定时任务实现

创建实现类并使用@Scheduled注解:

  • 方fixedRate方式:
@Componentpublic class SchedulerTask {    private int count = 0;        @Scheduled(fixedRate = 6000)    private void process() {        System.out.println("this is scheduler task running " + count++);    }}
  • 使用cron表达式:
@Componentpublic class Scheduler2Task {    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");        @Scheduled(cron="*/6 * * * * ?", fixedRate=6000)    public void reportCurrentTime() {        System.out.println("现在时间:" + dateFormat.format(new Date()));    }}

参数说明

  • fixedRate:每隔固定秒数执行任务。
  • fixedDelay:任务完成后等待固定秒数后再执行。
  • initialDelay:第一次调度的延迟间隔。
  • cron表达式:支持灵活时间设置。

Quartz定时方案介绍

Quartz简介

Quartz是一个开源的任务调度框架,支持复杂定时需求且能与Spring无缝集成。

核心概念

  • Job:定时任务接口。
  • JobDetail:任务详细信息。
  • Trigger:触发器,定义调度规则。
  • Scheduler:调度器,管理任务执行。

Spring Boot整合

在Spring Boot中使用Quartz需如下配置:

org.springframework.boot
spring-boot-starter-quartz

简单示例

定义Job并注册Trigger:

public class SampleJob extends QuartzJob {    private String name;    public void setName(String name) {        this.name = name;    }    @Override    protected void executeInternal(JobExecutionContext context) throws JobExecutionException {        System.out.println("Hello " + this.name + "!");    }}

配置JobDetail和Trigger:

@Configurationpublic class SampleScheduler {    @Bean    public JobDetail sampleJobDetail() {        return JobBuilder.newJob(SampleJob.class)                .withIdentity("sampleJob", "group1")                .usingJobData("name", "World")                .build();    }    @Bean    public Trigger sampleJobTrigger() {        SimpleScheduleBuilder scheduleBuilder = SimpleScheduleBuilder.simpleSchedule()                .withIntervalInSeconds(2)                .repeatForever();        return TriggerBuilder.newTrigger()                .forJob(sampleJobDetail())                .withIdentity("sampleTrigger", "group1")                .withSchedule(scheduleBuilder)                .build();    }}

启动定时任务

可以采取以下方式启动:

方式一:在启动类中启动

@SpringBootApplicationpublic class MainApplication extends CommandLineRunner {    @Autowired    privateatabase database;    public static void main(String[] args) {        SpringApplication.run(MainApplication.class, args);    }    @Override    public void run(String[] args) throws Exception {        schedulerService.start();    }}

方式二:集成Spring的@Scheduled注解

public class SchedulerListener {    @Autowired    private SchedulerService schedulerService;    @Scheduled(cron="0 30 11 25 11 ?", enabled=true)    public void schedule() {        schedulerService.start();    }}

总结

对于简单定时需求,Spring Boot内置方案足够;对复杂批量任务处理,经常选择Quartz。Spring Boot 2.0对Quartz的支持使其在项目中的应用更加流畅。

转载地址:http://ergdz.baihongyu.com/

你可能感兴趣的文章
OpenCV与AI深度学习 | CoTracker3:用于卓越点跟踪的最新 AI 模型
查看>>
OpenCV与AI深度学习 | OpenCV中八种不同的目标追踪算法
查看>>
OpenCV与AI深度学习 | OpenCV图像拼接--Stitching detailed使用与参数介绍
查看>>
OpenCV与AI深度学习 | OpenCV如何读取仪表中的指针刻度
查看>>
OpenCV与AI深度学习 | OpenCV常用图像拼接方法(一) :直接拼接
查看>>
OpenCV与AI深度学习 | OpenCV常用图像拼接方法(三):基于特征匹配拼接
查看>>
OpenCV与AI深度学习 | OpenCV常用图像拼接方法(二) :基于模板匹配拼接
查看>>
OpenCV与AI深度学习 | OpenCV常用图像拼接方法(四):基于Stitcher类拼接
查看>>
OpenCV与AI深度学习 | OpenCV快速傅里叶变换(FFT)用于图像和视频流的模糊检测(建议收藏!)
查看>>
OpenCV与AI深度学习 | PaddleOCR 2.9 发布, 正式开源文本图像智能分析利器
查看>>
OpenCV与AI深度学习 | SAM2(Segment Anything Model 2)新一代分割一切大模型介绍与使用(步骤 + 代码)
查看>>
OpenCV与AI深度学习 | T-Rex Label !超震撼 AI 自动标注工具,开箱即用、检测一切
查看>>
OpenCV与AI深度学习 | YOLO11介绍及五大任务推理演示(目标检测,图像分割,图像分类,姿态检测,带方向目标检测)
查看>>
OpenCV与AI深度学习 | YOLOv10在PyTorch和OpenVINO中推理对比
查看>>
OpenCV与AI深度学习 | YOLOv11来了:将重新定义AI的可能性
查看>>
OpenCV与AI深度学习 | YOLOv8自定义数据集训练实现火焰和烟雾检测(代码+数据集!)
查看>>
OpenCV与AI深度学习 | YOLOv8重磅升级,新增旋转目标检测,又该学习了!
查看>>
OpenCV与AI深度学习 | 一文带你读懂YOLOv1~YOLOv11(建议收藏!)
查看>>
OpenCV与AI深度学习 | 五分钟快速搭建一个实时人脸口罩检测系统(OpenCV+PaddleHub 含源码)
查看>>
OpenCV与AI深度学习 | 什么是 COCO 数据集?
查看>>