①新建一个重试线程
class RetryThread implements Callable<JSONArray> {
String appType;
String formInstId;
String token;
public RetryThread(String appType, String formInstId, String token) {
this.appType = appType;
this.formInstId = formInstId;
this.token = token;
}
int retryTimes = 0;
JSONArray approveJson = null;
@Override
public JSONArray call() {
do {
try {
approveJson = parseApproveJson(appType, formInstId, token);
if (approveJson.isEmpty() || approveJson.size() <= 1) {
retryTimes++;
if (retryTimes == 1) {
Thread.sleep(5 * 1000);
} else if (retryTimes == 2) {
Thread.sleep(10 * 1000);
} else if (retryTimes == 3) {
Thread.sleep(15 * 1000);
}
} else if (approveJson.size() > 1) {
break;
}
} catch (Exception ex) {
e.printStack();
}
} while (retryTimes <= 3);
return approveJson;
}
}
②新建异步线程池请求
//创建一个线程池对象
ExecutorService pool = Executors.newFixedThreadPool(100);
RetryThread task = new RetryThread(appType, formInstId, token);
Future<JSONArray> future = pool.submit(task);
while (true) {
if (future.isDone()) {
try {
approveApiArray = future.get();
// 获取approveApiArray 处理以下业务逻辑
// TODO
} catch (Exception ex) {
e.printStack();
}
//关闭线程池
pool.shutdown();
break;
}
}