2017-03-24 10 views
0

何らかのタスク(メソッド)を実行できるサービスを作成しようとしていて、そのタスクが失敗した場合は再試行します。私は(私はタイマーサービスを変更することはできませんよ)石英でタイマーサービスを使用していますタイマーを作成するための失敗した場合に一定時間タスクを実行する

Timerサービスまた、作成したタイマーのハンドラを登録することができる方法

this.timerService.createTimerRunRepeatedly(
      handledId, 
      timerId, 
      optionalDescription, 
      totalFireCount, 
      intervalMiliSeconds, 
      offsetMiliseconds, 
      optionalCallbackData); 

を持っています。

this.timerService.registerHandler(
        handlerId, 
        firedCallback, 
        expiredCallback); 

firedCallback - たびに呼び出されます>(オプション)コールバックこのハンドラが起動してタイマー(final Consumer<T> firedCallback

expiredCallback - タイマーと後に呼ばれる>(オプション)コールバックこのハンドラは、最後の時間を解雇した(final Consumer<T> expiredCallback

どのように私はこのretrySにいくつかの方法を渡すことができ、私は新しいTaskRetryServiceを作成しましたが、私は考えていますそれが実行されるようになる。

+0

http://stackoverflow.com/questions/2186931/java-pass-method-as-parameterこれはあなたが質問していた質問ですか? –

+0

それは似ています、はい。私は見てみましょう。 – mirzak

答えて

0

実際に私は必要なものすべてを持っていたので、それを動作させることができました。ただ、コールバックが

private <T> void fireCallback(final T t) { 
    System.out.println("fireCallback"); 
} 
private <T> void expiredCallback(final T t) { 
    System.out.println("expiredCallback"); 
} 

ある

@PostConstruct 
public void onPostConstruct() { 
    handler = this.retryService.registerHandler("1", this::fireCallback, this::expiredCallback); 
} 

ハンドラを登録して、私は

public void retryTask(final HandlerId<String> handlerId, 
         final String timerId, 
         final Integer intervalTimeMinutes, 
         final Integer retryTimeMinutes) { 
    this.timerService.createTimerRunRepeatedly(handlerId, 
      timerId, 
      "timer", 
      getTotalFireCount(intervalTimeMinutes, retryTimeMinutes), 
      (int) TimeUnit.MINUTES.toMillis(intervalTimeMinutes), 
      0, 
      null).thenAccept(timerWasCreated -> { 
     if (timerWasCreated) { 
      System.out.println("Timer was created"); 
     } else { 
      System.out.println("Error while creating timer"); 
     } 
    }); 
} 

そして

持って再試行サービスで

this.retryService.retryTask(handler, "12", 1, 5); 

でタイマーを作成

public <T> HandlerId<T> registerHandler(final String handlerId, 
             final Consumer<T> firedCallback, 
             final Consumer<T> expiredCallback) { 
    return this.timerService.registerHandler(handlerId, firedCallback, expiredCallback); 
} 
1

あなたはSpringを使用しているので、障害を処理して再試行するためにすぐに使用できる機能を持つSpring-Retryを使用することを検討してください。

@Retryable(SomeException.class)で注釈を付ける方法は、SomeExceptionが呼び出されたときに再試行されます。さらに、この方法は、呼び出される注釈付き故障コールバック@Recoverについて@Retryable(maxAttempts=12, [email protected](delay=100, maxDelay=500))

として試行間の遅延(指数ランダム&)を再試行を指定するためにカスタマイズすることができます。

詳細については、readmeのgithubとthisの記事をご覧ください。

希望すると便利です。

関連する問題