“重试”的设计模式失败的逻辑? [英] Design pattern for "retrying" logic that failed?

查看:94
本文介绍了“重试”的设计模式失败的逻辑?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一些重新连接逻辑,以定期尝试建立一个到远程终端的连接。基本上,代码如下所示:

  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.

这篇关于“重试”的设计模式失败的逻辑?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