2012-10-06 4 views
4

재생 2를 사용하여 외부 API에 대해 여러 웹 요청을 만들고 싶습니다. 각 웹 요청은 page 매개 변수에 따라 달라집니다. 내 코드는 다음과 같습니다게임 2에서 여러 HTTP 요청을 만드는 방법은 무엇입니까?

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(); 
       } 
      })); 
} 

그것은 페이지 1 작동하지만, 그래서 내가 제대로 result 비동기 요청 방법을 구축하고 있지 않다 의심되는 2 페이지 나에게 internall 오류가 있습니다. 내가 이것을 관리자로부터 실행하고 있기 때문에 나는이 비동기식을 필요로하지 않는다는 것에주의하십시오. 그리고이 모든 요청들이 분석 될 때까지 기다릴 수는 있습니다. 그러나 이것을 위해 연극 2에서 동기화 방법을 찾지 못했습니다. 내가 뭘 잘못하고 있는지 알려주시겠습니까?

답변

5

외부 WS 호출을 동 기적으로 만들려면 get() 약속 방법을 사용하십시오. 예를 들어

:

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

고마워요! 이것은 나를 도왔다! 내가 왜 문서에서 이것을 찾을 수 없었는지 모르겠다. :) –

관련 문제