public static void main(String[] args) throws Exception {
int maxRetryTimes = 4; // 最大重试次数
int retryTimes = 0; // 当前重试次数
while (retryTimes < maxRetryTimes) {
try {
String s = HttpUtils.sendPostYp("http://127.0.0.1:8085/api/v1/DataBoardAPI/test", null, null);
JSONObject jsonObject = JSONObject.parseObject(s);
if (!"200".equals(jsonObject.getString("status"))){
throw new Exception(jsonObject.getString("message"));
}
break; // 成功发送请求,跳出重试循环
} catch (Exception e) {
retryTimes++; // 捕获到异常,重试次数加1
if (retryTimes >= maxRetryTimes) {
throw e; // 达到重试上限后仍然失败,抛出异常
}
// 如果未达到重试上限,等待一段时间后进行重试
Thread.sleep(1000); // 1秒
System.out.println(e.getMessage()+ "435334");
}
}
}
java重发机制
最新推荐文章于 2024-07-13 15:29:04 发布