springboot实现quartz定时任务
原创
©著作权归作者所有:来自51CTO博客作者摇滚侠的原创作品,请联系作者获取转载授权,否则将追究法律责任
1.添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-quartz</artifactId>
</dependency>
2.创建定时任务
package com.yangjunbo.JPADemo.scheduled;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.quartz.QuartzJobBean;
public class SampleJob extends QuartzJobBean {
private static final Logger logger = LoggerFactory.getLogger(SchedulerTask.class);
private String name;
public void setName(String name) {
this.name = name;
}
@Override
protected void executeInternal(JobExecutionContext context)
throws JobExecutionException {
logger.info(String.format("Hello %s!", this.name));
}
}
3. 构建JobDetail、CronTrigger
package com.yangjunbo.JPADemo.config;
import com.yangjunbo.JPADemo.scheduled.SampleJob;
import org.quartz.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class SampleScheduler {
@Bean
public JobDetail sampleJobDetail() {
return JobBuilder.newJob(SampleJob.class).withIdentity("sampleJob")
.usingJobData("name", "weiz test1")
.storeDurably()
.build();
}
@Bean
public Trigger sampleJobTrigger() {
SimpleScheduleBuilder scheduleBuilder = SimpleScheduleBuilder.simpleSchedule()
.withIntervalInSeconds(10).repeatForever();
return TriggerBuilder
.newTrigger()
.forJob(sampleJobDetail())
.withIdentity("sampleTrigger")
.withSchedule(scheduleBuilder).build();
}
}
在上面的示例中,定义的SampleScheduler调度器类包含JobDetail和Trigger两个对象,分别用于控制任务启动时的参数传入和任务触发规则。示例参数说明如下:
1)JobBuilder类构造函数只能通过JobBuilder的静态方法newJob(Class<? extends Job> jobClass)生成JobDetail实例。2)usingJobData()方法动态传入Job任务中的参数,这里传入SampleJob任务类中的name参数。
3)withIdentity()方法可以传入String name和String group两个参数来定义TriggerKey。当然,也可以不设置参数,Quartz会自动生成一个独一无二的TriggerKey来区分不同的触发器。
4)withIntervalInSeconds()方法中的任务触发规则包含withIntervalInXXX等多种时间设置。示例代码中定义了每10秒执行一次任务。
4.运行测试
参考书籍:《springboot从入门到实战-章为忠著》
5.cron表达式
如果需要定义更复杂的应用场景可以使用CronSchedule,它可以设置更灵活的使用方式
6.创建CronJob
package com.yangjunbo.JPADemo.scheduled;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.quartz.QuartzJobBean;
public class CronJob extends QuartzJobBean {
private static final Logger logger = LoggerFactory.getLogger(SchedulerTask.class);
private String name;
public void setName(String name) {
this.name = name;
}
@Override
protected void executeInternal(JobExecutionContext context)
throws JobExecutionException {
logger.info(String.format("Hello %s!", this.name));
}
}
7.创建CronScheduler,构建jobdetail、crontrigger
package com.yangjunbo.JPADemo.config;
import com.yangjunbo.JPADemo.scheduled.CronJob;
import org.quartz.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class CronScheduler {
@Bean
public JobDetail cronJobDetail(){
return JobBuilder.newJob(CronJob.class)
.withIdentity("cronJob")
.usingJobData("name","weiz cronJob")
.storeDurably()
.build();
}
@Bean
public Trigger cronJobTrigger(){
CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule("0/10 * * * * ?");
return TriggerBuilder.newTrigger()
.withIdentity("CronSchedule")
.forJob(cronJobDetail())
.withSchedule(scheduleBuilder)
.build();
}
}
8.运行测试
参考书籍:《springboot从入门到实战-章为忠著》