pom.xml
<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>
<groupId>com.gqbj</groupId>
<artifactId>quartz-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.0.4.RELEASE</version>
</parent>
<dependencies>
<!-- pom文件 Spring Quartz依赖 -->
<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>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>6</source>
<target>6</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
Application
package com.gqbj.quartz_demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application
{
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
MyJob
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class MyJob implements Job {
private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
@Override
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
System.out.println(jobExecutionContext.getJobDetail());
System.out.println("我是业务,调用时间:" + sdf.format(new Date()));
}
}
QuartzSchedulerManager
import com.gqbj.quartz_demo.task.MyJob;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import java.util.Date;
@Configuration
public class QuartzSchedulerManager {
@Autowired
private Scheduler scheduler;
public void startJob(Date time) throws SchedulerException {
startJobTest(scheduler,time);
scheduler.start();
}
private void startJobTest(Scheduler scheduler , Date time) throws SchedulerException {
System.out.println(time + "TIME");
JobDetail jobDetail = JobBuilder.newJob(MyJob.class).withIdentity("job1", "group1").build();
SimpleScheduleBuilder simpleScheduleBuilder = SimpleScheduleBuilder.simpleSchedule();
SimpleTrigger simpleTrigger = TriggerBuilder.newTrigger().withIdentity("job1", "group1")
.withSchedule(simpleScheduleBuilder).startAt(time).build();
scheduler.scheduleJob(jobDetail, simpleTrigger);
}
}
testController
import com.gqbj.quartz_demo.config.QuartzSchedulerManager;
import org.quartz.SchedulerException;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.Date;
@RestController
public class testController {
@Resource
private QuartzSchedulerManager quartzSchedulerManager;
@RequestMapping("/startJob")
public void start(@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date time) throws SchedulerException {
System.out.println("开启定时器");
quartzSchedulerManager.startJob(time);
}
}
postman调用
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/0ab314c2654f042fc01d2372df4e273b.png)
控制台
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/1709bb8872a8ba1b42ba3cfe518df439.png)
大功告成!!!