!!!超简单 springboot2.0中 单机 quartz yml文件配置 持久化到数据库 看完不会你打我

本文介绍如何在Spring Boot项目中使用Quartz实现定时任务,包括数据库表创建、依赖引入、YML配置、Job类定义及任务调度。

创建表

可到官网下载源码 解压之后。在docs\dbTables文件下选择自己所需要的slq文件。下载地址

添加引用

   <!--quartz-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-quartz</artifactId>
        </dependency>

编辑yml文件

spring:
    quartz:
    properties:
      org:
        quartz:
          scheduler:
            instanceName: clusteredScheduler
            instanceId: AUTO
          jobStore:
            class: org.quartz.impl.jdbcjobstore.JobStoreTX
            driverDelegateClass: org.quartz.impl.jdbcjobstore.StdJDBCDelegate
            tablePrefix: QRTZ_
            useProperties: false
            dataSource: myDs
          threadPool:
            class: org.quartz.simpl.SimpleThreadPool
            threadCount: 10
            threadPriority: 5
            threadsInheritContextClassLoaderOfInitializingThread: true
          dataSource:
            myDs:
              driver: com.mysql.jdbc.Driver
              URL: ****
              user: ****
              password: ****

    jdbc-store-type: jdbc

jobStore中的dataSource的值myDs和下面的dataSource的myDs对应

添加job类

public class OrderTimeoutJob extends QuartzJobBean {
    private Long orderId;
    @Autowired
    private ShopOrderService orderService;

    @Override
    protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
       //代码
    }

    public void setOrderId(Long orderId) {
        this.orderId = orderId;
    }

}

项目使用

//import org.quartz.JobDetail;
//import org.quartz.Scheduler;
//import org.quartz.SchedulerException;
//import org.quartz.SimpleTrigger;

