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

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

问题描述

我正在编写一些重新连接逻辑,以定期尝试与发生故障的远程端点建立连接.本质上,代码如下所示:

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.

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

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