public void submitFuture(final HttpServletRequest req,final Callable<CompletableFuture> task) throwsException{
final String uri = req.getRequestURI();
final Map<String,String[]> params = req.getParameterMap();
final AsyncContext asyncContext = req.startAsync();
asyncContext.getRequest().setAttribute("uri",uri);
asyncContext.getRequest().setAttribute("params",params);
asyncContext.setTimeout(asyncTimeoutInSeconds*1000);
if(asyncListener != null){
asyncContext.addListener(asyncListener);
}
CompletableFuture future = task.call();
future.thenAccept(result->{
HttpServletResponse resp = (HttpServletResponse)asyncContext.getResponse();
try{
if(result instanceof String){
byte[] butes = result.getBytes("GBK");
resp.setContentType("text/html;charset=gbk");
resp.setContentLength(bytes.length);
resp.getOutputStream().write(bytes);
}else{
write(resp,JSONUtils.toJSON(result));
}
}catch(Throwable e){
resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
try{
LOG.error("get infoerror,uri:{},params:{}",uri,JSONUtils.toJSON(params),e);
}catch(Exception ex){}
}
}).exceptionally(e->{
asyncContext.complete();
return null;
});
}
|