Quartz入门例子
程序员文章站
2022-05-24 19:05:38
...
//给女友打电话是一个job
public class CallGirl implements Job{
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
System.out.println("正在打电话给女友...");
System.out.println("getTrigger().getName():"+context.getTrigger().getName());
}
}
package com.mtea.qtz.test;
import java.util.Date;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SimpleTrigger;
import org.quartz.impl.StdSchedulerFactory;
import com.mtea.qtz.job.CallGirl;
public class QuartzMainer {
public static void main(String[] args) throws SchedulerException {
Scheduler handsomeMan = new StdSchedulerFactory().getScheduler();
//CallGirl.class 回调类
JobDetail concerneGirl = new JobDetail("任务的名称", "group1", CallGirl.class);
//10 重复次数,2000L 周期
SimpleTrigger momentTrigger = new SimpleTrigger("trigger1", "group1", new Date(), null, 10, 2000L);
//way 2
//若时间到,即使重复100000000次也没有用
long startTime=System.currentTimeMillis();
SimpleTrigger momentTrigger2= new SimpleTrigger("trigger2", "group1");
momentTrigger2.setStartTime(new Date(startTime));
momentTrigger2.setEndTime(new Date(startTime+6000L));
momentTrigger2.setRepeatCount(10);
momentTrigger2.setRepeatInterval(1000L);
//帅哥通过时刻Trigger来调度关心女友这个job
handsomeMan.scheduleJob(concerneGirl, momentTrigger);
//开始
handsomeMan.start();
}
}
下一篇: Quartz简介