網站首頁 編程語言 正文
執行策略
執行策略的常見方式是調用策略的Execute函數
var policy = Policy.Handle<TimeoutException>().Retry(); policy.Execute(DoSomething);
返回值:
如果有返回值,它也是可以獲取其返回值的:
var result = policy.Execute(DoSomething);
捕獲異常:
如果在策略的執行過程中出現了異常,也會在該函數中同步拋出來,和直接執行該委托行為一致。可以直接通過try-catch處理。
try { policy.Execute(DoSomething); } catch (Exception e) { throw; }
也可以通過ExecuteAndCapture
函數捕獲異常。
var result = policy.ExecuteAndCapture(DoSomething); if (result.FaultType == null) { Console.WriteLine(result.Result); }
策略上下文
在策略的處理過程中,有一個上下文對象,可以在回調函數中使用:
public static RetryPolicy Retry(this PolicyBuilder policyBuilder, int retryCount, Action<Exception, int, Context> onRetry);
它是一個IDictionary<string,?object>類型的對象,它在Policy的執行過程中都可以使用,如:
Policy.Handle<TimeoutException>().Retry(3, (err, countDown, context) => { var method = context["method"]; ConsoleLogger.WriteLine(method); })
這個上下文對象可以在應用策略的時候帶入:
policy.Execute(DoSomething, new Context("context") { ["method"] = "PolicyTest" });
依賴注入
Polly也自帶了一個簡單的DI框架,方便復用Policy,使用方式如下:
var registry = new PolicyRegistry(); registry.Add("timeout & retry", Policy.Handle<TimeoutException>().Retry(3)); var policy = registry.Get<ISyncPolicy>("timeout & retry"); policy.Execute(DoSomething);
當然,也可以使用Autofac等自己喜歡的方式。
線程安全
Policy本身是線程安全的,可以并發使用同一個Policy,但如果Policy中執行的委托有多線程問題,仍然會出現異常。
異步的支持
Policy對異步操作也提供了良好的支持,只需要使用相應以Async結尾的函數即可。
var policy = Policy.Handle<TimeoutException>().RetryAsync(3); await policy.ExecuteAsync(DoSomethingAsync);
PS:有的不需要(也沒有)以Async的函數,如Handle
同步上下文
對于有的異常處理,如果需要同步上下文,則需要在ExecuteAsync函數中設置continueOnCapturedContext參數為false。
var policy = Policy.Handle<TimeoutException>().RetryAsync(3); await policy.ExecuteAsync(DoSomethingAsync, CancellationToken.None, continueOnCapturedContext: true);
Cancellation的支持
Polly也支持系統的co-operative cancellation框架,在重試,超時,bulkhead等策略中通知執行的委托,從而影響其行為。要使用Cancellation,需要在Excute的時候傳入CancellationToken。
policy.Execute(ct => { ct.ThrowIfCancellationRequested(); }, cts.Token);
具體示例在介紹彈性策略的超時策略時有介紹到。
原文鏈接:https://www.cnblogs.com/TianFang/p/8215736.html
相關推薦
- 2022-04-05 如何定義多個context:property-placeholder配置
- 2022-05-12 Kotlin filter 根據條件過濾數組
- 2022-07-11 EasyExcel實現追加寫入文件
- 2022-12-07 python中的eval函數使用實例_python
- 2022-07-27 詳解Python中的PyInputPlus模塊_python
- 2022-07-26 圖解Elasticsearch 獲取兩個索引數據不同之處的四種方案
- 2023-10-16 Echart 數據更新了,X軸或者Y軸顯示不變化的問題
- 2022-06-11 詳解如何在Flutter中獲取設備標識符_Android
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支