如何正确配置 Spring 引导项目以使用 Quartz 和 MySQL?

How to configure a Spring Boot project correctly to work with Quartz and MySQL?

我做的是:

  1. 使用 Web、JPA、MySQL 和 Quartz 作为依赖项创建了 Spring Boot (v2.1.8) Gradle 项目。
  2. 然后在 application.properties 文件
  3. 上添加了以下属性
spring.application.name=QuartzTestWithMySQL
server.port=8081

## Data source
#docker run -d --rm -p 3306:3306 --name=mysql-docker -e MYSQL_ROOT_PASSWORD=root -e MYSQL_USER=test -e MYSQL_PASSWORD=test -e MYSQL_DATABASE=testDB mysql:latest
spring.datasource.name = myDS
spring.datasource.url=jdbc:mysql://localhost:3306/testDB
spring.datasource.username=test
spring.datasource.password=test

## Hibernate Properties
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true
# The SQL dialect makes Hibernate generate better SQL for the chosen database
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
# Hibernate ddl auto (create, create-drop, validate, update)
spring.jpa.hibernate.ddl-auto = create-drop

## QuartzProperties
#spring.quartz.job-store-type=memory
spring.quartz.job-store-type=jdbc
spring.quartz.properties.org.quartz.threadPool.threadCount=5
#spring.quartz.jdbc.initialize-schema=always

#spring.quartz.properties.org.quartz.scheduler.instanceName = MyScheduler
#spring.quartz.properties.org.quartz.threadPool.threadCount = 3
#spring.quartz.properties.org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore

spring.quartz.properties.org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX
#spring.quartz.properties.org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.StdJDBCDelegate
spring.quartz.properties.org.quartz.jobStore.tablePrefix = QRTZ_
spring.quartz.properties.org.quartz.jobStore.dataSource = myDS

build.gradle 文件是:

plugins {
    id 'org.springframework.boot' version '2.1.8.RELEASE'
    id 'io.spring.dependency-management' version '1.0.8.RELEASE'
    id 'java'
}

group = 'com.onssoftware'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'org.springframework.boot:spring-boot-starter-quartz'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    runtimeOnly 'mysql:mysql-connector-java'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

  1. 然后创建了以下作业 class:
public class Bismillah implements Job {

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        System.out.println("Assalamu Alaikum");
    }
}
  1. 然后像这样创建了一个控制器:
@RestController
public class SchedulerTestController {

    @Autowired
    private Scheduler scheduler;

    @GetMapping("/scheduler")
    public void  testScheduler() throws SchedulerException {

        // Grab the Scheduler instance from the Factory
        //Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();

        // and start it off
        //scheduler.start();

        JobDetail job = JobBuilder.newJob(Bismillah.class)
                .withIdentity("job1", "group1")
                .storeDurably()
                .withDescription("Bismillah hir rahmanir rahim")
                .build();

        CronTrigger cronTrigger = TriggerBuilder.newTrigger()
                .withIdentity("trigger1", "group1")
                .withSchedule(CronScheduleBuilder.cronSchedule("* * 11 * * ?"))
                .forJob("job1", "group1")
                .build();

        scheduler.scheduleJob(job, cronTrigger);
    }
}
  1. 然后我从浏览器调用 url:
http://localhost:8081/scheduler

安排作业。

我得到的是:

2019-09-18 12:55:31.445  INFO 10507 --- [           main] c.o.Q.QuartzTestWithMySqlApplication     : No active profile set, falling back to default profiles: default
2019-09-18 12:55:32.326  INFO 10507 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data repositories in DEFAULT mode.
2019-09-18 12:55:32.359  INFO 10507 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 18ms. Found 0 repository interfaces.
2019-09-18 12:55:32.810  INFO 10507 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$95a7a2] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-09-18 12:55:33.198  INFO 10507 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8081 (http)
2019-09-18 12:55:33.235  INFO 10507 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2019-09-18 12:55:33.235  INFO 10507 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.24]
2019-09-18 12:55:33.344  INFO 10507 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2019-09-18 12:55:33.344  INFO 10507 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1841 ms
2019-09-18 12:55:33.545  INFO 10507 --- [           main] com.zaxxer.hikari.HikariDataSource       : myDS - Starting...
2019-09-18 12:55:34.179  INFO 10507 --- [           main] com.zaxxer.hikari.HikariDataSource       : myDS - Start completed.
2019-09-18 12:55:34.269  INFO 10507 --- [           main] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [
    name: default
    ...]
