gpt4 book ai didi

Spring Boot中使用Quartz

转载 作者:知者 更新时间:2024-03-12 08:30:09 25 4
gpt4 key购买 nike

本快速教程将在 5 分钟内教您如何在 Spring Boot 应用程序中使用 Quartz Scheduler

首先使用 spring shell 创建您的项目,以便包含Quartz、jpa 和 h2 依赖项(替换为您的实际 JDBC 驱动程序实现):

spring init -dquartz,h2,jpa demo-quartz

以下是将要添加的依赖项列表:

<?xml version="1.0" encoding="UTF-8"?><project>
      
   <dependencies>
               
      <dependency>
                      
         <groupId>org.springframework.boot</groupId>
                      
         <artifactId>spring-boot-starter-data-jpa</artifactId>
                  
      </dependency>
               
      <dependency>
                      
         <groupId>org.springframework.boot</groupId>
                      
         <artifactId>spring-boot-starter-quartz</artifactId>
                  
      </dependency>
                
      <dependency>
                      
         <groupId>org.quartz-scheduler</groupId>
                      
         <artifactId>quartz</artifactId>
                  
      </dependency>
                 
      <dependency>
                      
         <groupId>com.h2database</groupId>
                      
         <artifactId>h2</artifactId>
                  
      </dependency>
               
      <dependency>
                      
         <groupId>org.springframework.boot</groupId>
                      
         <artifactId>spring-boot-starter-test</artifactId>
                      
         <scope>test</scope>
                  
      </dependency>
           
   </dependencies>
     
</project>

让我们添加前两个将由Quartz 调度程序触发的作业类:

package com.masterspringboot.quartz.job;

import org.quartz.Job;
import org.quartz.JobExecutionContext;

public class JobOne implements Job {
  @Override
  public void execute(JobExecutionContext context) {
    System.out.println("Hello from Job One!");
  }
}

还有一个:

package com.masterspringboot.quartz.job;

import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;

public class JobTwo implements Job {
  @Override
  public void execute(JobExecutionContext context) {
    JobDataMap jobDataMap = context.getMergedJobDataMap();
    String data = jobDataMap.getString("somedata");
    System.out.println("Job Two fired with data: " + data);
  }
}

现在是一个 @Configuration 类,它将创建用于执行作业的触发器:

package com.masterspringboot.quartz.job;

import org.quartz.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.UUID;

@Configuration
public class ConfigureQuartzJob {
  @Bean
  public JobDetail jobADetails() {
    return JobBuilder.newJob(JobOne.class).withIdentity("sampleJobA").storeDurably().build();
  }

  @Bean
  public Trigger jobATrigger(JobDetail jobADetails) {
    return TriggerBuilder.newTrigger()
        .forJob(jobADetails)
        .withIdentity("TriggerA")
        .withSchedule(CronScheduleBuilder.cronSchedule("0/20 * * ? * * *"))
        .build();
  }

  @Bean
  public JobDetail jobBDetails() {
    return JobBuilder.newJob(JobTwo.class).withIdentity("sampleJobB").storeDurably().build();
  }

  @Bean
  public Trigger jobBTrigger(JobDetail jobBDetails) {
    JobDataMap jobDataMap = new JobDataMap();
    jobDataMap.put("somedata", UUID.randomUUID().toString());
    return TriggerBuilder.newTrigger()
        .forJob(jobBDetails)
        .withIdentity("TriggerB")
        .withSchedule(CronScheduleBuilder.cronSchedule("0/5 * * ? * * *"))
        .usingJobData(jobDataMap)
        .build();
  }
}

请注意,我们使用带有 Cron 表达式的 CronScheduleBuilder 来触发作业。我们还为 TriggerB 添加了一个示例 JobData 到其中。

就这样。还包括一个 Main 类:

package com.masterspringboot.quartz;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

为了选择作业的存储位置,我们将“jdbc”定义为存储。它将使用 H2 作为选项:

spring.quartz.job-store-type=jdbc 
spring.quartz.properties.org.quartz.scheduler.instanceId=AUTO 
spring.quartz.properties.org.quartz.threadPool.threadCount = 5

如果您更喜欢内存存储,您可以选择:

org.quartz.jobStore.class=org.quartz.simpl.RAMJobStore 
spring.quartz.job-store-type=memory

就这样!运行您的应用程序,您将在控制台中看到来自作业的日志:

Hello from Job One! Hello from Job One! Job data: b5df5e15-de9e-47d4-aa43-28cb0ee8e754 Hello from Job One! Hello from Job One! Hello from Job One!

为 Quartz 使用另一个数据库

使用另一个数据库需要在您的项目中添加正确的依赖项,例如“mysql”。这是一个使用 mysql 的示例 Quartz 配置:

spring.datasource.platform=org.hibernate.dialect.MySQL5Dialect 
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver 
spring.datasource.url=jdbc:mysql://localhost:3306/quartz_demo_db 
spring.datasource.username=root 
spring.datasource.password=admin 
spring.jpa.open-in-view=false 
spring.jpa.show-sql=true 
spring.quartz.job-store-type=jdbc 
spring.quartz.jdbc.initialize-schema=never

本文源代码:https://github.com/fmarchioni/masterspringboot/tree/master/quartz/quartz-example-master

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com