2020-10-23 10:47:40
浏览数 (1)
1.MyJob
代码语言:javascript
复制package utils;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import java.util.Date;
import java.text.SimpleDateFormat;
/** 工作类 具体需要特定时间执行的某个任务 备份数据库 定时还款
* zt
* 2020/10/15
* 11:18
*/
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
代码语言:javascript
复制package utils;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import java.util.Date;
/**
* zt
* 2020/10/15
* 11:20
*/
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.运行结果