在最短的时间内进行多次 API 调用并返回组合响应

编程入门 行业动态 更新时间:2024-10-22 11:26:06
本文介绍了在最短的时间内进行多次 API 调用并返回组合响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有 10 个健康检查 URL,它们只是获取服务我像下面这样循环击打它们

I have 10 health check URLs which are simply get service I am hitting them in a loop like below

for(int i=0;i<10;i++){ Response response = given().when().relaxedHttpsValidation().get(url[i]); list.add(response); } return list;

现在的问题是它串联命中 API 并等待所有响应,我只想并行命中所有 API 但合并结果,我尝试使用线程但无法了解如何合并响应在多线程的情况下

Now the problem is it hits API in series and waiting for a response for all, I just want to hit all API in parallel but combine the result, I tried using threads but unable to get an idea on how to combine the response in case of multi-threading

推荐答案

感谢您的快速回复,我现在只想分享我是如何实现的

Thank you for your quick response i just want to share now how i achieved it

List responseList = new ArrayList(); ExecutorService exec = Executors.newFixedThreadPool(10); for (int i = 0; i < 10; i++) { exec.submit(new Runnable() { public void run() { String response = executeServiceCall(urlArray[i]); responseList.add(response); } }); } exec.shutdown(); try { exec.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS); } catch (InterruptedException e) { LOGGER.error(e.toString()); } LOGGER.info("response list is " + responseList)

更多推荐

在最短的时间内进行多次 API 调用并返回组合响应

本文发布于:2023-11-27 07:21:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1637111.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:组合   时间内   最短   API

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!