I Have some method inside my Controller which executes @Async
task
我在Controller中有一些执行@Async任务的方法
@Async
public Future<String> getResultFromServer(){
String result = ......
return new AsyncResult<String>(result);
}
The method execution time is up to 1o minutes. All I need to do is just to return result to client side which will be connected using AJAX/JQuery.
方法执行时间最长为1分钟。我需要做的就是将结果返回给客户端,使用AJAX / JQuery进行连接。
I don't want the client to request my server every second whether @Async
method executed or not. I just want to keep my connection open and then just "push" result to Server.
我不希望客户端每秒都请求我的服务器是否执行@Async方法。我只想保持连接打开,然后将结果“推送”到服务器。
@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 very close to Comit model, but I'm not familiar with comet in general.
我明白这与Comit模型非常接近,但我对彗星一般都不熟悉。
My question is how can I keep connection open on client side using JavaScript/JQuery?
我的问题是如何使用JavaScript / JQuery在客户端保持连接打开?
and will my @RequestMapping(value="/async.do", method=RequestMethod.POST)
method push result to the client?
并且我的@RequestMapping(value =“/ async.do”,method = RequestMethod.POST)方法会将结果推送到客户端吗?
1 个解决方案
#1
2
The easiest way would be not to invoke the method in a asynchronous way, instead invoke it directly from the controller in a synchronous way.
最简单的方法是不以异步方式调用方法,而是以同步方式直接从控制器调用它。
Then the request will need to "wait" until the method result is calculated, and it can be returned as soon as it is created.
然后,请求将需要“等待”,直到计算出方法结果,并且可以在创建后立即返回。
Of course this means, that the connection will be open for how long it takes do the job (1 minute).
当然这意味着,连接将打开工作需要多长时间(1分钟)。
#1
2
The easiest way would be not to invoke the method in a asynchronous way, instead invoke it directly from the controller in a synchronous way.
最简单的方法是不以异步方式调用方法,而是以同步方式直接从控制器调用它。
Then the request will need to "wait" until the method result is calculated, and it can be returned as soon as it is created.
然后,请求将需要“等待”,直到计算出方法结果,并且可以在创建后立即返回。
Of course this means, that the connection will be open for how long it takes do the job (1 minute).
当然这意味着,连接将打开工作需要多长时间(1分钟)。