quartz定时任务
package com.source.security.config;
import org.quartz.*;
import org.quartz.impl.JobDetailImpl;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class QuartzSimpleConfig {
//指定具体的定时任务类
@Bean // 1 创建job对象,你要做什么事
public JobDetail uploadTaskDetail() {
return JobBuilder.newJob(QuartzSimpleTask.class)
.withIdentity("QuartzSimpleTask")
.storeDurably().build();
}
@Bean // 2 创建Trigger对象 在什么时间做
public Trigger uploadTaskTrigger() {
//这里设定触发执行的方式
CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule("*/5 * * * * ?");
// 返回任务触发器
return TriggerBuilder.newTrigger().withSchedule(SimpleScheduleBuilder.repeatSecondlyForever(3)).forJob(uploadTaskDetail())
.withIdentity("QuartzSimpleTask11")
// .withSchedule(scheduleBuilder)
.build();
}
// 3 创建 Scheduler对象再什么时间做什么事
}
package com.source.security.config;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import java.util.Date;
//public class QuartzSimpleTask extends QuartzJobBean {
public class QuartzSimpleTask implements Job {
@Override
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
System.out.println(jobExecutionContext.getCalendar());
System.out.println("quartz简单的定时任务执行时间222222222:"+new Date().toLocaleString());
}
/*@Override
protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
System.out.println("quartz简单的定时任务执行时间:"+new Date().toLocaleString());
}*/
}