提问者:小点点

如何使用默认的 spring.datasource 启动石英集群?


我正在努力按照手册https://docs.spring.io/spring-boot/docs/2.0.x/reference/html/boot-features-quartz.html和https://docs.spring.io/spring-boot/docs/current/reference/html/io.html#io.quartz

默认情况下,使用内存中的JobStore。但是,如果您的应用程序中有可用的DataSource bean,并且配置了spring.quartz.job-store-type属性,则可以配置基于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的连接池...但是quartz在尝试配置< code > org . quartz . simple . 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 不符合您的应用程序中可用的数据源 bean 的条件?

如果我尝试强制Spring石英使用数据库:

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'

共1个答案

匿名用户

你需要将 spring-boot-starter-jdbc 添加到 pom.xml:

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

注意:当您设置< code > org . quartz . job store . class 时,这意味着Spring Framework将不再设置< code>DataSource。在这种情况下,您需要声明一个将由< code>Quartz使用的< code>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.properties文件中的以下属性:

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

注意:这里我提供了PostgreSql数据库的示例