【问题标题】:Run Quartz Scheduler Job with specific start, end date and within time constraints在特定的开始、结束日期和时间限制内运行 Quartz 调度程序作业
【发布时间】:2013-10-03 18:51:33
【问题描述】:

我正在使用 Quartz-Scheduler 执行重复性任务,但我遇到了麻烦。在我的服务器端,我的用户想要指定一些日期范围,例如 From 2013-09-27 with in 09:00 AM - 12:00 PM to 2013-09-30

解释:

从2013-09-27 到2013-09-30 运行作业,但只能在09:00 AM - 12:00 PM 之间运行

我在为其编写 Cron 表达式时遇到了麻烦,此外,我的用户不是技术人员,因此我的用户希望我从两个时间戳值自动创建 Cron 表达式。

请帮帮我。如果有其他方法,请告诉我。

我在 Google 上看过很多资源,但还是什么都找不到。

链接:

http://quartz-scheduler.org/documentation/quartz-1.x/tutorials/crontrigger

http://quartz-scheduler.org/documentation/quartz-2.x/tutorials/tutorial-lesson-05

Does cron expression in unix/linux allow specifying exact start and end dates

更新

我写了一个,但它不起作用

|------------------------------------------------------------------|
| Seconds | Minutes | Hours | DayOfMonth | Month | DayOfWeek | Year|
|         |         |       |            |       |           |     |
|   0     |    0    | 9-12  |   27-30    |   9   |     ?     | 2013|
|------------------------------------------------------------------|

尝试将2013-09-27 映射到2013-09-30,但仅限于09:00 AM - 12:00 PM 之间

更新 我也试过用

运行它
Trigger trigger = TriggerBuilder.newTrigger().withIdentity(NAME_TRIGGER_TASK_UPDATER, GROUP_TASK_TRIGGER)
                    .withSchedule(CronScheduleBuilder.cronSchedule("0 0 9-12 19-22 10 ? *")).build();

但它没有给出任何错误,也没有进入我工作的执行方法

cronSchedule("0 0 9-12 ? * ?") throws invalid schedule exception.

下面的代码在不考虑开始和结束日期的情况下运行它。

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 * * ?"))
          .endAt(endDate)
          .build();

【问题讨论】:

  • 你的 cron 表达式在我看来是正确的。您的应用程序中的其他石英触发器和作业是否按预期执行?
  • 不只是运行这个:-( Quartz Scheduler Java 有什么不同吗?

标签: java cron quartz-scheduler


【解决方案1】:

当您说它不起作用时,您得到的错误是什么?

您可以尝试以下代码(编辑: 适用于 Quartz 2.2)。这种方法不在 cron 表达式中指定开始/结束日期和年份,而是使用 Trigger 方法来指定它们。 (注意:我自己没有测试过,如果它适合你,请告诉我)

编辑: 我有机会测试了这段代码,我运行了下面的代码并不断更改系统时钟,从开始到结束日期的上午 9 点到 12 点之间所有的触发器都成功了。

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");
        }        
    }
}

如果上述代码不起作用,请尝试将cronSchedule("0 0 9-12 * * ?")替换为cronSchedule("0 0 9-12 ? * ?")

【讨论】:

  • 我已经尝试了上面的代码并且它可以工作,唯一的问题是触发器在它应该触发的时间之后执行 20 秒或更晚。是石英问题还是外部原因?我改变我的时钟系统以跟上执行的日期。这会是原因之一吗
  • 是的,您需要确保系统时钟设置正确
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-11-10
  • 2016-11-14
  • 1970-01-01
  • 2018-02-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多