spring mvc 请求异步处理,即时响应

spring mvc 的 controller,对于一个非常耗时的处理,让 controller 先异步返回响应给前端,后台继续慢慢执行完。

@RequestMapping(value = "refreshScore.do", method = RequestMethod.POST)
    public String refreshScore(HttpServletRequest request){
        Map<String, Object> paramMap = DataConvertUtil.convertRequestParam(request);

        try{
            // 刷新
            paramMap.put("year", DateUtil.getCurrentYear()); // SCORE_YEAR
            paramMap.put("month", DateUtil.getMonth(new Date())); // SCORE_MONTH

            CompletableFuture future = CompletableFuture.runAsync(() -> {
                scoreCheckBiz.refreshScore(paramMap);
            }).thenAccept(e->System.out.println(e));//thenAccept接口完成处理结果

        }catch(Exception e){
            e.printStackTrace();
        }

        return "OK!";
    }

 

posted @ 2022-11-10 15:05  moonsoft  阅读(195)  评论(0编辑  收藏  举报