2012-04-16 19 views
0

私はタスクの応答に応じてスケジュールを設定しようとしています。タスクのようなものです:応答に応じてタスクをスケジュールします

public Date scheduledTask() { 
    Date nextRun; 
    // ... 
    nextRun = something(); 
    // ... 
    return nextRun; 
} 

どのように私はnextRunに到達したときに同じタスクが再び呼び出されていることを確認することができますか?

ありがとうございます。

+0

これは、戦略設計パターンの良い使用例です。ドキュメントを確認してくださいhttp://en.wikipedia.org/wiki/Strategy_pattern#Example – Phani

+0

あなたはタスクスケジューリングにどのようなアプローチをしていますか?例として[this](http://darthanthony.wordpress.com/2009/07/07/dynamic-scheduling-with-javaspring/)が役に立ちます。 – nobeh

+0

ありがとう、@nobeh。残念ながらあなたのリンクは私にはあまり役立たない、私はすでに同様のアプローチを試みたが、明らかに結果に応じてタスクを再スケジュールする方法がない。私は、SpringのQuartz SupportとSpring 3.0のScheduling Namespaceの両方を使って遊んでみました。どんな助けでも大歓迎です。ありがとう – satoshi

答えて

0

これは、標準のQuartzスケジューラAPIを使用すると非常に簡単です。そして、あなたのJob計算nextRun時間内で定義されたstartAt()でトリガーを作成します。

public class ScheduledJob implements Job { 

    @Override 
    public void execute(JobExecutionContext context) throws JobExecutionException { 
     final Date nextRun = something(); 

     Trigger trigger = newTrigger(). 
       startAt(nextRun). 
       forJob(context.getJobDetail()). 
       build(); 

     context.getScheduler().scheduleJob(trigger); 
    } 

} 

テストされたが、魔法のように動作します。

+0

こんにちは@TomaszNurkiewicz、ありがとうございます。あなたのアプローチは非常に良いようですが、唯一のことは、アプリケーションを開始するときに初めて手動で起動する必要があることです。それを行う最良の方法は何ですか?あなたのQuartz XML設定を私に見せることができますか?どうもありがとうございました。 – satoshi

0

は、あなたが持っていることができるはず、here述べたアイデアに従ってください:

public class GuaranteeSchedule implements Trigger { 

    private Future<?> resultForNextRun; 
    private TaskScheduler scheduler; 

    public void scheduledTask() { 
    // 'this' is this trigger that is used by the scheduler 
    // and the method `nextExecutionTime` is automatically called by the scheduler 
    resultForNextRun = scheduler.schedule(theTask, this); 
    // theTask is the object that calls something() 
    } 

    // Implementing Trigger to add control dynamic execution time of this trigger 
    @Override 
    public Date nextExecutionTime(TriggerContext tc) { 
    // make sure the result for the previous call is ready using the waiting feature of Future 
    Object result = resultForNextRun.get(); 
    // Use tc or other stuff to calculate new schedule 
    return new Date(); 
    } 

} 

残り、あなたは参照で述べた構成に従ってください。私は、これが前の結果のトリガーの次の呼び出しに依存する問題を解決すると信じています。また、scheduledTaskの最初の呼び出しに注意して、resultForNextRun != nullを確認する必要があります。

関連する問題