话不多说,直接上代码:
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.8.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>dayup</artifactId> <version>0.0.1-SNAPSHOT</version> <name>demo</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-quartz</artifactId> </dependency> <!-- 连接sqlite的部分 --> <!-- <dependency> <groupId>org.xerial</groupId> <artifactId>sqlite-jdbc</artifactId> <version>3.23.1</version> </dependency> --> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.10</version> </dependency> <dependency> <groupId>c3p0</groupId> <artifactId>c3p0</artifactId> <version>0.9.1.2</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
application.yml
server:
port: 8080
spring:
application:
name: dayup
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: org.h2.Driver
#url: jdbc:h2:mem:dayup 这是内存类型的
url: jdbc:h2:file:C:/code/dayup
username: sa
password: Wh123
quartz:
job-store-type: jdbc
jdbc:
initialize-schema: embedded
properties:
org:
quartz:
scheduler:
instanceName: clusteredScheduler
instanceId: AUTO
jobStore:
dataSource: myDB
class: org.quartz.impl.jdbcjobstore.JobStoreTX
driverDelegateClass: org.quartz.impl.jdbcjobstore.StdJDBCDelegate
tablePrefix: QRTZ_
isClustered: true
clusterCheckinInterval: 10000
useProperties: false
threadPool:
class: org.quartz.simpl.SimpleThreadPool
threadCount: 10
threadPriority: 5
threadsInheritContextClassLoaderOfInitializingThread: true
dataSource:
myDB:
user: sa
password: Wh123
maxConnections: 10
driver: org.h2.Driver
URL: jdbc:h2:file:C:/code/dayup
HiJob.java
package com.example.dayup.quartz; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import org.springframework.scheduling.quartz.QuartzJobBean; public class HiJob extends QuartzJobBean { @Override protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException { System.out.println("测试"); } }
QuartzConfig.java
package com.example.dayup.quartz; import org.quartz.*; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class QuartzConfig { @Bean public JobDetail myJobDetail(){ JobDetail jobDetail = JobBuilder.newJob(HiJob.class) .withIdentity("myJob1","MyJobGroup1") .usingJobData("job_param","job_param1") .storeDurably() .build(); return jobDetail; } @Bean public Trigger myTrigger(){ Trigger trigger = TriggerBuilder.newTrigger() .forJob(myJobDetail()) .withIdentity("myTrigger1","myTriggerGroup1") .usingJobData("job_trigger_param","job_trigger_param1") .startNow() .withSchedule(CronScheduleBuilder.cronSchedule("0 0/1 * * * ?")) .build(); return trigger; } }
然后运行就有了,要注意运行之前先初始化H2数据库,建上Quartz需要的表。建表语句在Quartz官方的GitHub里有,路径在此。