AsyncHandlerInterceptor提供了一个afterConcurrentHandlingStarted()方法, 这个方法会在Controller方法异步执行时开始执行, 而Interceptor的postHandle方法则是需要等到Controller的异步执行完才能执行
例如
@Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { System.out.println(11111); } @Override public void afterConcurrentHandlingStarted(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { System.out.println(22222); }
@ResponseBody @RequestMapping(value = "/topQuery") public DeferredResult<Object> topQuery(@RequestParam String token, @RequestParam(required = false, defaultValue = "") String tn, @RequestParam(required = false, defaultValue = "baiduhui") String zt) { topQueryCounter.increment(); DeferredResult<Object> dr = new DeferredResult<Object>(); Map<String, Object> extra = Maps.newHashMap(); extra.put(TN_KEY, TN_KEY + "=" + tn); extra.put(ZT_KEY, ZT_KEY + "=" + zt); topQueryService.fetchData(dr, "limit=" + topQueryLimit, 1, new AtomicInteger(0), new ArrayList<BaiduTopQuery>(), extra); return dr; }
afterConcurrentHandlingStarted()方法会在return dr后执行, 而postHandle()方法则需要dr.setResult()以后才会执行