效果:访问controller后,添加定时任务,十秒后输出访问者的ip
新建maven项目,pom文件:
<?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>
<groupId>com.tansun</groupId>
<artifactId>quartz-test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>quartz-test</name>
<description></description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.2.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
配置类:
package com.tansun.config;
import java.util.UUID;
import org.quartz.CronScheduleBuilder;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import com.tansun.job.FirstJob;
import com.tansun.job.SecondJob;
import com.tansun.job.ThirdJob;
@Configuration
public class QuartzConfig {
@Bean
public Scheduler scheduler() throws SchedulerException {
Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
JobDetail firstJob = JobBuilder.newJob(FirstJob.class).withIdentity("job1", "group1").build();
JobDetail secondJob = JobBuilder.newJob(SecondJob.class).withIdentity("job2", "group1").build();
Trigger firstTrigger = TriggerBuilder.newTrigger().withIdentity("trigger1", "group1")
.withSchedule(CronScheduleBuilder.cronSchedule("0/2 * * * * ?")).build();
Trigger secondTrigger = TriggerBuilder.newTrigger().withIdentity("trigger2", "group1")
.withSchedule(CronScheduleBuilder.cronSchedule("0/3 * * * * ?")).build();
scheduler.scheduleJob(firstJob, firstTrigger);
scheduler.scheduleJob(secondJob, secondTrigger);
scheduler.start();
return scheduler;
}
@Bean
@Scope("prototype")
public JobDetail jobDetail() {
JobDetail jobDetail = JobBuilder.newJob(ThirdJob.class)
.withIdentity("job" + UUID.randomUUID(), "group2").build();
return jobDetail;
}
}
工具类:
package com.tansun.utils;
import java.util.Date;
import java.util.UUID;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class QuartzUtil {
@Autowired
private Scheduler scheduler;
public void scheTask(String ip) throws SchedulerException {
Trigger trigger = TriggerBuilder.newTrigger().withIdentity("trigger" + UUID.randomUUID(), "group2").startAt(new Date(new Date().getTime() + 10 * 1000)).build();
// jobDetail配置多例,QuartzUtil是单例,无法直接注入,所以使用Spring容器获取
JobDetail jobDetail = SpringUtil.getBean(JobDetail.class);
jobDetail.getJobDataMap().put("ip", ip);
scheduler.scheduleJob(jobDetail, trigger);
}
}
package com.tansun.utils;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
@Component
public class SpringUtil implements ApplicationContextAware {
private static ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
if(SpringUtil.applicationContext == null) {
SpringUtil.applicationContext = applicationContext;
}
}
public static <T> T getBean(Class<T> clazz){
return applicationContext.getBean(clazz);
}
}
job类:
package com.tansun.job;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
public class ThirdJob implements Job {
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
System.out.println(context.getJobDetail().getJobDataMap().get("ip") + "在10秒之前点击过页面");
}
}
控制器:
package com.tansun.controller;
import javax.servlet.http.HttpServletRequest;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import com.tansun.utils.QuartzUtil;
@RestController
public class QuartzController {
@Autowired
private QuartzUtil quartzUtil;
@GetMapping
public String test1(HttpServletRequest request) throws SchedulerException {
quartzUtil.scheTask(request.getRemoteAddr());
return "ok";
}
}
主方法:
package com.tansun;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class QuartzTestApplication {
public static void main(String[] args) {
SpringApplication.run(QuartzTestApplication.class, args);
}
}
完整的代码的地址:http://download.csdn.net/download/jia_costa/10230990