@Component
public class OrderJobMaker {
    @Autowired
    private Scheduler Scheduler;
    public void addScheme(){
        int timeout=10
        Long id=1000L;        
         SimpleTrigger trigger=(SimpleTrigger) newTrigger()
                .withIdentity("trigger-"+id, group)
                .startAt(futureDate(timeout, IntervalUnit.MINUTE)) // use DateBuilder to create a date in the future
                .build();

        JobDetail job = newJob(OrderTimeoutJob.class)
                .withIdentity("job-"+id, groupName)
                .usingJobData("orderId",id)
                .build();
        Scheduler.scheduleJob(job, trigger);
    }

}
<think>好的,我现在需要帮助用户解决在Spring Boot中集成Quartz,并通过YML配置文件完成设置而不依赖数据库的问题。首先,我得回顾一下Quartz在Spring Boot中的基本集成方法。 用户明确要求不使用数据库,这意味着需要配置Quartz使用内存存储(RAMJobStore)而不是JDBCJobStore。在Spring Boot中,通常可以通过配置属性来设置Quartz的属性,特别是使用YML文件。我需要确认相关的配置属性是否正确。 首先,Quartz的存储类型由`org.quartz.jobStore.class`属性决定。内存存储对应的类是`org.quartz.simpl.RAMJobStore`,所以需要确保这个类被正确配置。此外,Spring Boot的Quartz自动配置通常会依赖数据库,如果不数据库,可能需要排除相关的自动配置,或者正确设置属性来覆盖默认行为。 接下来,用户提到使用YML配置文件,我需要回忆Spring Boot中Quartz配置项。在Spring Boot 2.x及以上版本中,Quartz配置可以通过`spring.quartz`前缀的属性进行设置。例如,`spring.quartz.job-store-type`可以设置为`memory`来指定使用内存存储。这可能比直接配置Quartz属性更简单,因为Spring Boot会自动处理这些配置。 另外,需要确保项目中包含了Quartz的依赖。通常,Spring Boot提供了`spring-boot-starter-quartz` starter,需要添加到项目的依赖管理中。如果用户的项目是基于Maven或Gradle的,需要确认pom.xml或build.gradle文件中包含这个依赖。 然后,关于不依赖数据库的问题,除了设置存储类型为内存之外,还需要排除任何可能触发数据库初始化的配置。例如,检查是否有其他配置引用了数据库相关的属性,或者是否有数据源的自动配置被启用。如果用户没有配置数据源,Spring Boot可能会默认创建一个内存数据库,比如H2,但如果用户明确不希望使用任何数据库,可能需要排除数据源的自动配置。不过,在Quartz的内存模式下,可能不需要数据源,因此只要正确设置Quartz的存储类型即可。 现在,考虑具体的YML配置。正确的配置应该包括以下内容: ```yaml spring: quartz: job-store-type: memory properties: org: quartz: scheduler: instanceName: MyScheduler threadPool: threadCount: 5 ``` 这里,`job-store-type`设置为`memory`,这会让Spring Boot配置Quartz使用RAMJobStore。同时,可以通过`properties`字段进一步定制Quartz的其他属性,比如线程池的大小。 另外,需要确保依赖正确。在Maven项目中,需要添加: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-quartz</artifactId> </dependency> ``` 然后,用户需要创建Job和Trigger的配置类。例如,定义一个JobDetail和Trigger的Bean,这样Quartz就会在应用启动时调度这些任务。 可能遇到的问题:如果用户没有正确设置`job-store-type`,Spring Boot可能会尝试使用数据库存储,导致需要配置数据源。因此,确认配置是否正确是关键。 最后,还需要提醒用户,使用内存存储的Quartz在应用重启后不会持久化任务,适用于测试或非持久化需求的场景。如果需要持久化,必须使用数据库存储。 总结步骤: 1. 添加Quartz starter依赖。 2. 配置YML文件,设置job-store-type为memory。 3. 定义Job和Trigger的Bean。 4. 排除数据源自动配置(如果有必要)。 需要验证这些步骤是否正确,以及是否有遗漏的配置项。例如,是否需要配置其他Quartz属性,如线程池大小,或者是否会有默认值足够使用。</think>在Spring Boot中集成Quartz并避免依赖数据库,可以通过以下步骤实现: --- ### **1. 添加依赖** 在`pom.xml`中引入Spring Boot Quartz Starter: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-quartz</artifactId> </dependency> ``` --- ### **2. 配置YML文件** 在`application.yml`中设置Quartz为内存模式: ```yaml spring: quartz: job-store-type: memory # 使用内存存储而非数据库 properties: org: quartz: scheduler: instanceName: MyScheduler threadPool: threadCount: 5 # 线程池大小 jobStore: class: org.quartz.simpl.RAMJobStore # 显式指定内存存储 ``` --- ### **3. 定义Job和Trigger** 创建定时任务配置类: ```java @Configuration public class QuartzConfig { @Bean public JobDetail sampleJobDetail() { return JobBuilder.newJob(SampleJob.class) .withIdentity("sampleJob") .storeDurably() .build(); } @Bean public Trigger sampleJobTrigger() { SimpleScheduleBuilder schedule = SimpleScheduleBuilder.simpleSchedule() .withIntervalInSeconds(10) .repeatForever(); return TriggerBuilder.newTrigger() .forJob(sampleJobDetail()) .withIdentity("sampleTrigger") .withSchedule(schedule) .build(); } } ``` --- ### **4. 实现Job逻辑** 定义具体的Job类: ```java public class SampleJob implements Job { @Override public void execute(JobExecutionContext context) { System.out.println("Job executed at: " + new Date()); } } ``` --- ### **关键配置说明** 1. **`job-store-type: memory`** 强制Quartz使用内存存储,避免依赖数据库[^1]。 2. **`RAMJobStore`** Quartz默认的内存存储实现类,无需额外依赖。 3. **线程池配置** 通过`threadCount`控制并发任务数量。 --- ### **注意事项** - **数据非持久化**:内存模式下任务信息在应用重启后丢失。 - **排除数据库依赖**:确保未配置`spring.datasource`相关属性,否则需添加`spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration`[^2]。 ---
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值