在已经构建响应后异步处理 httpservletrequest

问题描述 投票:0回答:0

我正在尝试实现一个构建成功响应的 servlet 过滤器,并异步处理请求。因此,GET 请求的客户端将需要尽快获得成功状态的响应以及之后要处理的请求(通过其余的过滤器并作为正常请求到达控制器(使用例外,因为客户端已经断开连接,响应将被构建并被忽略。类似于 GET 请求的触发和遗忘,客户端只需要知道请求已发送。

@Override
protected void doFilterInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, FilterChain filterChain) throws ServletException, IOException {
    mimicSuccessfullResponse(httpServletRequest, httpServletResponse);

    executorService.submit(() ->
    {
        try {
            filterChain.doFilter(httpServletRequest, httpServletResponse);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (ServletException e) {
            throw new RuntimeException(e);
        }
    });

}

所以想法是受益于正常流程(过滤器,处理程序适配器+处理程序(控制器)。

根据我的所见所闻,我需要创建自己的实现并将初始请求的数据存储在发出的线程上,因为如果我尝试使用传递给另一个线程上的过滤器的 httpServletRequest,我会得到 null尝试 getQueryString 时(链中另一个过滤器所需的方法)。我不知道我得到 null 的原因,但我认为请求绑定到线程请求。 实现所有方法并缓存初始数据看起来很糟糕并且容易出错,并且可能无法正常工作,因为 servlet 将使用一些输入流。 (我还需要实现假的 HttpServletResponse)

感谢您阅读并试图帮助我。

servlets servlet-filters
© www.soinside.com 2019 - 2024. All rights reserved.