網(wǎng)站首頁 編程語言 正文
執(zhí)行策略
執(zhí)行策略的常見方式是調(diào)用策略的Execute函數(shù)
var policy = Policy.Handle<TimeoutException>().Retry(); policy.Execute(DoSomething);
返回值:
如果有返回值,它也是可以獲取其返回值的:
var result = policy.Execute(DoSomething);
捕獲異常:
如果在策略的執(zhí)行過程中出現(xiàn)了異常,也會(huì)在該函數(shù)中同步拋出來,和直接執(zhí)行該委托行為一致。可以直接通過try-catch處理。
try { policy.Execute(DoSomething); } catch (Exception e) { throw; }
也可以通過ExecuteAndCapture
函數(shù)捕獲異常。
var result = policy.ExecuteAndCapture(DoSomething); if (result.FaultType == null) { Console.WriteLine(result.Result); }
策略上下文
在策略的處理過程中,有一個(gè)上下文對象,可以在回調(diào)函數(shù)中使用:
public static RetryPolicy Retry(this PolicyBuilder policyBuilder, int retryCount, Action<Exception, int, Context> onRetry);
它是一個(gè)IDictionary<string,?object>類型的對象,它在Policy的執(zhí)行過程中都可以使用,如:
Policy.Handle<TimeoutException>().Retry(3, (err, countDown, context) => { var method = context["method"]; ConsoleLogger.WriteLine(method); })
這個(gè)上下文對象可以在應(yīng)用策略的時(shí)候帶入:
policy.Execute(DoSomething, new Context("context") { ["method"] = "PolicyTest" });
依賴注入
Polly也自帶了一個(gè)簡單的DI框架,方便復(fù)用Policy,使用方式如下:
var registry = new PolicyRegistry(); registry.Add("timeout & retry", Policy.Handle<TimeoutException>().Retry(3)); var policy = registry.Get<ISyncPolicy>("timeout & retry"); policy.Execute(DoSomething);
當(dāng)然,也可以使用Autofac等自己喜歡的方式。
線程安全
Policy本身是線程安全的,可以并發(fā)使用同一個(gè)Policy,但如果Policy中執(zhí)行的委托有多線程問題,仍然會(huì)出現(xiàn)異常。
異步的支持
Policy對異步操作也提供了良好的支持,只需要使用相應(yīng)以Async結(jié)尾的函數(shù)即可。
var policy = Policy.Handle<TimeoutException>().RetryAsync(3); await policy.ExecuteAsync(DoSomethingAsync);
PS:有的不需要(也沒有)以Async的函數(shù),如Handle
同步上下文
對于有的異常處理,如果需要同步上下文,則需要在ExecuteAsync函數(shù)中設(shè)置continueOnCapturedContext參數(shù)為false。
var policy = Policy.Handle<TimeoutException>().RetryAsync(3); await policy.ExecuteAsync(DoSomethingAsync, CancellationToken.None, continueOnCapturedContext: true);
Cancellation的支持
Polly也支持系統(tǒng)的co-operative cancellation框架,在重試,超時(shí),bulkhead等策略中通知執(zhí)行的委托,從而影響其行為。要使用Cancellation,需要在Excute的時(shí)候傳入CancellationToken。
policy.Execute(ct => { ct.ThrowIfCancellationRequested(); }, cts.Token);
具體示例在介紹彈性策略的超時(shí)策略時(shí)有介紹到。
原文鏈接:https://www.cnblogs.com/TianFang/p/8215736.html
相關(guān)推薦
- 2022-09-25 自動(dòng)微分----pytorch中的梯度運(yùn)算與反向傳播函數(shù)(預(yù)備知識)
- 2022-07-31 pycharm終端解釋器與Python解釋器配置_python
- 2022-09-04 Go語言異常處理(Panic和recovering)用法詳解_Golang
- 2022-05-23 iOS?Segment帶滑動(dòng)條切換效果_IOS
- 2022-05-09 Python?Matplotlib條形圖之垂直條形圖和水平條形圖詳解_python
- 2022-11-15 如何將Android?Studio卸載干凈_Android
- 2022-08-21 Caffe卷積神經(jīng)網(wǎng)絡(luò)視覺層Vision?Layers及參數(shù)詳解_python
- 2022-09-18 基于WPF實(shí)現(xiàn)用戶頭像選擇器的示例代碼_C#教程
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支