4

反復タスクに Quartz-Scheduler を使用していますが、問題に直面しています。私のサーバー側では、ユーザーは次のような日付範囲を指定したいと考えてます 2013-09-27 09:00 AM - 12:00 PM 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

UNIX/Linux の cron 式では、正確な開始日と終了日を指定できますか?

アップデート

書いたけど動かない

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

1 に答える 1

7

動作していないと言うと、どのようなエラーが表示されますか?

次のコードを試すことができます (編集: 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 ? * ?")

于 2013-09-27T19:07:46.870 に答える