java - Servlet 3.0 async 支持的缺点

标签 java asynchronous servlet-3.0

在我的应用程序中,我有一个自己开发的框架,它将所有请求映射到一个中央 ControllerServlet(没什么特别的,它具有几乎所有框架的基本功能)。 我必须实现发布者-订阅者消息交换功能,并且我决定使用 Servlet 3.0 的异步方法。

当前的 ControllerServletAuthenticationFilter 没有设置为 asyncSupported。我可以为所有异步处理实现第二个 CotrollerServlet,或者将现有的 ControllerServlet 声明为 asyncSupported=true(即使存在不需要异步处理的请求)。

...对于使用异步功能的应用程序,整个请求处理链必须通过注释或在其部署描述符中设置此属性... - source

从这里我了解到我必须将 AuthenticationFilter 声明为 asyncSupported=true(并且在某些情况下不需要异步)。

将 servlet 和过滤器声明为 asyncSupported=true 是否有任何缺点,即使它们也映射不需要异步处理的请求?会不会有可扩展性问题?

最佳答案

Java Servlet 3.0 specification2.3.3.3 异步处理一章包含提示:

Dispatching from a synchronous servlet to an asynchronous servlet would be illegal. However the decision of throwing an IllegalStateException is differed to the point when the application calls startAsync. This would allow a servlet to either function as a synchronous or an asynchronous servlet.

虽然没有直接说明,但这句话让我相信,对于 JLS 作者而言,一个 servlet 的这种双重同步/异步特性并不是什么不寻常的事情。

另一方面,请查阅容器的文档/源代码。仅仅因为从 JLS 的角度来看它是合法的,并不意味着如果 AsyncContext 没有真正使用,您的容器就没有进行一些会对性能产生负面影响的巧妙优化。否则,如果所有 servlet 都可以简单地在任一模式下工作,那么除了记录之外,这个属性的目的是什么?

关于java - Servlet 3.0 async 支持的缺点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12028078/

相关文章:

multithreading - 如何优化验证异步代码的测试?

c# - Async/await 作为协程的替代品

java - 小服务程序 3.0 : Can't send an asynchronous response?

java - 在 NetBeans 中检测设计时间

java - 如何仅将修改后的类文件自动更新到目标 .jar 文件中?

java - JTable 渲染器不会为单元格着色

java - 谷歌Eclipse插件: App Engine ORM Enhancement?

java - Android java客户端程序使用单独的线程进行套接字IO,找不到使用按钮发送数据的方法

java - Websphere 8.5 和 SpringServletContainerInitializer

spring-mvc - 没有 web.xml 的 Spring Security