gpt4 book ai didi

spring - 如何在没有 web.xml 的情况下在 Java Config 中定义 Spring 3.2 Async Servlet-Filter

转载 作者:行者123 更新时间:2023-12-04 14:58:14 24 4
gpt4 key购买 nike

使用最新最好的 Spring 3.2.3、Tomcat 7.0.42、Servlet 3.0 容器、Java 7。我们需要为我们的响应做 JSONP,所以我们通过做一个 Servlet 过滤器来实现它,就像这样:

http://jpgmr.wordpress.com/2010/07/28/tutorial-implementing-a-servlet-filter-for-jsonp-callback-with-springs-delegatingfilterproxy/

但目前没有 web.xml ..我们正在使用 Java Annotation Config。

在我们的 @Controller ,我们返回一个 DeferredResult<String> ,然后在我们的 @Service ,由我们的 @Controller 调用,我们有 @Async注解。我们去的原因@Async路由(不是 AysncContext 路由)是因为这是一个“即发即忘”类型的异步操作。但是我确实需要向请求者返回一些表示操作已经开始的 JSON。

@RequestMapping(method = RequestMethod.GET, produces=MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public DeferredResult<String> testing(@RequestParam(value="submit", required=true) String param)
{
final DeferredResult<String> result = new DeferredResult<String>();
service.doIt(param, result);

return result;
}

而在我们的 @Service
@Async
public DeferredResult<String> doIt(String param2, DeferredResult<String> result)
{
log.info("Calling Async doIt Method");
result.setResult("{hello:there}");
}

我被卡住了……我想知道是否有办法添加 异步支持 Java 配置中 Servlet 过滤器的标记?我基于看到这个演示文稿(幻灯片 28):

http://rstoyanchev.github.io/spring-mvc-32-update/#28

其他一些信息:

@EnableAsync在配置上,我们使用 AbstractAnnotationConfigDispatcherServletInitializer 来定义我们的应用程序,但我似乎无法弄清楚如何告诉它使用异步过滤器(仅使用 getServletFilters() 返回我们的 JsonpFilter)。

基本上发生的事情是 JSONP 过滤器没有“包装”任何东西......但我确实看到过滤器第二次被调用,在 TaskExecutor 线程中设置结果之后......但此时响应已经消失...所以,我看到过滤器“获取数据”两次......一次是正确的,当 @Controller方法退出,然后在设置 DeferredResult.setResult() 之后第二次退出。

我们也在我们的 Filter 类中尝试了这个:
@WebFilter(urlPatterns = "/*", asyncSupported = true)

但是那没有用(甚至看起来它甚至根据日志制作了 2 个单独的过滤器……但是,我可能错了)。

如果需要,我们可以切换到 Callable ......这没什么大不了的。我确实看到 DeferredResult 和 Callable 之间在 Spring 知道的线程方面存在一些差异。

如果答案是您需要使用 web.xml,那么使用 web.xml 混合进行 Java 配置的真正好方法是什么?

编辑 1:

基于阅读了一些不同的资源,我发现了一些事情:
  • 来自:Spring Aync 预览博客

  • Servlet Filters

    *All Spring Framework Servlet filter implementations have been modified as necessary to work in asynchronous request processing. As for any other filters, some will work — typically those that do pre-processing, and others will need to be modified — typically those that do post-processing at the end of a request. Such filters will need to recognize when the initial Servlet container thread is being exited, making way for another thread to continue processing, and when they are invoked as part of an asynchronous dispatch to complete processing.**


  • 来自 Spring MVC 引用。文档,我实际上是 RTFM 的:

  • The sequence of events for async request processing with a DeferredResult is the same in principle except it's up to the application to produce the asynchronous result from some thread: (1) Controller returns a DeferredResult and saves it in some in-memory queue or list where it can be accessed, (2) Spring MVC starts async processing, (3) the DispatcherServlet and all configured Filter's exit the request processing thread but the response remains open, (4) the application sets theDeferredResult from some thread and Spring MVC dispatches the request back to the Servlet container, (5) the DispatcherServlet is invoked again and processing resumes with the asynchronously produced result.



    所以,基本上,我知道/知道单独的线程会调用过滤器......这不是让我感到困惑的原因......它是如何确定过滤器是否应该修改响应......你可以'不要查看数据的大小,因为 0 字节可能是正确的“答案”。

    所以,我现在只写 DispatchType = ASYNC

    不太确定这对于长期来说是否正确......但是,它似乎确实解决了这个问题。

    任何其他建议/想法?

    最佳答案

    似乎如果您要扩展 AbstractAnnotationConfigDispatcherServletInitializer,您可以覆盖此方法并设置异步支持:

    @Override
    protected void customizeRegistration(ServletRegistration.Dynamic registration) {
    registration.setInitParameter("dispatchOptionsRequest", "true");
    registration.setAsyncSupported(true);
    }

    关于spring - 如何在没有 web.xml 的情况下在 Java Config 中定义 Spring 3.2 Async Servlet-Filter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17756223/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com