在特定的开始、结束日期和时间限制内运行 Quartz Scheduler Job [英] Run Quartz Scheduler Job with specific start, end date and within time constraints

查看:48
本文介绍了在特定的开始、结束日期和时间限制内运行 Quartz Scheduler Job的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using Quartz-Scheduler for repetitive tasks but I am facing a trouble. In my server side my user wants to specify some date range like From 2013-09-27 with in 09:00 AM - 12:00 PM to 2013-09-30

说明:

2013-09-272013-09-30 运行作业,但仅限于 09:00 AM - 12:00 PM

Run a job from 2013-09-27 to 2013-09-30 but only between 09:00 AM - 12:00 PM

我在为它编写 Cron 表达式时遇到了麻烦,而且我的用户不是技术人员,所以我的用户希望我从两个时间戳值自动创建 Cron 表达式.

I am facing trouble in writing a Cron expression for it, furthermore my user is non-technical so my user wants me to create Cron expression automatically from both time stamp values.

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

Please help me out. Let me know if there is another way.

我在谷歌上看到了很多资源,但我仍然找不到任何东西.

I have seen many resources on Google but I still can't find nothing.

链接:

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

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

是否有 cron 表达式在 unix/linux 中允许指定确切的开始和结束日期

更新

我已经写了一个但它不起作用

I have written one but it's not working

|------------------------------------------------------------------|
| 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

trying to map 2013-09-27 to 2013-09-30 but only between 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();

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

but it doesn't give any error nor go into my execute method of my job

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

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

The code below runs it without respecting the start and end date.

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();

推荐答案

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

What is the error you get when you say it is not working?

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

You can try the following code ( applies to Quartz 2.2). This approach does not specify the start/end dates and year in the cron expression, instead uses the Trigger methods to specify them. (Note: I haven't tested it myself, let me know if it works for you)

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

I had the chance to test this code, I ran the code below and kept changing the system clock and all triggers were successful between 9 am to 12 am from start to end date.

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 ? * ?")

If the above code does not work, try to replace the cronSchedule("0 0 9-12 * * ?") with cronSchedule("0 0 9-12 ? * ?")

这篇关于在特定的开始、结束日期和时间限制内运行 Quartz Scheduler Job的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