2019-09-18 12:55:34.344  INFO 10507 --- [           main] org.hibernate.Version                    : HHH000412: Hibernate Core {5.3.11.Final}
2019-09-18 12:55:34.345  INFO 10507 --- [           main] org.hibernate.cfg.Environment            : HHH000206: hibernate.properties not found
2019-09-18 12:55:34.502  INFO 10507 --- [           main] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.0.4.Final}
2019-09-18 12:55:34.682  INFO 10507 --- [           main] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
2019-09-18 12:55:34.763  INFO 10507 --- [           main] o.h.e.j.e.i.LobCreatorBuilderImpl        : HHH000421: Disabling contextual LOB creation as hibernate.jdbc.lob.non_contextual_creation is true
2019-09-18 12:55:35.033  INFO 10507 --- [           main] o.h.t.schema.internal.SchemaCreatorImpl  : HHH000476: Executing import script 'org.hibernate.tool.schema.internal.exec.ScriptSourceInputNonExistentImpl@2007435e'
2019-09-18 12:55:35.039  INFO 10507 --- [           main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2019-09-18 12:55:35.596  INFO 10507 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2019-09-18 12:55:35.764  WARN 10507 --- [           main] aWebConfiguration$JpaWebMvcConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2019-09-18 12:55:36.509  WARN 10507 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.autoconfigure.quartz.QuartzAutoConfiguration$JdbcStoreTypeConfiguration$QuartzSchedulerDependencyConfiguration': Unexpected exception during bean creation; nested exception is java.lang.ArrayStoreException: sun.reflect.annotation.TypeNotPresentExceptionProxy
2019-09-18 12:55:36.518  INFO 10507 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Shutting down ExecutorService 'applicationTaskExecutor'
2019-09-18 12:55:36.520  INFO 10507 --- [           main] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
2019-09-18 12:55:36.522  INFO 10507 --- [           main] .SchemaDropperImpl$DelayedDropActionImpl : HHH000477: Starting delayed evictData of schema as part of SessionFactory shut-down'
2019-09-18 12:55:36.541  INFO 10507 --- [           main] com.zaxxer.hikari.HikariDataSource       : myDS - Shutdown initiated...
2019-09-18 12:55:36.553  INFO 10507 --- [           main] com.zaxxer.hikari.HikariDataSource       : myDS - Shutdown completed.
2019-09-18 12:55:36.561  INFO 10507 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2019-09-18 12:55:36.592  INFO 10507 --- [           main] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2019-09-18 12:55:36.613 ERROR 10507 --- [           main] o.s.boot.SpringApplication               : Application run failed

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.autoconfigure.quartz.QuartzAutoConfiguration$JdbcStoreTypeConfiguration$QuartzSchedulerDependencyConfiguration': Unexpected exception during bean creation; nested exception is java.lang.ArrayStoreException: sun.reflect.annotation.TypeNotPresentExceptionProxy
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:528) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean[=17=](AbstractBeanFactory.java:320) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:845) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:877) ~[spring-context-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549) ~[spring-context-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141) ~[spring-boot-2.1.8.RELEASE.jar:2.1.8.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:744) [spring-boot-2.1.8.RELEASE.jar:2.1.8.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:391) [spring-boot-2.1.8.RELEASE.jar:2.1.8.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:312) [spring-boot-2.1.8.RELEASE.jar:2.1.8.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215) [spring-boot-2.1.8.RELEASE.jar:2.1.8.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1204) [spring-boot-2.1.8.RELEASE.jar:2.1.8.RELEASE]
    at com.onssoftware.QuartzTestWithMySQL.QuartzTestWithMySqlApplication.main(QuartzTestWithMySqlApplication.java:10) [classes/:na]
Caused by: java.lang.ArrayStoreException: sun.reflect.annotation.TypeNotPresentExceptionProxy
    at sun.reflect.annotation.AnnotationParser.parseClassArray(AnnotationParser.java:724) ~[na:1.8.0_201]
    at sun.reflect.annotation.AnnotationParser.parseArray(AnnotationParser.java:531) ~[na:1.8.0_201]
    at sun.reflect.annotation.AnnotationParser.parseMemberValue(AnnotationParser.java:355) ~[na:1.8.0_201]
    at sun.reflect.annotation.AnnotationParser.parseAnnotation2(AnnotationParser.java:286) ~[na:1.8.0_201]
    at sun.reflect.annotation.AnnotationParser.parseAnnotations2(AnnotationParser.java:120) ~[na:1.8.0_201]
    at sun.reflect.annotation.AnnotationParser.parseAnnotations(AnnotationParser.java:72) ~[na:1.8.0_201]
    at java.lang.reflect.Executable.declaredAnnotations(Executable.java:599) ~[na:1.8.0_201]
    at java.lang.reflect.Executable.declaredAnnotations(Executable.java:597) ~[na:1.8.0_201]
    at java.lang.reflect.Executable.getAnnotation(Executable.java:570) ~[na:1.8.0_201]
    at java.lang.reflect.Method.getAnnotation(Method.java:622) ~[na:1.8.0_201]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.lambda$determineCandidateConstructors[=17=](AutowiredAnnotationBeanPostProcessor.java:249) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:410) ~[spring-core-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:417) ~[spring-core-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:389) ~[spring-core-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.determineCandidateConstructors(AutowiredAnnotationBeanPostProcessor.java:248) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.determineConstructorsFromBeanPostProcessors(AbstractAutowireCapableBeanFactory.java:1269) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1184) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    ... 14 common frames omitted


Process finished with exit code 1

提前感谢您的任何建议!

我认为您的错误与以下问题有关 /spring-boot/issues/18153

您可以尝试将 spring-boot 降级到 2.1。7.RELEASE:

plugins {
    id 'org.springframework.boot' version '2.1.7.RELEASE'
...
}