烙印99
写个简单实现:
private static Map> futures = new HashMap<>();
@RequestMapping(value = "ehr", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON)
@ResponseBody
public User getUser(HttpServletRequest request, HttpServletResponse response) {
String sessionID = request.getSession().getId();
User u = new User();
u.setMsg("未完成");
if(futures.containsKey(sessionID)) {
Future userFuture = futures.get(sessionID);
if(userFuture.isDone()) {
try {
u = userFuture.get();
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
futures.remove(sessionID);
}
} else {
Future future = poolTaskExecutor.submit(ehrDownloadTask);
futures.put(sessionID,future);
}
return u;
}
前台轮训直到正确返回。Key你可以用我这样的SessionID,也可以自己根据逻辑生成如果是分布式Tomcat可以用Redis代替HashMap