The error "There is no DataSource named 'myQz'" occurs when SpirngBoot+Quartz

61 Views Asked by At

I want to configure a separate data source for Quartz, separate from the business data source. So I configured two data sources in the application.yml file and configured the related configuration of quartz. The application.yml file content is as follows:

spring:
  quartz:
    job-store-type: jdbc
    jdbc:
      initialize-schema: always
    properties.org.quartz:
      datasource:
        myQz:
          driver: com.mysql.cj.jdbc.Driver
          URL: jdbc:mysql://localhost:3306/qrtz?useUnicode=true&serverTimezone=UTC&characterEncoding=utf-8&useSSL=false&allowMultiQueries=true&allowPublicKeyRetrieval=true
          username: root
          password: 123456
      scheduler:
        instanceName: clusteredScheduler
        instanceId: AUTO
      jobStore:
        class: org.quartz.impl.jdbcjobstore.JobStoreTX
        dataSource: myQz
        tablePrefix: QRTZ_
        isClustered: true
        clusterCheckinInterval: 10000
        useProperties: false
      threadPool:
        class: org.quartz.simpl.SimpleThreadPool
        threadCount: 10
        threadPriority: 5
        threadsInheritContextClassLoaderOfInitializingThread: true

datasource:
  scheduler:
    driver: com.mysql.cj.jdbc.Driver
    URL: jdbc:mysql://localhost:3306/biz?useUnicode=true&serverTimezone=UTC&characterEncoding=utf-8&useSSL=false&allowMultiQueries=true&allowPublicKeyRetrieval=true
    username: root
    password: 123456
  primary:
    driver: com.mysql.cj.jdbc.Driver
    URL: jdbc:mysql://localhost:3306/qrtz?useUnicode=true&serverTimezone=UTC&characterEncoding=utf-8&useSSL=false&allowMultiQueries=true&allowPublicKeyRetrieval=true
    username: root
    password: 123456

Then the content of the QuartzConfig class is as follows:

@Configuration
public class QuartzConfig {

    @Bean
    public Scheduler scheduler(@Autowired @Qualifier("quartzDataSource") DataSource dataSource) throws Exception {
        SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
        // 设置调度器数据源
        schedulerFactoryBean.setDataSource(dataSource);
        schedulerFactoryBean.afterPropertiesSet();
        return schedulerFactoryBean.getObject();
    }

    @Bean
    @Primary
    @ConfigurationProperties(prefix = "datasource.primary")
    public DataSourceProperties primaryDataSourceProperties() {
        return new DataSourceProperties();
    }

    @Bean
    @ConfigurationProperties(prefix = "datasource.scheduler")
    public DataSourceProperties quartzDataSourceProperties() {
        return new DataSourceProperties();
    }

    @Bean(name = "primaryDataSource")
    public DataSource primaryDataSource() {
        return primaryDataSourceProperties().initializeDataSourceBuilder().type(DruidDataSource.class).build();
    }

    @Bean(name = "quartzDataSource")
    @QuartzDataSource
    public DataSource quartzDataSource() {
        return quartzDataSourceProperties().initializeDataSourceBuilder().type(DruidDataSource.class)
                .build();
    }

}

The error reported during startup:

java.sql.SQLException: There is no DataSource named 'myQz'
    at org.quartz.utils.DBConnectionManager.shutdown(DBConnectionManager.java:135) ~[quartz-2.3.2.jar:na]
    at org.quartz.impl.jdbcjobstore.JobStoreSupport.shutdown(JobStoreSupport.java:746) ~[quartz-2.3.2.jar:na]
    at org.quartz.core.QuartzScheduler.shutdown(QuartzScheduler.java:732) ~[quartz-2.3.2.jar:na]
    at org.quartz.impl.StdScheduler.shutdown(StdScheduler.java:206) ~[quartz-2.3.2.jar:na]
    at org.springframework.scheduling.quartz.SchedulerFactoryBean.destroy(SchedulerFactoryBean.java:848) ~[spring-context-support-5.3.30.jar:5.3.30]
    at org.springframework.beans.factory.support.DisposableBeanAdapter.destroy(DisposableBeanAdapter.java:213) ~[spring-beans-5.3.30.jar:5.3.30]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroyBean(DefaultSingletonBeanRegistry.java:587) ~[spring-beans-5.3.30.jar:5.3.30]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroySingleton(DefaultSingletonBeanRegistry.java:559) ~[spring-beans-5.3.30.jar:5.3.30]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.destroySingleton(DefaultListableBeanFactory.java:1163) ~[spring-beans-5.3.30.jar:5.3.30]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroySingletons(DefaultSingletonBeanRegistry.java:520) ~[spring-beans-5.3.30.jar:5.3.30]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.destroySingletons(DefaultListableBeanFactory.java:1156) ~[spring-beans-5.3.30.jar:5.3.30]
    at org.springframework.context.support.AbstractApplicationContext.destroyBeans(AbstractApplicationContext.java:1112) ~[spring-context-5.3.30.jar:5.3.30]
    at org.springframework.context.support.AbstractApplicationContext.doClose(AbstractApplicationContext.java:1078) ~[spring-context-5.3.30.jar:5.3.30]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.doClose(ServletWebServerApplicationContext.java:174) ~[spring-boot-2.7.16.jar:2.7.16]
    at org.springframework.context.support.AbstractApplicationContext.close(AbstractApplicationContext.java:1024) ~[spring-context-5.3.30.jar:5.3.30]
    at org.springframework.boot.SpringApplication.handleRunFailure(SpringApplication.java:787) ~[spring-boot-2.7.16.jar:2.7.16]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:317) ~[spring-boot-2.7.16.jar:2.7.16]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1303) ~[spring-boot-2.7.16.jar:2.7.16]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1292) ~[spring-boot-2.7.16.jar:2.7.16]
    at com.example.quartz.QuartzApplication.main(QuartzApplication.java:10) ~[classes/:na]

After consulting various solutions on the Internet, the most likely answer is to directly delete "org.quartz.impl.jdbcjobstore.JobStoreTX" in the configuration. However, after I delete the configuration, an error will be reported at startup:

2023-10-15 15:42:30.660  WARN 20232 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'quartzScheduler' defined in class path resource [org/springframework/boot/autoconfigure/quartz/QuartzAutoConfiguration.class]: 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 'useProperties']

My environment is Windows11 + JDK17 + SpringBoot2.7.16. Is there any solution?

0

There are 0 best solutions below