首页 > 解决方案 > 如何使用具有相同 CronTrigger 对象的 Quartz 调度两次调度作业?

问题描述

我正在使用 Quartz-Scheduler 来安排任务。我的要求是在给定日期范围之间有两个触发器,一个在开始时间,一个在结束时间。

IE -String startDateStr = "2013-09-27 00:00:00.0"; String endDateStr = "2013-09-31 00:00:00.0";

 expecting first trigger at 09:00 AM
 expecting second trigger at 12:00 PM

我如何使用动态 cron 表达式来实现相同的目的。对此的任何帮助将不胜感激。期待中的感谢

    public class CronJob {

    public static void main(String[] args) throws ParseException, SchedulerException {

        Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();

        JobDetail job = newJob(TestJob.class)
            .withIdentity("cronJob", "testJob") 
            .build();

        String startDateStr = "2013-09-27 00:00:00.0";
        String endDateStr = "2013-09-31 00:00:00.0";

        Date startDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S").parse(startDateStr);
        Date endDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S").parse(endDateStr);

        CronTrigger cronTrigger = newTrigger()
          .withIdentity("trigger1", "testJob")
          .startAt(startDate)
          .withSchedule(CronScheduleBuilder.cronSchedule("0 0 9-12 * * ?").withMisfireHandlingInstructionDoNothing())
          .endAt(endDate)
          .build();

        scheduler.scheduleJob(job, cronTrigger);
        scheduler.start();
    }    

    public static class TestJob implements Job {
        @Override
        public void execute(JobExecutionContext context) throws JobExecutionException {
            System.out.println("this is a cron scheduled test job");
        }        
    }
}




 

标签: javacronquartz-schedulercrontrigger

解决方案


推荐阅读