




全文預(yù)覽已結(jié)束
下載本文檔
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
背景介紹在實(shí)際工作過程中,重試是一個(gè)經(jīng)常使用的手段。比如MQ發(fā)送消息失敗,會(huì)采取重試手段,比如工程中使用RPC請(qǐng)求外部服務(wù),可能因?yàn)榫W(wǎng)絡(luò)波動(dòng)出現(xiàn)超時(shí)而采取重試手段.可以看見重試操作是非常常見的一種處理問題,系統(tǒng)設(shè)計(jì)的手段而在之前我們項(xiàng)目中處理重拾操作依賴MQ自身的重試機(jī)制,但是這種機(jī)制不是很靈活,如果某些功能沒有使用MQ的話,那么就不是那么方便了,而本文介紹的Spring-Retry卻能夠以一種很優(yōu)雅的方式解決這種問題,當(dāng)然目前版本的Spring-retry還不是完美的,還是有待改進(jìn)的.不過已經(jīng)很不錯(cuò)了.基本使用例子1ConfigurationEnableRetrypublic class Application Bean public Service service() return new Service(); Serviceclass Service Retryable(RemoteAccessException.class) public void service() / . do something Recover public void recover(RemoteAccessException e) / . panic 例子2org.springframework.stereotype.Servicepublic class Service1 Retryable(value = RemoteAccessException.class, RuntimeException.class, maxAttempts = 2, backoff = Backoff(value = 2000) public void service() System.out.println(do some things); / this exception will just trigger recover1, do not trigger recover3 throw new RemoteAccessException(remote access exception); / this exception will just trigger recover2/ throw new RuntimeException(runtime exception);/ System.out.println(do another things); / 如果使用注解的話,這個(gè)recover貌似只能寫在本類中,我測(cè)試了如果將recover方法寫在 / recoverService中,好像找不到 Recover public void recover1(RemoteAccessException e) System.out.println(e.getMessage(); System.out.println(do recover operation1); Recover public void recover2(RuntimeException e) System.out.println(e.getMessage(); System.out.println(do recover operation2); Recover public void recover3(RemoteAccessException e) System.out.println(e.getMessage(); System.out.println(do recover operation3); 例子3Servicepublic class Service2 public void test() final RetryTemplate retryTemplate = new RetryTemplate(); final SimpleRetryPolicy policy = new SimpleRetryPolicy(3, Collections.Class, Boolean singletonMap(Exception.class, true); FixedBackOffPolicy fixedBackOffPolicy = new FixedBackOffPolicy(); fixedBackOffPolicy.setBackOffPeriod(100); retryTemplate.setRetryPolicy(policy); retryTemplate.setBackOffPolicy(fixedBackOffPolicy); final RetryCallback retryCallback = new RetryCallback() public Object doWithRetry(RetryContext context) throws Exception System.out.println(do some thing); /設(shè)置context一些屬性,給RecoveryCallback傳遞一些屬性 context.setAttribute(key1, value1); System.out.println(context.getRetryCount(); throw new Exception(exception);/ return null; ; / 如果RetryCallback執(zhí)行出現(xiàn)指定異常, 并且超過最大重試次數(shù)依舊出現(xiàn)指定異常的話,就執(zhí)行RecoveryCallback動(dòng)作 final RecoveryCallback recoveryCallback = new RecoveryCallback() public Object recover(RetryContext context) throws Exception System.out.println(do recory operation); System.out.println(context.getAttribute(key1); return
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2024~2025學(xué)年廣東八年級(jí)下冊(cè)4月期中考試數(shù)學(xué)試題【帶答案】
- 2024~2025學(xué)年福建泉州八年級(jí)下冊(cè)4月期中考試數(shù)學(xué)試題【帶答案】
- 師生關(guān)系構(gòu)建考核試卷
- 濕敷敷料的透氣性對(duì)皮膚水分蒸發(fā)與熱量散失的關(guān)系探討考核試卷
- 清潔生產(chǎn)技術(shù)應(yīng)用考核試卷
- 印刷機(jī)智能調(diào)度系統(tǒng)的生產(chǎn)安全風(fēng)險(xiǎn)評(píng)估與管理技術(shù)考核試卷
- 自律監(jiān)管與消費(fèi)者權(quán)益保護(hù)考核試卷
- 成本控制與設(shè)計(jì)優(yōu)化考核試卷
- 新型維護(hù)材料在農(nóng)副食品加工中的應(yīng)用考核試卷
- 反浮選技術(shù)在鐵礦石選礦中的應(yīng)用考核試卷
- 【MOOC】算法初步-北京大學(xué) 中國(guó)大學(xué)慕課MOOC答案
- 食品檢驗(yàn)員考試題庫(kù)單選題100道及答案解析
- 鄉(xiāng)鎮(zhèn)污水管道改造施工方案
- 四年級(jí)下冊(cè)道德與法治知識(shí)點(diǎn)
- 人工智能(AI)訓(xùn)練師職業(yè)技能鑒定考試題及答案
- ASTM-D3359-(附著力測(cè)試標(biāo)準(zhǔn))-中文版
- 全國(guó)中小學(xué)生學(xué)籍信息管理系統(tǒng)學(xué)生基本信息采集表(2022修訂版)
- CJT 211-2005 聚合物基復(fù)合材料檢查井蓋
- 云南省曲靖市2023-2024學(xué)年八年級(jí)下學(xué)期期末語文試題
- DZ∕T 0212.4-2020 礦產(chǎn)地質(zhì)勘查規(guī)范 鹽類 第4部分:深藏鹵水鹽類(正式版)
- 借款利息確認(rèn)書
評(píng)論
0/150
提交評(píng)論