“重试”的设计模式失败的逻辑?

编程入门 行业动态 更新时间:2024-10-08 18:37:18
本文介绍了“重试”的设计模式失败的逻辑?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我正在写一些重新连接逻辑,以定期尝试建立一个到远程终端的连接。基本上,代码如下所示:

public void establishConnection(){ try { this。 connection = newConnection(); } catch(IOException e){ //连接失败,再试一次。 try {Thread.sleep(1000); } catch(InterruptedException e){}; 建立连接(); } }

我已经解决了这个一般问题,代码类似于以上在很多场合,但我感到很不满意的结果。有没有设计用于处理这个问题的设计模式?

解决方案

一个值得注销的图书馆是 Sarge ,根据定义的计划自动执行重试。

I'm writing some reconnect logic to periodically attempt to establish a connection to a remote endpoint which went down. Essentially, the code looks like this:

public void establishConnection() { try { this.connection = newConnection(); } catch (IOException e) { // connection failed, try again. try { Thread.sleep(1000); } catch (InterruptedException e) {}; establishConnection(); } }

I've solved this general problem with code similar to the above on many occasions, but I feel largely unsatisfied with the result. Is there a design pattern designed for dealing with this issue?

解决方案

One library worth checkout out is Sarge, which automatically performs retries according to a defined plan.

更多推荐

“重试”的设计模式失败的逻辑?

本文发布于:2023-11-26 07:42:30,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1633230.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:重试   逻辑   模式

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!