我正在编写一些重新连接逻辑,以周期性地尝试与远程端点建立连接(该端点已经关闭)。基本上,代码看起来像这样:
public void establishConnection() {
try {
this.connection = newConnection();
} catch (IOException e) {
// connection failed, try again.
try { Thread.sleep(1000); } catch (InterruptedException e) {};
establishConnection();
}
}
我已经多次使用类似上面的代码解决了这个普遍问题,但我对结果感到大部分不满意。是否有专为处理此问题而设计的设计模式?