本文介绍了“重试"的设计模式失败的逻辑?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在编写一些重新连接逻辑,以定期尝试与发生故障的远程端点建立连接.本质上,代码如下所示:
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?
推荐答案值得一试的图书馆是 Sarge,根据定义的计划自动执行重试.
One library worth checkout out is Sarge, which automatically performs retries according to a defined plan.
更多推荐
“重试"的设计模式失败的逻辑?
发布评论