2017-03-31 36 views
1

春のブートアプリケーションでは、以下のようにyamlファイルにいくつかの設定プロパティを定義します。Springで設定プロパティを挿入する方法Spring起動時の再試行注釈?

my.app.maxAttempts = 10 
my.app.backOffDelay = 500L 

そして、例えば、豆、私は春のリトライ注釈にmy.app.maxAttemptsmy.app.backOffdelayの値を挿入するにはどうすればよい

@ConfigurationProperties(prefix = "my.app") 
public class ConfigProperties { 
    private int maxAttempts; 
    private long backOffDelay; 

    public int getMaxAttempts() { 
    return maxAttempts; 
    } 

    public void setMaxAttempts(int maxAttempts) { 
    this.maxAttempts = maxAttempts; 
    } 

    public void setBackOffDelay(long backOffDelay) { 
    this.backOffDelay = backOffDelay; 
    } 

    public long getBackOffDelay() { 
    return backOffDelay; 
    } 

?以下の例では、maxAttemptsの値10と、バックオフ値の500Lを、対応する設定プロパティの参照と置き換えたいとします。

@Retryable(maxAttempts=10, include=TimeoutException.class, [email protected](value = 500L)) 

答えて

6

我々は@Retryable注釈に設定可能なプロパティを使用することができますspring-retry-1.2.0から見つめます。あなたが任意の設定可能なプロパティクラスを必要としない1.2.0.Also未満の任意のバージョンを使用する場合、それは動作しません

@Retryable(maxAttemptsExpression = "#{${my.app.maxAttempts}}", 
backoff = @Backoff(delayExpression = "#{${my.app. backOffDelay}}")) 

使用「maxAttemptsExpression」は、使用するために以下のコードを参照してください。

1

下図のようにあなたは、プロパティをロードするために春ELを使用することができます。

@Retryable(maxAttempts="${my.app.maxAttempts}", 
    include=TimeoutException.class, 
    [email protected](value ="${my.app.backOffDelay}")) 
1

既存のBeanを式属性で使用することもできます。

@Retryable(include = RuntimeException.class, 
      maxAttemptsExpression = "#{@retryProperties.getMaxAttempts()}", 
      backoff = @Backoff(delayExpression = "#{@retryProperties.getBackOffInitialInterval()}", 
           maxDelayExpression = "#{@retryProperties.getBackOffMaxInterval" + "()}", 
           multiplierExpression = "#{@retryProperties.getBackOffIntervalMultiplier()}")) 
    String perform(); 

    @Recover 
    String recover(RuntimeException exception); 

retryProperties

あなたの場合のようにリトライ関連のプロパティを保持し、あなたの豆です。

関連する問題