gpt4 book ai didi

java - 如何使用默认的 spring.datasource 启动 quartz 集群?

转载 作者:行者123 更新时间:2023-12-05 04:28:49 35 4
gpt4 key购买 nike

我正在尝试按照 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

By default, an in-memory JobStore is used. However, it is possible to configure a JDBC-based store if a DataSource bean is available in your application and if the spring.quartz.job-store-type property is configured [with "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 失败并尝试配置 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.

如果您的应用程序中有 DataSource bean,这是否意味着 spring.datasource 和/或 hikariCP 不符合条件

如果我试图强制 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'

最佳答案

你需要在pom.xml中添加spring-boot-starter-jdbc:

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

注意:当您设置org.quartz.jobstore.class 时,这意味着Spring Framework 将不再设置DataSource。在这种情况下,您需要声明一个 DataSource bean,它将被 Quartz 使用:

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 数据库的例子

关于java - 如何使用默认的 spring.datasource 启动 quartz 集群?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72522172/

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