前期准备
- 创建一个springboot项目
- 引入相关依赖
演示效果截图
前端调用定时任务
调用完成,定时任务定时执行。
项目结构截图
启动类相关代码
package com.chenhu.quarter;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringbootQuarterApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootQuarterApplication.class,args);
}
}
controller相关代码
package com.chenhu.quarter.controller;
import com.chenhu.quarter.utils.TaskFileScheduler;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* 定时任务
*/
@RestController
@RequestMapping("/demo/quartet")
public class QuarterController {
@Autowired
private TaskFileScheduler taskFileScheduler;
/**
* 1.调用定时任务
* @return
*/
@RequestMapping("callQuarter1")
public String callQuarter1() throws SchedulerException {
taskFileScheduler.scheduleJobs(1001);
return "success";
}
/**
* 2.调用定时任务
* @return
*/
@RequestMapping("callQuarter2")
public String callQuarter2() throws SchedulerException {
taskFileScheduler.scheduleJobs(1002);
return "success";
}
}
定时任务配置类
package com.chenhu.quarter.utils;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.stereotype.Component;
@Component
public class TaskFileScheduler {
@Autowired
private SchedulerFactoryBean schedulerFactoryBean;
private Integer taskId;
public void setTaskId(Integer taskId){
this.taskId=taskId;
}
/**
* 1.业务代码中调用这个方法就可以添加这个定时任务
* @param taskId
* @throws SchedulerException
*/
public void scheduleJobs(Integer taskId) throws SchedulerException {
if(taskId==null){
throw new SchedulerException("定时任务id不可为空");
}
this.taskId=taskId;
Scheduler scheduler = schedulerFactoryBean.getScheduler();
setTaskExecuteDate(scheduler);
}
/**
* 设置定时任务执行时间
* @param scheduler
* @throws SchedulerException
*/
private void setTaskExecuteDate(Scheduler scheduler) throws SchedulerException {
//新建定时任务名,任务组
String taskName = String.format("scheduled_task_name_%s", taskId); //定时任务名称
String taskGroup = String.format("scheduled_task_group_%s", taskId);//定时任务分组
//指定调用类
JobDetail jobDetail = JobBuilder.newJob(TaskJob.class).withIdentity(taskName,taskGroup).build();
//设置任务调用规则
CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule("0/5 * * * * ?");//每隔5秒调用一次
//定时任务设置参数
JobDataMap jobDataMap = new JobDataMap();
jobDataMap.put("taskId",this.taskId);
//创建定时任务
CronTrigger cronTrigger = TriggerBuilder.newTrigger()
.withIdentity(taskName,taskGroup)
.usingJobData(jobDataMap)
.withSchedule(scheduleBuilder)
.build();
//执行定时任务
scheduler.scheduleJob(jobDetail,cronTrigger);
}
}
定时任务调用类
package com.chenhu.quarter.utils;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* 创建定时任务
*/
public class TaskJob implements Job {
/**
* 定时任务执行方法
* @param jobExecutionContext
* @throws JobExecutionException
*/
@Override
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String currentDate = format.format(new Date());
System.out.println("定时任务当前时间:"+currentDate);
JobDataMap mergedJobDataMap = jobExecutionContext.getMergedJobDataMap();
System.out.println("获取taskId:"+mergedJobDataMap.get("taskId"));
}
}
pom文件
这个是我的一个模块,所以父工程依赖是springcloud-demo,重点是应用的依赖 org.springframework.boot。直接复制我的pom.xml 可能会报错 。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>springcloud-demo</artifactId>
<groupId>ch.demo</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>springboot-quarter</artifactId>
<dependencies>
<!--springboot web依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--定时任务依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-quartz</artifactId>
</dependency>
</dependencies>
</project>