I have some method inside my controller that does the task @Async
@Async
public Future<String> getResultFromServer(){
String result = ......
return new AsyncResult<String>(result);
}
The execution time of the method is up to 1 minute. All I need to do is simply return the result to the client side, which will be connected using AJAX / JQuery.
I do not want the client to request my server every second, regardless of whether the method was executed @Asyncor not. I just want my connection to open, and then just “push” the result to the server.
@RequestMapping(value="/async.do", method=RequestMethod.POST)
public void getResult(HttpServletResponse res){
String result = null;
PrintWriter out = res.getWriter();
Future<String> future = getResultFromServer();
try {
if (future.isDone())
result = future.get();
out.println(result);
out.close();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
I understand that this is very close to the Comit model, but I am not familiar with comets in general.
My question is, how can I open a client-side connection using JavaScript / JQuery?
and my method @RequestMapping(value="/async.do", method=RequestMethod.POST)pushes the result to the client?