2021年8月16日 | Leave a comment 问题: 解决: pom.xml 引入 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-quartz</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> 12345678 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-quartz</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> 修改 application.yml spring: datasource: url: jdbc:mysql://127.0.0.1:3306/quartz1?characterEncoding=utf8&useUnicode=true&autoReconnectForPools=true&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=Asia%2fShanghai&allowPublicKeyRetrieval=true username: root password: 123456 quartz: job-store-type: jdbc jdbc.initialize-schema: never properties: org.quartz: scheduler: instanceId: AUTO instanceName: quartzscheduler1 jobStore: isClustered: true 123456789101112131415 spring: datasource: url: jdbc:mysql://127.0.0.1:3306/quartz1?characterEncoding=utf8&useUnicode=true&autoReconnectForPools=true&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=Asia%2fShanghai&allowPublicKeyRetrieval=true username: root password: 123456 quartz: job-store-type: jdbc jdbc.initialize-schema: never properties: org.quartz: scheduler: instanceId: AUTO instanceName: quartzscheduler1 jobStore: isClustered: true 创建表 sql 脚本 在 https://github.com/quartz-scheduler/quartz/blob/master/quartz-core/src/main/resources/org/quartz/impl/jdbcjobstore/tables_mysql_innodb.sql 。 在 quartz 官网下载的 压缩文件 docs/dbTables 目录下 。(参考 http://www.quartz-scheduler.org/documentation/quartz-2.3.0/tutorials/tutorial-lesson-09.html JDBCJobStore ) 需要调用一次 scheduleJob private final static class MyJob implements Job { @Override public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException { System.out.println("myjob execute.... " + Thread.currentThread().getName() + " date:" + LocalDateTime.now()); } } @Bean public CommandLineRunner ss(String[] args) throws SchedulerException { return (a) -> { //需要执行一次 // var job = newJob(MyJob.class).build(); // Trigger trigger = newTrigger().startNow().withSchedule(simpleSchedule().withIntervalInSeconds(5).repeatForever()).build(); // scheduler.scheduleJob(job, trigger); }; } 1234567891011121314151617 private final static class MyJob implements Job { @Override public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException { System.out.println("myjob execute.... " + Thread.currentThread().getName() + " date:" + LocalDateTime.now()); } } @Bean public CommandLineRunner ss(String[] args) throws SchedulerException { return (a) -> {//需要执行一次// var job = newJob(MyJob.class).build();// Trigger trigger = newTrigger().startNow().withSchedule(simpleSchedule().withIntervalInSeconds(5).repeatForever()).build();// scheduler.scheduleJob(job, trigger); }; } 完整代码参考: https://github.com/giant35/demo/tree/master/quartz1 参考: http://www.quartz-scheduler.org/documentation/quartz-2.3.0/quick-start.html https://jeecg.blog.csdn.net/article/details/105708815