Trigger 就是触发器的意思,用来指定什么时间开始触发,触发多少次,每隔多久触发一次
SimpleTrigger 可以方便的实现一系列的触发机制。
1.下一个8秒的倍数开始运行:
public class SimpleTriggerDemo { public static void main(String[] args) throws Exception{ Scheduler scheduler= StdSchedulerFactory.getDefaultScheduler(); Date startTime = DateBuilder.nextGivenSecondDate(null, 8); JobDetail job=JobBuilder.newJob(MailJob.class) .withIdentity("job1","group1") .usingJobData("email","憨批!") .build(); SimpleTrigger trigger=(SimpleTrigger) TriggerBuilder.newTrigger() .withIdentity("job1","group1") .withSchedule(simpleSchedule().withIntervalInSeconds(1).withRepeatCount(5)) .startAt(startTime) .build(); 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(); Thread.sleep(200000); scheduler.shutdown(true); } }
2.10秒后运行
Date startTime = DateBuilder.futureDate(10, DateBuilder.IntervalUnit.SECOND);
3.无限重复,间隔1秒