1. 引入springboot和quartz的maven依赖,因为需要将任务信息持久化到数据库MySQL,故引入mysql驱动
<!-- quartz 排除c3p0数据源连接池-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-quartz</artifactId>
<exclusions>
<exclusion>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- 测试添加调度任务用到 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- MySQL连接驱动 -->
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>8.0.33</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
2.配置数据源和quartz基本配置
spring.datasource.url=jdbc:mysql://localhost:3306/quartz-db
spring.datasource.username=root
spring.datasource.password=db123321
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.hikari.maximum-pool-size=10
## quartz配置信息,具体配置信息参考官方配置说明,文档地址:https://github.com/quartz-scheduler/quartz/blob/main/docs/configuration.adoc
spring.quartz.job-store-type=jdbc
spring.quartz.properties.org.quartz.scheduler.instanceName=MyScheduler
spring.quartz.properties.org.quartz.scheduler.instanceId=AUTO
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.useProperties=false
spring.quartz.properties.org.quartz.jobStore.tablePrefix=QRTZ_
#如果调度器使用集群模式,需要配置数据源,如果设置为false,可以不设置下面的自定义数据源,使用spring托管的数据源
spring.quartz.properties.org.quartz.jobStore.isClustered=false
#spring.quartz.properties.org.quartz.dataSource.myDS.driver=com.mysql.cj.jdbc.Driver
#spring.quartz.properties.org.quartz.dataSource.myDS.URL=jdbc:mysql://localhost:3306/quartz-db
#spring.quartz.properties.org.quartz.dataSource.myDS.user=root
#spring.quartz.properties.org.quartz.dataSource.myDS.password=db123321
#spring.quartz.properties.org.quartz.dataSource.myDS.maxConnections=10
##替换默认的c3p0数据源连接池,注意:部分版本的可能为Hikaricp,有问题两个都尝试下
#spring.quartz.properties.org.quartz.dataSource.myDS.provider=hikaricp
3. 编写任务类和测试接口
Job任务类:
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
/**
* @author dzt
*/
public class TestJob implements Job {
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
System.out.println("来活了,我是勤劳的牛马.....");
}
}
创建一个服务类,构建JobDetail和Triggger,并进行绑定:
import com.bjb.quartz.job.WechatMessageSendJob;
import org.quartz.CronScheduleBuilder;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.TimeZone;
/**
* @author dzt
*/
@Service
public class QuartzService {
@Autowired
private Scheduler scheduler;
private static final String DEFAULT_JOB_GROUP = "default_job_group";
private static final String DEFAULT_TRIGGER_GROUP = "default_trigger_group";
public void bindJobDetailAndTrigger() throws SchedulerException {
JobDetail jobDetail = JobBuilder.newJob(TestJob.class)
//任务描述
.withDescription("测试任务")
.withIdentity("job1", DEFAULT_JOB_GROUP)
.storeDurably()
.build();
Trigger trigger = TriggerBuilder.newTrigger()
.withIdentity("trigger1", DEFAULT_TRIGGER_GROUP)
.withSchedule(
CronScheduleBuilder.cronSchedule("0 0 17 * * ? 2024")
.inTimeZone(TimeZone.getTimeZone("GMT+8"))
)
.build();
scheduler.scheduleJob(jobDetail, trigger);
scheduler.start();
}
}
测试接口:
```java
import com.bjb.quartz.service.QuartzService;
import org.quartz.SchedulerException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
/**
* @author dzt
*/
@RestController
public class TestQuartzController {
@Resource
QuartzService quartzService;
@GetMapping(value = "/test")
public String test() throws SchedulerException {
quartzService.bindJobDetailAndTrigger();
return "success";
}
}