gabriel
gabriel

Reputation: 357

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

I am trying to follow the manual on https://docs.spring.io/spring-boot/docs/2.0.x/reference/html/boot-features-quartz.html and 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"].

i'm including both autoconfigurators. my 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>
...

my properties file include

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

spring.quartz.job-store-type=jdbc

When I start the application I see hikariCP starting the connection pool to the spring.datasource url... but quartz fail with errors trying to configure org.quartz.simpl.RAMJobStore... that doesn't look right.

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: 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.

Does it mean spring.datasource and/or hikariCP doesn't qualify as if a DataSource bean is available in your application?

If I try to force spring-quartz to use a database:

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

then the error is:

...
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'

Upvotes: 4

Views: 8439

Answers (1)

Nemanja
Nemanja

Reputation: 3679

You need to add spring-boot-starter-jdbc to pom.xml:

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

NOTE: When you set org.quartz.jobstore.class this means that Spring Framework will no longer set the DataSource. in that case you need to declare a DataSource bean which will be used by 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();
}
}

And below properties in application.propeties file:

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

Note : Here I provided example with PostgreSql database

Upvotes: 2

Related Questions