Quartz-SimpleTrigger

  1.  SimpleTrigger
    Trigger 就是触发器的意思,用来指定什么时间开始触发,触发多少次,每隔多久触发一次.
    SimpleTrigger 可以方便的实现一系列的触发机制。
  2.  下一个8秒的倍数
    Quartz-SimpleTrigger_第1张图片
    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);
        }
    }
    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();
            }
        }
    }
  3. 10 秒后运行
    使用方法:
     DateBuilder.futureDate()
    可以方便的获取10秒后, 5分钟后, 3个小时候,2个月后这样的时间

    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);
        }
    }
  4. 累计n次,间隔n秒
    Quartz-SimpleTrigger_第2张图片
    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);
        }
    }
  5.  无限重复,间隔1秒
    注: 无限重复 累计次数是显示的0,因为没法表示无限。。
    Quartz-SimpleTrigger_第3张图片
    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);
        }
    }











你可能感兴趣的:(Quartz)