SpringBoot整合Quartz

添加依赖

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

配置quartz属性

复制代码
spring.quartz.properties.org.quartz.scheduler.instanceName=myScheduler
spring.quartz.properties.org.quartz.scheduler.instanceId=AUTO
spring.quartz.properties.org.quartz.jobStore.class=org.quartz.impl.jdbcjobstore.JobStoreTX
spring.quartz.properties.org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.StdJDBCDelegate
spring.quartz.properties.org.quartz.jobStore.tablePrefix=QRTZ_
spring.quartz.properties.org.quartz.jobStore.isClustered=true
spring.quartz.properties.org.quartz.jobStore.clusterCheckinInterval=20000

这是一个基本的Quartz配置,你可能需要根据你的需求进行修改。

创建Job

创建一个实现Job接口的类,该类定义了需要执行的任务逻辑。例如:

java 复制代码
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

public class MyJob implements Job {

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        // 执行你的任务逻辑
        System.out.println("Job executed!");
    }
}

配置JobDetail和Trigger

在@Configuration类中配置JobDetail和Trigger,并将它们注册到Scheduler中:

java 复制代码
import org.quartz.JobDetail;
import org.quartz.SimpleTrigger;
import org.quartz.Trigger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class QuartzConfig {

    @Bean
    public JobDetail myJobDetail() {
        return JobBuilder.newJob(MyJob.class)
                .withIdentity("myJob")
                .storeDurably()
                .build();
    }

    @Bean
    public Trigger myTrigger(JobDetail job) {
        return TriggerBuilder.newTrigger()
                .forJob(job)
                .withIdentity("myTrigger")
                .withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(10).repeatForever())
                .build();
    }
}

这里的myTrigger配置了一个简单的触发器,每隔10秒执行一次。

启用Quartz Scheduler

在你的应用主类上添加@EnableScheduling注解,以启用Spring的调度功能:

java 复制代码
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableScheduling
public class YourApplication {

    public static void main(String[] args) {
        SpringApplication.run(YourApplication.class, args);
    }
}

运行项目

现在,当你运行应用时,Quartz会自动调度你配置的任务。

以上只是一个简单的Quartz整合示例,你可能需要根据实际需求进行更复杂的配置。确保了解Quartz的相关概念,例如JobDetail、Trigger、Scheduler等。

相关推荐
xcLeigh几秒前
Go入门:整数类型的溢出与安全边界
java·安全·golang
源码宝30 分钟前
SpringBoot+Vue2 诊所门诊管理系统,完整前后端源码,可直接部署上线
java·源码·his系统·门诊系统·程序代码·诊所系统·门诊his
yychen_java32 分钟前
九:Text-to-SQL 智能数据查询与 Human-in-the-Loop 人机协作
java·人工智能·架构
user_admin_god1 小时前
数据采集/交换系统设计经验
spring boot·spring·策略模式
lhldsg2 小时前
智慧场馆解决方案小程序开发实战:从架构设计到部署指南
java·小程序·uni-app
Freak嵌入式2 小时前
Pico UART 数据收发实战:硬件配置、MicroPython 编程
java·开发语言·单片机·嵌入式硬件·生活
devilnumber3 小时前
Oracle 与 MySQL substr 函数差异总结
java·数据库·mysql·oracle
cfm_29143 小时前
线程池阻塞队列
java·开发语言
学代码的CJY3 小时前
Linux 重定向、管道与环境变量
linux·spring boot·spring
坐吃山猪3 小时前
【多线程】ThreadPool线程池参数说明
java·网络