1.MyJob
package utils;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import java.util.Date;
import java.text.SimpleDateFormat;
public class MyJob implements Job {
@Override
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
System.out.println("双11活动开始啦,快来抢购啊 "+simpleDateFormat.format(new Date()));
}
}
2.QuartzDemo1
package utils;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import java.util.Date;
public class QuartzDemo1 {
public static void main(String[] args) throws Exception{
//1.调度器 scheduler
Scheduler scheduler = new StdSchedulerFactory().getScheduler();
//2.触发器 触发任务执行
Trigger trigger = TriggerBuilder
.newTrigger().withIdentity("myGroup1","myTrigger1")
.withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(5)
.repeatForever())
.startNow()
.build();
//3.工作对象 jobDetail
JobDetail jobDetail = JobBuilder.newJob(MyJob.class)
.withIdentity("myJob", "myGroup").build();
//4.注册任务和触发器
Date date = scheduler.scheduleJob(jobDetail, trigger);
//5.开启任务
scheduler.start();
}
}
3.运行结果