So I have a web-app in Spring Boot, and there is a part where I make many HTTP requests to an API, and it seems to timeout if too many requests are made. I heard that switching from Synchronous to Asynchronous requests might help this issue.
Using OkHttp, this is what my Synchronous GET request looks like:
private JSONObject run(String url) throws Exception {
Request newRequest = new Request.Builder()
.url(url)
.addHeader("Authorization", token)
.build();
try (Response response = client.newCall(newRequest).execute()) {
if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
return new JSONObject(response.body().string());
}
}
I return the response as a JSON object from parsing the response body. However, in trying to use an OkHttp asynchronous call, it seems that I can't use the same approach. This is what I have so far:
public void runAsync(String url) throws Exception {
Request request = new Request.Builder()
.url(url)
.addHeader("Authorization", token)
.build();
client.newCall(request).enqueue(new Callback() {
@Override public void onFailure(Call call, IOException e) {
e.printStackTrace();
}
@Override public void onResponse(Call call, Response response) throws IOException {
if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
System.out.println(response.body().string());
}
});
}
I cannot simply return the result as a JSON, as the response is wrapped inside of the Callback method, which has a return value of void. Any ideas on how I might achieve similar results to my Synchronous GET of extracting the response?