gpt4 book ai didi

java - 如何在Java中并行调用多个rest api?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:32:52 25 4
gpt4 key购买 nike

在 Java 中并行调用多个 api 的最佳方式是什么?我想进行多次休息调用,合并结果并返回一个 jsonArray。我正在使用 ExecutorService 并在我仅从一台客户端电脑访问 url 时获得正确的输出。但是当我从多个客户端访问 url 时,即使我调用相同的 url,每次我都会得到一个不同大小的 jsonArray。

我所做的如下:

ExecutorService executor = Executors.newFixedThreadPool(5); 
resultArray = new JSONArray();
for (Branch branch : Branches) {
Future<Response> response = executor.submit(new Request(branch.getUrl(),
branch.getUserName(), branch.getPassword()));
responseBody = response.get().getResponseBody();
resultArray.put(responseBody);
}
executor.shutdown();
while(!executor.isTerminated()) {

}
return resultArray.toString();


public class Request implements Callable<Response> {

private HttpURLConnection con;
private URL obj;
private String response;

private String url;
private String username;
private String password;

public Request(String url, String username, String password) {
this.url = url;
this.username = username;
this.password = password;
}

@Override
public Response call() {
try {
obj = new URL(url);
con = (HttpURLConnection) obj.openConnection();
String userCredentials = username + ":" + password;
String basicAuth = "Basic " + java.util.Base64.getEncoder().encodeToString(userCredentials.getBytes());
con.setRequestProperty ("Authorization", basicAuth);
con.setRequestMethod("GET");

int responseCode = con.getResponseCode();
if(responseCode == 200) {
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer stringBuffer = new StringBuffer();

while ((inputLine = in.readLine()) != null) {
stringBuffer.append(inputLine);
}
in.close();
response = stringBuffer.toString();
return new Response(responseCode, response);
}
else {
response = "{\"response\":\"some error occurred\"}";
return new Response(responseCode, response);
}

} catch (IOException e) {
response = "{\"output\":\"some error occurred\"}";
return new Response(404, response);
}
}
}

public class Response {

private String responseBody;
private int responseCode;

public Response(int responseCode, String responseBody) {
this.responseBody = responseBody;
this.responseCode = responseCode;
}

public int getResponseCode() {
return responseCode;
}

public String getResponseBody() {
return responseBody;
}
}

最佳答案

请检查一下,

Collection<Callable<Response>> tasks = new ArrayList<>();
for (Branch branch : Branches) {
tasks.add(new Request(branch.getUrl(), branch.getUserName(), branch.getPassword()));
}

int numThreads = Branches.size() > 4 ? 4 : Branches.size();
ExecutorService executor = Executors.newFixedThreadPool(numThreads);
List<Future<Response>> results = executor.invokeAll(tasks);
for(Future<Response> response : results){
responseBody = response.get().getResponseBody();
resultArray.put(responseBody);
}

从 Java 7 开始,我们可以尝试用 ForkJoin Pool 替换 ExecutorService,

Collection<Callable<Response>> tasks = new ArrayList<>();
for (Branch branch : Branches) {
tasks.add(new Request(branch.getUrl(), branch.getUserName(), branch.getPassword()));
}

int numThreads = Branches.size() > 4 ? 4 : Branches.size();
ForkJoinPool pool = new ForkJoinPool(Runtime.getRuntime().availableProcessors());
List<Future<Response>> results = pool.invokeAll(tasks);
for(Future<Response> response : results){
responseBody = response.get().getResponseBody();
resultArray.put(responseBody);
}

注意:- 代码未经测试,几个月前我使用过类似的东西。

关于java - 如何在Java中并行调用多个rest api?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50345971/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com