spring boot整合quartz框架,存储到mysql数据库
1、利用intellij idea创建java maven项目
2、添加如下重要依赖spring-boot-starter-孥恶膈茯quartz<!-- https://mvnrepository.co罪焐芡拂m/artifact/org.springframework.boot/spring-boot-starter-quartz --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-quartz</artifactId> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-jdbc --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.38</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.10</version> </dependency>
3、整个pom配置文件如下图所示
4、在application.yml配置文件,添加数据源配置,quartz相关配置,如下图所示logging: level: root: debugspring: datasource: druid: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/quartz?serverTimezone=Hongkong&characterEncoding=utf8&useUnicode=true&useSSL=false&allowMultiQueries=true username: root password: 123456 initialSize: 5 minIdle: 50 maxActive: 500 maxWait: 60000 timeBetweenEvictionRunsMillis: 60000 minEvictableIdleTimeMillis: 300000 validationQuery: SELECT 1 testWhileIdle: true testOnBorrow: false testOnReturn: false poolPreparedStatements: true maxPoolPreparedStatementPerConnectionSize: 20 filters: stat,wall,log4j connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000 useGlobalDataSourceStat: true quartz: job-store-type: jdbc
5、打开源码,找到org/quartz/impl/jdbcjobstore/tables_mysql_innodb.sql,复制sql语句,初始化数据库表
6、如果要在job实现类中,注入spring的其他单例,可以使用@Component注解。然后就可以根据quartz的文档开发,JobBuilder.newJob(jobClass),底层会找到刚才主导到spring容器的job类