步骤 2 : 下一个8秒的倍数 步骤 3 : 10 秒后运行 步骤 4 : 累计n次,间隔n秒 步骤 5 : 无限重复,间隔1秒
Trigger 就是触发器的意思,用来指定什么时间开始触发,触发多少次,每隔多久触发一次.
SimpleTrigger 可以方便的实现一系列的触发机制。 package com.how2java;
import static org.quartz.JobBuilder.newJob;
import static org.quartz.TriggerBuilder.newTrigger;
import java.util.Date;
import org.quartz.DateBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SimpleTrigger;
import org.quartz.impl.StdSchedulerFactory;
public class TestQuartz {
public static void main(String[] args) throws Exception{
Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
Date startTime = DateBuilder.nextGivenSecondDate(null, 8);
JobDetail job = newJob(MailJob.class).withIdentity("mailJob", "mailGroup").build();
SimpleTrigger trigger = (SimpleTrigger) newTrigger().withIdentity("trigger1", "group1").startAt(startTime).build();
// schedule it to run!
Date ft = scheduler.scheduleJob(job, trigger);
System.out.println("当前时间是:" + new Date().toLocaleString());
System.out.printf("%s 这个任务会在 %s 准时开始运行,累计运行%d次,间隔时间是%d毫秒%n", job.getKey(), ft.toLocaleString(), trigger.getRepeatCount()+1, trigger.getRepeatInterval());
scheduler.start();
//等待200秒,让前面的任务都执行完了之后,再关闭调度器
Thread.sleep(200000);
scheduler.shutdown(true);
}
}
package com.how2java;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.quartz.DisallowConcurrentExecution;
import org.quartz.Job;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
public class MailJob implements Job {
public void execute(JobExecutionContext context) throws JobExecutionException {
JobDetail detail = context.getJobDetail();
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
String now = sdf.format(new Date());
System.out.printf("发出了一封邮件, 当前时间是: %s%n" , now);
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
使用方法:
DateBuilder.futureDate() 可以方便的获取10秒后, 5分钟后, 3个小时候,2个月后这样的时间 package com.how2java;
import static org.quartz.JobBuilder.newJob;
import static org.quartz.TriggerBuilder.newTrigger;
import java.util.Date;
import org.quartz.DateBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SimpleTrigger;
import org.quartz.DateBuilder.IntervalUnit;
import org.quartz.impl.StdSchedulerFactory;
public class TestQuartz {
public static void main(String[] args) throws Exception{
Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
Date startTime = DateBuilder.futureDate(10, IntervalUnit.SECOND);
JobDetail job = newJob(MailJob.class).withIdentity("mailJob", "mailGroup").build();
SimpleTrigger trigger = (SimpleTrigger) newTrigger().withIdentity("trigger1", "group1").startAt(startTime).build();
// schedule it to run!
Date ft = scheduler.scheduleJob(job, trigger);
System.out.println("当前时间是:" + new Date().toLocaleString());
System.out.printf("%s 这个任务会在 %s 准时开始运行,累计运行%d次,间隔时间是%d毫秒%n", job.getKey(), ft.toLocaleString(), trigger.getRepeatCount()+1, trigger.getRepeatInterval());
scheduler.start();
//等待200秒,让前面的任务都执行完了之后,再关闭调度器
Thread.sleep(200000);
scheduler.shutdown(true);
}
}
package com.how2java;
import static org.quartz.JobBuilder.newJob;
import static org.quartz.TriggerBuilder.newTrigger;
import static org.quartz.SimpleScheduleBuilder.simpleSchedule;
import java.util.Date;
import org.quartz.DateBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SimpleTrigger;
import org.quartz.impl.StdSchedulerFactory;
public class TestQuartz {
public static void main(String[] args) throws Exception{
Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
Date startTime = DateBuilder.nextGivenSecondDate(null, 8);
JobDetail job = newJob(MailJob.class).withIdentity("mailJob", "mailGroup").build();
SimpleTrigger trigger = (SimpleTrigger) newTrigger()
.withIdentity("trigger1", "group1")
.startAt(startTime)
.withSchedule(simpleSchedule()
.withRepeatCount(3)
.withIntervalInSeconds(1))
.build();
// schedule it to run!
Date ft = scheduler.scheduleJob(job, trigger);
System.out.println("当前时间是:" + new Date().toLocaleString());
System.out.printf("%s 这个任务会在 %s 准时开始运行,累计运行%d次,间隔时间是%d毫秒%n", job.getKey(), ft.toLocaleString(), trigger.getRepeatCount()+1, trigger.getRepeatInterval());
scheduler.start();
//等待200秒,让前面的任务都执行完了之后,再关闭调度器
Thread.sleep(200000);
scheduler.shutdown(true);
}
}
注: 无限重复 累计次数是显示的0,因为没法表示无限。。
package com.how2java;
import static org.quartz.JobBuilder.newJob;
import static org.quartz.TriggerBuilder.newTrigger;
import static org.quartz.SimpleScheduleBuilder.simpleSchedule;
import java.util.Date;
import org.quartz.DateBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SimpleTrigger;
import org.quartz.impl.StdSchedulerFactory;
public class TestQuartz {
public static void main(String[] args) throws Exception{
Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
Date startTime = DateBuilder.nextGivenSecondDate(null, 8);
JobDetail job = newJob(MailJob.class).withIdentity("mailJob", "mailGroup").build();
SimpleTrigger trigger = (SimpleTrigger) newTrigger()
.withIdentity("trigger1", "group1")
.startAt(startTime)
.withSchedule(simpleSchedule()
.repeatForever()
.withIntervalInSeconds(1))
.build();
// schedule it to run!
Date ft = scheduler.scheduleJob(job, trigger);
System.out.println("当前时间是:" + new Date().toLocaleString());
System.out.printf("%s 这个任务会在 %s 准时开始运行,累计运行%d次,间隔时间是%d毫秒%n", job.getKey(), ft.toLocaleString(), trigger.getRepeatCount()+1, trigger.getRepeatInterval());
scheduler.start();
//等待200秒,让前面的任务都执行完了之后,再关闭调度器
Thread.sleep(200000);
scheduler.shutdown(true);
}
}
HOW2J公众号,关注后实时获知最新的教程和优惠活动,谢谢。
问答区域
2019-08-13
请问trigger的withIdentity("trigger1", "group1")这个是干吗用的,去掉之后马上就执行了
提问之前请登陆
提问已经提交成功,正在审核。 请于 我的提问 处查看提问记录,谢谢
|