この記事では、春の再試行をサポートするために春の再試行方法を紹介します。それはあなたと共有されます。詳細は次のとおりです。
最初のステップは、Maven依存関係を導入することです
<parent> <groupid> org.springframework.boot </groupid> <artifactid> spring-boot-starter-parent </artifactid> <バージョン> 1.5.3.release </version> </parent> <seplency> org.springframework.boot </groupid> <artifactid> spring-boot-starter-web </artifactid> </dependency> <! - https://mvnrepository.com/artifact/org.springframework.retry/spring-retry-> <依存症> <依存関係> <groupid> org.springframework.retry </groupid> </groupid> </groupid> </groupid> <バージョン> 1.1.2.Release </version> </dependency> <dependency> <groupid> org.aspectj </groupid> <artifactid> astifactid> astifactid> <バージョン> 1.8.6 </version> </dependency>
ステップ2:@retryableおよび@Recoverアノテーションを追加します
パッケージhello; import org.springframework.remoting.remoteacessexception; Import org.springframework.retry.annotation.backoff; import org.springframework. retry.annotation.Recover; Import org.springframework.retry.annotation.entrabe.not.not.Retrywork.Retrywork.Retrywork org.springframework.stereotype.service; @servicepublic class remoteservice {@retryable(value = {remoteaccessexception.class}、maxattempts = 3、backoff = @backoff(del = 5000l、multiplier = 1))新しいRemoteaCcessException( "RPCコール例外");}@RecoverPublic void Recover(RemoteaCcessexception E){System.out.Println(E.GetMessage());}}} @retryable Annotation <BR />注釈付きメソッドに例外がある場合、再試行されます
値:例外が発生したことを指定し、再試行してください
含める:Value、Defaultは空です。除外が空の場合、すべての例外が再試行されます
除外:例外が再試行されないことを指定し、デフォルトは空です。含めることも空の場合、すべての例外が再試行されます。
maxattemps:再試行時間の数、デフォルト3
バックオフ:報酬メカニズムを再試行します。デフォルトはありません
@backoff annotation
遅延:遅延を指定し、再試行してください
乗数:遅延= 5000L、乗数= 2など、最初の再試行が5秒、2回目は10秒、3回目は20秒です。
@回復する
再試行が指定された回数に達すると、注釈付きメソッドが呼び出され、この方法でログ処理を実行できます。例外が発生し、パラメータータイプが同じ場合にのみ、コールバックが発生することに注意する必要があります。
ステップ3:コンテナを起動し、スプリングブートでテストします
@enableretryアノテーションを追加して、再試行機能を有効にします
パッケージhello; import org.springframework.boot.autoconfigure.springbootapplication; Import org.springframework.context.applicationContext; Import org.springframework.context.annotation.annotation.annotationConfigapplicationContext; Import; org.springframework.retry.annotation.Enableretry;@SpringBootApplication@enableretrypublic class Application {public static void main(string [] args)throws {applicationContextAnnotationContext = new AnnotationConfigApplicationContext( "hello"); Remoteservice Remoteservice = annotationContext.getBean( "Remoteservice"、Remoteservice.class); Remoteservice.call(); }}実行結果:
16:50:51.012 [Main] Debug org.springframework.retry.support.retrytemplate -retry:count = 0
何かをする…
16:50:51.025 [Main] Debug org.springframework.retry.backoff.exponentialbackOffpolicy- 5000で寝ています
16:50:56.026 [Main] Debug org.springframework.retry.support.retrytemplate- rethrowのチェック:count = 1
16:50:56.026 [Main] Debug org.springframework.retry.support.retrytemplate -retry:count = 1
何かをする…
16:50:56.026 [Main] Debug org.springframework.retry.backoff.exponentialbackoffpolicy- 5000で寝ています
16:51:01.026 [Main] Debug org.springframework.retry.support.retrytemplate- rethrowのチェック:count = 2
16:51:01.027 [Main] Debug org.springframework.retry.support.retrytemplate -retry:count = 2
何かをする…
16:51:01.027 [Main] Debug org.springframework.retry.support.retrytemplate- rethrowのチェック:count = 3
16:51:01.027 [Main] Debug org.springframework.retry.support.retrytemplate-再試行失敗した最後の試み:count = 3
RPCコール例外
参照:https://github.com/spring-projects/spring-retry
補充します
非公開リクエスト(新規操作や更新された操作など)の場合、データの一貫性に大きな影響を与えるため、再試行を使用しないでください。
上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。