yantian yue
2023-10-18 f4a3430eb6b6800d4ef7330293dd8fb834eee196
提交 | 用户 | 时间
1ac2bc 1 package cn.stylefeng.guns.core.schedue.quartz;
2
3 import org.quartz.*;
4 import org.springframework.beans.factory.annotation.Autowired;
5 import org.springframework.boot.CommandLineRunner;
6
7 /**
8  * quartz的启动示例
9  *
10  * @author fengshuonan
11  * @Date 2019/2/24 16:55
12  */
13 public class StartQuartzExample implements CommandLineRunner {
14
15     @Autowired
16     private Scheduler scheduler;
17
18     @Override
19     public void run(String... args) throws Exception {
20
21         // 启动调度器
22         scheduler.start();
23
24         //构建job信息
25         JobDetail jobDetail = JobBuilder.newJob(SampleQuartzJob.class).withIdentity(SampleQuartzJob.class.getName(), "aaa").build();
26
27         //表达式调度构建器(即任务执行的时间)
28         CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule("*/5 * * * * ?");
29
30         //按新的cronExpression表达式构建一个新的trigger
31         CronTrigger trigger = TriggerBuilder.newTrigger().withIdentity(SampleQuartzJob.class.getName(), "aaa")
32                 .withSchedule(scheduleBuilder).build();
33
34         try {
35             scheduler.scheduleJob(jobDetail, trigger);
36
37         } catch (SchedulerException e) {
38             System.out.println("创建定时任务失败" + e);
39             throw new Exception("创建定时任务失败");
40         }
41     }
42 }