簡體   English   中英

如何使用默認 spring.datasource 啟動石英集群?

[英]How to start a quartz cluster using default spring.datasource?

我正在嘗試遵循https://docs.spring.io/spring-boot/docs/2.0.x/reference/html/boot-features-quartz.htmlhttps://docs.spring.io/上的手冊spring-boot/docs/current/reference/html/io.html#io.quartz

默認情況下,使用內存中的 JobStore。 但是,如果 DataSource bean 在您的應用程序中可用並且 spring.quartz.job-store-type 屬性配置為 [with "jdbc"],則可以配置基於 JDBC 的存儲。

我包括兩個自動配置器。 我的 pom.xml:

...
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-quartz</artifactId>
            <version>2.7.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>com.zaxxer</groupId>
            <artifactId>HikariCP</artifactId>
...

我的屬性文件包括

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql:...

spring.quartz.job-store-type=jdbc

當我啟動應用程序時,我看到 hikariCP 正在啟動到 spring.datasource url 的連接池......但是石英失敗,嘗試配置org.quartz.simpl.RAMJobStore時出現錯誤......看起來不正確。

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::       (v1.5.22.RELEASE)
2022-06-06 17:52:18.232 [main] INFO  org.eclipse.jetty.server.Server - Started @2217ms
2022-06-06 17:52:18.389 [main] INFO  com.zaxxer.hikari.HikariDataSource - HikariPool-1 - Starting...
2022-06-06 17:52:18.753 [main] INFO  com.zaxxer.hikari.HikariDataSource - HikariPool-1 - Start completed.
2022-06-06 17:52:18.801 [main] INFO  o.s.o.j.LocalContainerEntityManagerFactoryBean - Building JPA container EntityManagerFactory for persistence unit 'default'
...
2022-06-06 17:52:19.492 [main] INFO  o.s.o.j.LocalContainerEntityManagerFactoryBean - Initialized JPA EntityManagerFactory for persistence unit 'default'
2022-06-06 17:52:19.914 [main] WARN  o.s.b.c.e.AnnotationConfigEmbeddedWebApplicationContext - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'QuartzClusteredScheduler' defined in class path resource [spring/quartzScheduler.xml]: Invocation of init method failed; nested exception is org.quartz.SchedulerException: JobStore class 'org.quartz.simpl.RAMJobStore' props could not be configured. [See nested exception: java.lang.NoSuchMethodException: No setter for property 'tablePrefix']
2022-06-06 17:52:19.914 [main] INFO  o.s.o.j.LocalContainerEntityManagerFactoryBean - Closing JPA EntityManagerFactory for persistence unit 'default'
2022-06-06 17:52:19.915 [main] INFO  com.zaxxer.hikari.HikariDataSource - HikariPool-1 - Shutdown initiated...
2022-06-06 17:52:19.915 [main] INFO  com.zaxxer.hikari.pool.HikariPool - HikariPool-1 - Close initiated...
2022-06-06 17:52:19.918 [main] INFO  com.zaxxer.hikari.pool.HikariPool - HikariPool-1 - Closed.
2022-06-06 17:52:19.918 [main] INFO  com.zaxxer.hikari.HikariDataSource - HikariPool-1 - Shutdown completed.

這是否意味着 spring.datasource 和/或 hikariCP 不符合條件,就if a DataSource bean is available in your application

如果我嘗試強制 spring-quartz 使用數據庫:

org.quartz.jobStore.class=org.springframework.scheduling.quartz.LocalDataSourceJobStore

那么錯誤是:

...
2022-06-06 17:56:06.811 [main] WARN  o.s.s.quartz.LocalDataSourceJobStore - Database connection shutdown unsuccessful.
java.sql.SQLException: There is no DataSource named 'null'

您需要將spring-boot-starter-jdbc添加到 pom.xml 中:

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

注意:當您設置org.quartz.jobstore.class時,這意味着 Spring Framework 將不再設置DataSource 在這種情況下,您需要聲明Quartz將使用的DataSource bean:

import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.quartz.QuartzDataSource;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;


@Configuration
@EnableAutoConfiguration
public class QuartzConfiguration {

    @Bean
@QuartzDataSource
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource quartzDataSource() {
    return DataSourceBuilder.create().build();
}
}

以及application.propeties文件中的以下屬性:

using.spring.schedulerFactory=true
spring.quartz.job-store-type=jdbc
spring.quartz.properties.org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.PostgreSQLDelegate

注意:這里我提供了 PostgreSql 數據庫的示例

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2025 STACKOOM.COM