使用方法
Future
代碼演示
public class AsyncQichacha {
@Autowired
private QichachaHttpClient qichachaHttpClient;
@Async("qichacha")
public Future<String> findSpecifiedDataAsync(SearchParameters parameters, QichachaEnum qichachaEnum) throws InterruptedException {
System.out.println(Thread.currentThread().getName() + " 線程啟動");
TimeUnit.SECONDS.sleep(3);
System.out.println(Thread.currentThread().getName() + " 線程結(jié)束");
return new AsyncResult<>(qichachaHttpClient.findByQichacha(parameters, qichachaEnum));
}
}
public class AsyncService {
@Autowired
private AsyncQichacha asyncQichacha;
@Autowired
private QichachaHttpClient qichachaHttpClient;
public void AsyncRequest(SearchParameters parameters, Boolean details) throws InterruptedException, ExecutionException {
List<Future<String>> list = new ArrayList();
QichachaFullData refreshJson = new QichachaFullData();
List<String> courtNoticeDetailList = new ArrayList<>();
/* 企業(yè)詳細信息 */
Future<String> details1 = asyncQichacha.findSpecifiedDataAsync(parameters, QichachaEnum.QICHACHA_DETAILS);
list.add(details1);
/* 開庭公告列表信息 */
Future<String> courtNotice = asyncQichacha.findSpecifiedDataAsync(parameters, QichachaEnum.COURT_NOTICE);
list.add(courtNotice);
List<Future> result = new ArrayList<>();
result.addAll(list);
while (true){
if (list.isEmpty()){
break;
}
for (int i=0; i<list.size(); i++){
if (list.get(i).isDone()){
courtNoticeDetailList.add(list.get(i).get());
list.remove(i--);
}
}
// 每隔1秒 檢測一次
TimeUnit.SECONDS.sleep(1);
}
System.out.println("異步方法全部調(diào)用成功");
System.out.println("其查查獲取的結(jié)果如下: ");
System.out.println(courtNoticeDetailList.toString());
System.out.println("refreshJson: " + refreshJson.toString());
}
}