I make Web Application on Google App Engine and I use AsyncURLFetch Api.
But, I cannot get response. Please help me…
Searc.java
private ArrayList<Future<HTTPResponse>> fetchURL() {
ArrayList<Future<HTTPResponse>> futureList = new ArrayList<Future<HTTPResponse>>();
for (SearchResult searchResult : searchResultList) {
if (!searchResult.isEmpty()) {
for (WebPage webPage : searchResult.getWebPageArray()) {
try {
URL url = new URL(
"http://myapi.appspot.com/htmlparser?url="
+ webPage.getPageURL());
URLFetchService us = URLFetchServiceFactory
.getURLFetchService();
futureList.add(us.fetchAsync(url));
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
return futureList;
}
private ArrayList<HTTPResponse> getFetchResult(
ArrayList<Future<HTTPResponse>> futureList) {
ArrayList<HTTPResponse> responseList = new ArrayList<HTTPResponse>();
try {
for (Future<HTTPResponse> future : futureList) {
if (future.isDone()) {
log.info("future calls get");
HTTPResponse resp = future.get();
responseList.add(resp);
}
}
} catch (InterruptedException e) {
e.printStackTrace();
log.info(e.getMessage());
} catch (ExecutionException e) {
e.printStackTrace();
log.info(e.getMessage());
}
return responseList;
}
HTMLParser.java
public class HTMLParser extends HttpServlet{
protected static Logger log = LoggerFactory.getLogger(HTMLParser.class);
private PrintWriter writer;
private URL paramURL;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
resp.setContentType("text/html; charset=utf8");
writer = resp.getWriter();
getParameter(req);
viewTest();
}
private void getParameter(HttpServletRequest req){
paramURL = getURL(req);
}
private URL getURL(HttpServletRequest req){
try {
URL url;
url = new URL(req.getParameter("url"));
return url;
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
private void viewTest(){
writer.println(paramURL);
log.info(paramURL.toString());
}}
future.isDone() is “false”
What I do something to get “true” ?
Have you read the documentation on Java Futures?
future.isDone()will not block, and will return False until the future has finished executing. To get the results of all the asynchronous calls, you should callfuture.get()on each one unconditionally.