# springboot 使用Quartz实现动态配置定时任务

# 1. 引入依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-quartz</artifactId>
</dependency>
1
2
3
4

# 2. 编写job

public class TestJob implements Job {
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        System.out.println("hello time ="+System.currentTimeMillis());
    }
}
1
2
3
4
5
6

# 3. 使用

# 配置

spring:
  quartz:
    job-store-type: memory
    auto-startup: false
    jdbc:
      initialize-schema: always
    startup-delay: 10
1
2
3
4
5
6
7

# 开启调度

@RestController
@RequestMapping("/job")
public class TestController {
    @Autowired
    private Scheduler scheduler;

    @GetMapping("start")
    public String start(@RequestParam String cron, @RequestParam String jobClass,
                        @RequestParam String jobName, @RequestParam String jobGroup) {
    
        TriggerKey triggerKey = TriggerKey.triggerKey(jobName, jobGroup);
        JobKey jobKey = JobKey.jobKey(jobName, jobGroup);
        CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(cron).withMisfireHandlingInstructionDoNothing();
        CronTrigger trigger = TriggerBuilder.newTrigger().withIdentity(triggerKey).withSchedule(scheduleBuilder).build();
        Class<? extends Job> clazz = null;
        try {
            clazz = (Class<? extends Job>) Class.forName(jobClass);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        JobDetail jobDetail = JobBuilder.newJob(clazz).withIdentity(jobKey).build();
        try {
            scheduler.scheduleJob(jobDetail, trigger);
            return "ok";
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
        return "err";
    }                                   
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

# 关闭

@RestController
@RequestMapping("/job")
public class TestController {
    @Autowired
    private Scheduler scheduler;
    
    @GetMapping("/stop")
    public String stop(@RequestParam String jobName, @RequestParam String jobGroup){
        try {
            scheduler.unscheduleJob(new TriggerKey(jobName,jobGroup));
            return "ok";
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
        return "err";
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17