一、新建web工程,引入quatrz的包等
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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.cobra</groupId>
<artifactId>quartzlearn</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>quartzlearn</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
二、coding:
目录结构如下:
1、在job包下,新建MyJob类implements实现Job接口,在execute方法里面写上具体的业务:
package com.cobra.quartzlearn.job;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* @Author: Baron
* @Description:
* @Date: Created in 2019/3/13 23:17
*/
public class MyJob implements Job {
@Override
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
//具体业务
Date date = new Date();
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("现在的时间是:"+sf.format(date));
System.out.println("Hello Quartz");
}
}
2、在controller包下新建QuartzController类:
package com.cobra.quartzlearn.controller;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @Author: Baron
* @Description:
* @Date: Created in 2019/3/14 0:06
*/
@RestController
@RequestMapping("/schedule")
public class QuartzController {
/**
* 利用SimpleTrigger设定运行机制
* @return
* @throws ClassNotFoundException
* @throws SchedulerException
*/
@GetMapping("/bySimple")
public String executeOnTime() throws ClassNotFoundException, SchedulerException {
JobDetail jobDetail = JobBuilder.newJob(getMyClass()).withIdentity("myFirstJob").build();
SimpleTrigger simpleTrigger = TriggerBuilder.newTrigger().withIdentity("myTrigger").startNow().withSchedule(
SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(5).repeatForever()).build();
StdSchedulerFactory factory = new StdSchedulerFactory();
Scheduler scheduler = factory.getScheduler();
scheduler.start();
scheduler.scheduleJob(jobDetail, simpleTrigger);
return "执行bySimple";
}
/**
* 利用CronTrigger设定运行机制
* @return
* @throws SchedulerException
* @throws ClassNotFoundException
*/
@GetMapping("/byCron")
public String execute() throws SchedulerException, ClassNotFoundException {
JobDetail jobDetail = JobBuilder.newJob(getMyClass()).withIdentity("mySecondJob").build();
CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity("cronTrigger").withSchedule(CronScheduleBuilder.cronSchedule("0 0/1 * * * ?")).build();
StdSchedulerFactory factory = new StdSchedulerFactory();
Scheduler scheduler = factory.getScheduler();
scheduler.start();
scheduler.scheduleJob(jobDetail, cronTrigger);
return "执行byCron";
}
/**
* 通过类路径获取类
* @return
* @throws ClassNotFoundException
*/
private Class getMyClass() throws ClassNotFoundException {
Class myClass = Class.forName("com.cobra.quartzlearn.job.MyJob");
return myClass;
}
}
三、启动项目测试:
1、访问localhost:8080/schedule/bySimple,控制台输出:
2、访问localhost:8080/schedule/byCron,控制台输出:
四、可控制停止开启任务的demo
1、新建类ScheduleController
package com.cobra.quartzlearn.controller;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* @Author: Baron
* @Description:
* @Date: Created in 2019/3/14 0:06
*/
@RestController
@RequestMapping("/quartz")
public class ScheduleController {
/**
* 开始执行任务
* @return
* @throws ClassNotFoundException
* @throws SchedulerException
*/
@GetMapping("/start")
public String start() throws ClassNotFoundException, SchedulerException {
Scheduler scheduler = createScheduler();
scheduler.start();
return "执行任务!!!!!";
}
/**
* 停止执行任务
* @return
* @throws SchedulerException
* @throws ClassNotFoundException
*/
@GetMapping("/stop")
public String stop() throws SchedulerException, ClassNotFoundException {
StdSchedulerFactory factory = new StdSchedulerFactory();
Scheduler scheduler = factory.getScheduler();
JobKey jobKey = new JobKey("myStoppableJob");
scheduler.pauseJob(jobKey);
scheduler.deleteJob(jobKey);
return "停止任务!!!!!";
}
/**
* 新建任务
* @return
* @throws SchedulerException
* @throws ClassNotFoundException
*/
public Scheduler createScheduler() throws SchedulerException, ClassNotFoundException {
JobDetail jobDetail = JobBuilder.newJob(getMyClass()).withIdentity("myStoppableJob").build();
SimpleTrigger simpleTrigger = TriggerBuilder.newTrigger().withIdentity("myStoppableTrigger").startNow().withSchedule(
SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(2).repeatForever()).build();
StdSchedulerFactory factory = new StdSchedulerFactory();
Scheduler scheduler = factory.getScheduler();
scheduler.scheduleJob(jobDetail, simpleTrigger);
return scheduler;
}
/**
* 通过类路径获取类
* @return
* @throws ClassNotFoundException
*/
private Class getMyClass() throws ClassNotFoundException {
Class myClass = Class.forName("com.cobra.quartzlearn.job.MyJob");
return myClass;
}
}
2、重启项目,测试访问localhost:8080/quartz/start,控制台输出日志:
访问localhost:8080/quartz/stop,控制台停止输出日志