2016-09-24 7 views
1

jacabi-aspectsを使用してURLに接続を再試行して接続が戻るまでhttp://xxxxxx:8080/hello接続が戻ってくるまでご存知のように@RetryOnFailure by jcabiには2つのフィールドの試行と遅延があります。私はjcabi @ RetryOnFailure.Howに試み(12)= expiryTime(1分= 60000ミリ秒)/遅延時間(5秒= 5000ミリ秒)ような操作を実行するjcabiで@RetryOnFailureの新しい操作を実行する方法

iはthis.Theコードスニペットをしますかであります以下のように。

@RetryOnFailure(attempts = 12, delay = 5) 
public String load(URL url) { 
    return url.openConnection().getContent(); 
} 
+0

Yegorのブログを見ると、この機能に関する記事があります:http://www.yegor256.com/2014/08/15/retry-java-method-on-exception.html私はそれがあなたを助けることができると思います – Dorian

+0

実際には、実際には5秒ごとに失敗するコードを実行したい、つまり**有効期限**です。私のコードの有効期限は重要です!! @ RetryOnFailureに期限を設定する方法は私の質問です –

答えて

1

など、backOffPolicies、リスナーのような、非常に使いやすく、機能の完全です永遠に、しかし@Timeableは分でそれを止めるでしょう。

1

あなたが選んだライブラリ(jcabi)にはこの機能がありません。しかし、幸いにも春バッチから非常に便利RetryPoliciesが抽出されている(あなたがバッチ処理せずに、一人でそれらを使用することができます):多くのクラスの

Spring-Retry

一つTimeoutRetryPolicyありますから、あなたが使用することができます

RetryTemplate template = new RetryTemplate();

@Timeable(unit = TimeUnit.MINUTE, limit = 1) 
@RetryOnFailure(attempts = Integer.MAX_VALUE, delay = 5) 
public String load(URL url) { 
    return url.openConnection().getContent(); 
} 

@RetryOnFailureが再試行されます:

TimeoutRetryPolicy policy = new TimeoutRetryPolicy(); 
policy.setTimeout(30000L); 

template.setRetryPolicy(policy); 

Foo result = template.execute(new RetryCallback<Foo>() { 

    public Foo doWithRetry(RetryContext context) { 
     // Do stuff that might fail, e.g. webservice operation 
     return result; 
    } 

}); 

全体のバネリトライプロジェクトは、次の2つのアノテーションを組み合わせることができます

関連する問題