SpringBoot集成Quartz

一、引入依赖

xml 复制代码
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-quartz</artifactId>
</dependency>

二、示例任务

java 复制代码
import lombok.extern.slf4j.Slf4j;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;

import java.util.Date;

@Slf4j
public class DemoJob extends QuartzJobBean {

    @Override
    protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
        context.getJobDetail().getJobDataMap().forEach(
                (k, v) -> log.info("param, key:{}, value:{}", k, v)
        );
        log.info("执行时间: " + new Date());
    }
    
}

三、配置

java 复制代码
import org.quartz.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class QuartzConfig {

    @Bean("demoJob")
    public JobDetail helloJobDetail() {
        return JobBuilder.newJob(DemoJob.class)
                .withIdentity("demoJob")
                .usingJobData("msg", "Hello Quartz")
                .storeDurably()
                .build();
    }

    @Bean
    public Trigger printTimeJobTrigger() {
        // 每秒执行一次
        CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule("0/1 * * * * ?");
        return TriggerBuilder.newTrigger()
                .forJob(helloJobDetail())
                .withIdentity("quartzTaskService")
                .withSchedule(cronScheduleBuilder)
                .build();
    }
}
相关推荐
不懂英语的程序猿1 天前
【Java 工具类】Java通过 TCP/IP 调用斑马打印机(完整实现)
java
多多*1 天前
分布式系统中的CAP理论和BASE理论
java·数据结构·算法·log4j·maven
sg_knight1 天前
Docker 实战:如何限制容器的内存使用大小
java·spring boot·spring·spring cloud·docker·容器·eureka
小马爱打代码1 天前
Spring Boot 3 :实现分布式追踪
spring boot·分布式·microsoft
合作小小程序员小小店1 天前
web网页开发,在线考勤管理系统,基于Idea,html,css,vue,java,springboot,mysql
java·前端·vue.js·后端·intellij-idea·springboot
serendipity_hky1 天前
【微服务 - easy视频 | day03】服务与服务之间的调用
spring boot·spring cloud·微服务·架构
随便叫个啥呢1 天前
java使用poi-tl模版+vform自定义表单生成word,使用LibreOffice导出为pdf
java·pdf·word
面向星辰1 天前
扣子开始节点和结束节点
java·服务器·前端
烤麻辣烫1 天前
黑马程序员苍穹外卖(新手)Day1
java·数据库·spring boot·学习·mybatis
失散131 天前
分布式专题——51 ES 深度分页问题及其解决方案详解
java·分布式·elasticsearch·架构