How to make multiple HTTP requests in game 2?

I want to make some web requests for an external api using game 2. Each web request will be different from the page parameter. My code is:

 static WSRequestHolder urlPaging = WS .url("http://my_api") .setQueryParameter("apiKey", "api_key") .setQueryParameter("pageSize", "5") .setQueryParameter("format", "json"); public static Result insertProducts() { int totalPages = 83; Logger.info("total pages: " + totalPages); for (int currentPage = 1; currentPage < totalPages; currentPage++) { Logger.info("--current page:" + currentPage); result(currentPage); } return ok(); } public static AsyncResult result(int currentPage) { return async(urlPaging .setQueryParameter("page", String.valueOf(currentPage)).get() .map(new Function<WS.Response, Result>() { public Result apply(WS.Response response) { insertProductsFromPage(response); return ok(); } })); } 

It works on page 1, but gives me an internal error for page 2, so I suspect that I am not building the result the asynchronous request method. Please note that I do not need this asynchronous mode, because I run it from the administrator, and I can wait there until all these requests are analyzed, but I did not find a way to synchronize in game 2 for this. Could you tell me what I am doing wrong?

+4
source share
1 answer

If you want to make external WS calls synchronously, just use the get () method.

For instance:

 Promise<WS.Response> promise = urlPaging.setQueryParameter("page", String.valueOf(currentPage)).get(); WS.Response response = promise.get(); // wait for the result of the promise. 
+5
source

Source: https://habr.com/ru/post/1438147/


All Articles