一、添加依赖:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.37</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-quartz</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
注:此处示例是将任务job存储在mysql数据库中,所以需要使用到jdbc和mysql的相关依赖;
二、数据库插入quartz的表结构:
可以去官网下载对应使用的quartz包,解压后docs/db Tables 目录下;
如下图所示表:
三、编辑application.yml配置文件
server:
port: 8083
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql:///quarzt
username: root
password: 123456
注:就是要操作的数据库的配置信息
四、编辑quartz的配置信息类
@Component
public class QuartzConfig {
private static Logger logger = LoggerFactory.getLogger(QuartzConfig.class);
@Resource
private DataSource dataSource;
@Resource
private PlatformTransactionManager transactionManager;
@Bean(name = "schedulerFactor")
public SchedulerFactoryBean getSchedulerFactor(){
logger.info("***start init scheduler factory***");
SchedulerFactoryBean factoryBean = new SchedulerFactoryBean();
//设置数据源
factoryBean.setDataSource(dataSource);
factoryBean.setTransactionManager(transactionManager);
//设置覆盖已经存在的任务
factoryBean.setOverwriteExistingJobs(true);
//设置30秒开始初始化调度器
factoryBean.setStartupDelay(30);
//设置调度器自动运行
factoryBean.setAutoStartup(true);
return factoryBean;
}
}
五、定义quartz需要使用到的接口:
package com.zgl.quartzstudy.quartz;
import org.quartz.SchedulerException;
import java.util.Map;
/**
* ProjectName:quartz-study
* Date:2019/11/20
* @Author: Guolin.Zhan
* Desc:
*/
public interface QuartzService {
/**
* 添加调度
* @param jobClassName
* @param groupName
* @param jobName
* @param dataMap
* @return
*/
boolean addScheduler(String jobClassName, String groupName, String jobName, Map<String,Object> dataMap) throws ClassNotFoundException, IllegalAccessException, InstantiationException, SchedulerException;
/**
* 删除定时任务job
* @param jobName job名称
* @param groupName job组名称
* @return
* @throws SchedulerException
*/
boolean deleteScheduler(String jobName, String groupName) throws SchedulerException;
/**
* 暂停任务job
* @param jobName
* @param groupName
* @return
*/
boolean pauseJob(String jobName,String groupName) throws SchedulerException;
/**
* 暂停所有任务job
* @return
*/
boolean pauseAll() throws SchedulerException;
/**
* 恢复任务job
* @param jobName
* @param groupName
* @return
*/
boolean resumeJob(String jobName,String groupName) throws SchedulerException;
/**
* 恢复所有任务
* @return
*/
boolean resumeAll() throws SchedulerException;
/**
* 暂停调度器
* @param jobClassName job权限定类名称
* @param groupName job组名称
* @return
*/
boolean pauseTrigger(String jobClassName, String groupName) throws SchedulerException;
/**
* 恢复调度器
* @param jobClassName
* @param groupName
* @return
*/
boolean resumeTrigger(String jobClassName, String groupName) throws SchedulerException;
}
六、实现定义quartz接口类:
package com.zgl.quartzstudy.quartz;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Map;
import static org.quartz.JobBuilder.newJob;
import static org.quartz.JobKey.jobKey;
import static org.quartz.SimpleScheduleBuilder.simpleSchedule;
import static org.quartz.TriggerBuilder.newTrigger;
import static org.quartz.TriggerKey.triggerKey;
/**
* ProjectName:quartz-study
* Date:2019/11/20
* @Author: Guolin.Zhan
* Desc:
*/
@Service
public class QuartzServiceImpl implements QuartzService{
@Resource
private Scheduler scheduler;
@Override
public boolean addScheduler(String jobClassName, String groupName, String jobName, Map<String, Object> dataMap) throws ClassNotFoundException, IllegalAccessException, InstantiationException, SchedulerException {
//注:jobClassName是类的全限定名称
Job job = (Job) Class.forName(jobClassName).newInstance();
JobDetail jobDetail = newJob(job.getClass())
.withIdentity(jobName, groupName)
.build();
jobDetail.getJobDataMap().putAll(dataMap);
SimpleTrigger trigger = newTrigger()
.withIdentity(jobClassName, groupName)
.withSchedule(
simpleSchedule()
.withIntervalInMilliseconds(100)
.repeatForever()
).build();
scheduler.scheduleJob(jobDetail,trigger);
return true;
}
@Override
public boolean deleteScheduler(String jobName, String groupName) throws SchedulerException {
JobKey jobKey = jobKey(jobName, groupName);
scheduler.pauseJob(jobKey);
scheduler.deleteJob(jobKey);
return true;
}
@Override
public boolean pauseJob(String jobName, String groupName) throws SchedulerException {
JobKey jobKey = jobKey(jobName, groupName);
scheduler.pauseJob(jobKey);
return true;
}
@Override
public boolean pauseAll() throws SchedulerException {
scheduler.pauseAll();
return true;
}
@Override
public boolean resumeJob(String jobName, String groupName) throws SchedulerException {
JobKey jobKey = jobKey(jobName, groupName);
scheduler.resumeJob(jobKey);
return true;
}
@Override
public boolean resumeAll() throws SchedulerException {
scheduler.resumeAll();
return true;
}
@Override
public boolean pauseTrigger(String jobClassName, String groupName) throws SchedulerException {
TriggerKey triggerKey = triggerKey(jobClassName, groupName);
scheduler.pauseTrigger(triggerKey);
return true;
}
@Override
public boolean resumeTrigger(String jobClassName, String groupName) throws SchedulerException {
TriggerKey triggerKey = triggerKey(jobClassName, groupName);
scheduler.resumeTrigger(triggerKey);
return true;
}
}
七、定义job类:
package com.zgl.quartzstudy.job;
import org.quartz.*;
import org.springframework.stereotype.Component;
import java.io.Serializable;
/**
* ProjectName:quartz-study
* Date:2019/11/19
* @Author: Guolin.Zhan
* Desc:
*/
@PersistJobDataAfterExecution
@DisallowConcurrentExecution
@Component
public class MyJob implements Job {
public MyJob() {
}
@Override
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
//此处编辑需要实现的业务逻辑
System.out.println("***********被调度了***********");
}
}
八、测试类:
package com.zgl.quartzstudy;
import com.zgl.quartzstudy.quartz.QuartzService;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Map;
/**
* ProjectName:quartz-study
* Date:2019/11/20
* @Author: Guolin.Zhan
* Desc:
*/
@RestController
@RequestMapping("/test-scheduler")
public class QuartzTest {
@Autowired
private QuartzService quartzService;
@GetMapping("/add-scheduler")
public boolean addScheduler(@RequestParam String jobClassName,
@RequestParam String groupName,
@RequestParam String jobName,
@RequestParam Map<String,Object> dataMap) throws Exception {
return quartzService.addScheduler(jobClassName,groupName,jobName,dataMap);
}
@DeleteMapping("/delete-scheduler")
public boolean deleteScheduler(@RequestParam String jobName, @RequestParam String groupName) throws SchedulerException {
return quartzService.deleteScheduler(jobName,groupName);
}
@GetMapping("/pause-job")
public boolean pauseJob(@RequestParam String jobName, @RequestParam String groupName) throws SchedulerException {
return quartzService.pauseJob(jobName,groupName);
}
@GetMapping("/pause-all")
public boolean pauseAll() throws SchedulerException {
return quartzService.pauseAll();
}
@GetMapping("/resume-job")
public boolean resumeJob(@RequestParam String jobName, @RequestParam String groupName) throws SchedulerException {
return quartzService.resumeJob(jobName,groupName);
}
@GetMapping("/resume-all")
public boolean resumeAll() throws SchedulerException {
return quartzService.resumeAll();
}
@GetMapping("/pause-trigger")
public boolean pauseTrigger(@RequestParam String jobClassName, @RequestParam String groupName) throws SchedulerException {
return quartzService.pauseTrigger(jobClassName,groupName);
}
@GetMapping("/resume-trigger")
public boolean resumeTrigger(@RequestParam String jobClassName, @RequestParam String groupName) throws SchedulerException {
return quartzService.resumeTrigger(jobClassName,groupName);
}
}