4

スプリング タスク スケジューラ (ConcurrentTaskScheduler) を使用してタスクをスケジュールしています。APIを使用しています

public ScheduledFuture schedule(Runnable task,Trigger trigger)

私のタスクを実行します。私が使用しているトリガーは CronTrigger です。
次のステートメントを使用してトリガーを初期化しています

Trigger trigger = new CronTrigger(cronExp);

特定の日付に開始し、それ以降毎日実行されるように cronExp を作成する必要があります。

ConcurrentTaskScheduler の API を確認しましたが、適切な API を見つけることができました。API を見逃している可能性があります。

上記の要件を達成する方法を誰かに提案できますか?

4

2 に答える 2

5

To my knowledge, you can't use Spring's CronTrigger to start only from a certain date.

Cron syntax doesn't support running something daily from an arbitrary date; it supports EITHER running something daily OR running once on an arbitrary date — but not both at once. That means you could use two triggers: have one cron trigger set to trigger on your start date; then create a new daily trigger when that first trigger occurs.

However this only works properly with if the cron trigger you are using supports years, for example Quartz has an option year field in its cron trigger. Spring's CronTrigger doesn't support years. So if you did try to schedule something for a specific date (say 0 0 12 26 1 ? for noon on Australia day) then it would run every year, not just once, causing duplicate triggers to be created each year.

Instead I recommend creating a simple trigger to run daily, ie:

    Trigger trigger = new CronTrigger("0 0 12 * * ?);

So your code will run daily. Then add a simple date check in your code: if you haven't reached the start date then skip your task, ie:

    if ((new Date()).after(startDate)) {
        // Run your task here
    }
于 2011-03-15T04:00:11.227 に答える
0

以下のリンクを見つけてください

http://www.mkyong.com/spring/spring-quartz-scheduler-example/

Spring には、統合された Quartz スケジューラがあります。ジョブをスケジュールする必要があるのは xml 構成のみです。そこで、必要に応じて cron 式を構成できます。

スプリングクォーツスケジューラ統合を使用することをお勧めします

于 2013-03-21T04:07:23.613 に答える