定时任务主要使用它的三大组件。
使用流程:
void execute(JobExecutionContext context) 方法中。
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
<version>2.5.1version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<version>2.5.1version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-quartzartifactId>
<version>2.5.1version>
dependency>
dependencies>
@SpringBootApplication
public class QuartzApplication {
public static void main(String[] args) {
SpringApplication.run(QuartzApplication.class, args);
System.out.println("QuartzApplication启动成功");
}
}
/**
* DemoJob
* 定时任务
*
* @author : he zhe
* @date : 2022-07-29 14:26
**/
public class DemoJob implements Job {
@Override
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
//获取JobDetail中传递的参数
String url = (String) jobExecutionContext.getJobDetail().getJobDataMap().get("url");
//获取当前时间
Date date = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//打印信息
System.out.println("当前时间:" + dateFormat.format(date)+" 用户:"+new Random().nextInt(100)+" 访问了"+url);
System.out.println("----------------------------------------");
}
}
/**
* JobUtils
* 任务调度工具类
*
* @author : he zhe
* @date : 2022-07-29 14:49
**/
public class JobUtils {
private static SchedulerFactory schedulerFactory;
private static void setSchedulerFactory(){
schedulerFactory = new StdSchedulerFactory();
}
public static void open(String className, String jobId){
if (schedulerFactory == null) {
setSchedulerFactory();
}
try {
// 1、创建调度器Scheduler
Scheduler scheduler = schedulerFactory.getScheduler();
// 2、创建JobDetail实例,并与Job类绑定(Job执行内容)
className = "com.study.job."+className;
Class forName = Class.forName(className);
JobDetail jobDetail = JobBuilder.newJob(forName)
.withIdentity(jobId)
.usingJobData("url","https://blog.csdn.net/qq_43466788")
.build();
// 3、构建Trigger实例,每隔1s执行一次
Trigger trigger = TriggerBuilder.newTrigger().withIdentity("trigger1", "triggerGroup1")
.startNow()//立即生效
.withSchedule(SimpleScheduleBuilder.simpleSchedule()
.withIntervalInSeconds(1)//每隔1s执行一次
.repeatForever()).build();//一直执行
//4、执行
scheduler.scheduleJob(jobDetail, trigger);
System.out.println("--------scheduler start ! ------------");
scheduler.start();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void close(String jobId) {
try {
// 任务全部使用同一个调度器
Scheduler scheduler = null;
// 获取调度器
scheduler = schedulerFactory.getScheduler();
JobKey jobKey = JobKey.jobKey(jobId);
// 添加到调度器
scheduler.deleteJob(jobKey);
System.out.println("--------scheduler shutdown ! ------------");
} catch (Exception e) {
e.printStackTrace();
}
}
}
@RestController
public class DemoJobController {
@RequestMapping(value = "/open", method = RequestMethod.POST)
public String test(String className) {
//开启定时任务
JobUtils.open(className,"1");
return "定时任务启动成功";
}
@RequestMapping(value = "/close", method = RequestMethod.POST)
public String test2(String jobId) {
//开启定时任务
JobUtils.close(jobId);
return "定时任务关闭成功";
}
}



