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

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

问题描述

我使用Quartz-Scheduler重复任务,但我面临麻烦。在我的服务器端,我的用户希望指定一些日期范围,例如 2013-09-27 c $ c> 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


说明:

Explanation:

2013-09-27 到 2013-09-30 但仅在 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.

我在Google上看到了很多资源,但我还是找不到任何东西。

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

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

更新

我写了一个,但不起作用

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

更新
我也尝试过使用

Updated I have also tried it running with

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


推荐答案

不工作?

您可以尝试以下代码(编辑:适用于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作业的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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