为 Spring Boot 驱动的 Vaadin 应用程序启用服务器推送不起作用

Posted

技术标签:

【中文标题】为 Spring Boot 驱动的 Vaadin 应用程序启用服务器推送不起作用【英文标题】:Enabling server-push for a Spring Boot powered Vaadin application does not work 【发布时间】:2014-08-07 12:04:22 【问题描述】:

在我们当前的项目中,我们希望在 Spring Boot 驱动的 Vaadin 应用程序中使用 server-push。

我们按照 wiki 文章 https://vaadin.com/book/vaadin7/-/page/advanced.push.html 中的说明进行操作(向 pom.xml 添加了 vaadin-push 依赖项,向 out UI 类添加了 @Push 注释,没有在 web.xml 文件中设置异步支持,因为它没有在这种情况下存在)并收到以下错误消息:

2014-06-17 14:32:43,340 [http-nio-8080-exec-3] WARN  org.atmosphere.cpr.AtmosphereFramework - SessionSupport error. Make sure you define org.atmosphere.cpr.SessionSupport as a listener in web.xml instead
2014-06-17 14:32:44,981 [http-nio-8080-exec-3] WARN  org.atmosphere.cpr.DefaultAsyncSupportResolver - Found multiple containers, please specify which one to use: org.atmosphere.container.Tomcat7CometSupport, org.atmosphere.container.TomcatCometSupport,  until you do, Atmosphere will use:class org.atmosphere.container.Tomcat7CometSupport
2014-06-17 14:32:45,001 [http-nio-8080-exec-3] WARN  org.atmosphere.cpr.AtmosphereFramework - Failed using comet support: org.atmosphere.container.Tomcat7CometSupport, error: Tomcat failed to detect this is a Comet application because context.xml is missing or the Http11NioProtocol Connector is not enabled.You must use the atmosphere-native-runtime dependency in order to use native Comet Support
If that's not the case, you can also remove META-INF/context.xml and WEB-INF/lib/atmosphere-compat-tomcat7.jar Is the NIO or APR Connector enabled?
2014-06-17 14:32:45,005 [http-nio-8080-exec-3] ERROR org.atmosphere.cpr.AtmosphereFramework - If you have more than one Connector enabled, make sure they both use the same protocol, e.g NIO/APR or HTTP for all. If not, org.atmosphere.container.BlockingIOCometSupport will be used and cannot be changed.
java.lang.IllegalStateException: Tomcat failed to detect this is a Comet application because context.xml is missing or the Http11NioProtocol Connector is not enabled.You must use the atmosphere-native-runtime dependency in order to use native Comet Support
If that's not the case, you can also remove META-INF/context.xml and WEB-INF/lib/atmosphere-compat-tomcat7.jar
    at org.atmosphere.container.Tomcat7CometSupport.<clinit>(Tomcat7CometSupport.java:56) ~[atmosphere-runtime-2.1.2.vaadin2.jar:2.1.2.vaadin2]
    (...)
2014-06-17 14:32:45,006 [http-nio-8080-exec-3] WARN  org.atmosphere.cpr.AtmosphereFramework - Using org.atmosphere.container.BlockingIOCometSupport

2014-06-17 14:32:58,684 [http-nio-8080-exec-1] ERROR com.vaadin.server.DefaultErrorHandler - 
java.lang.IllegalStateException: Cannot suspend a response longer than the session timeout. Increase the value of session-timeout in web.xml
    at org.atmosphere.cpr.AtmosphereResourceImpl.suspend(AtmosphereResourceImpl.java:314) ~[atmosphere-runtime-2.1.2.vaadin2.jar:2.1.2.vaadin2]
    (...)

在阅读 *** 线程 Getting rid of web.xml in Vaadin 7 with VaadinServlet 后,我们在 UI 类中添加了一个静态 servlet

@WebServlet(asyncSupported = true)
public static class Servlet extends VaadinServlet 

并得到相同的错误消息:

2014-06-17 14:33:52,928 [http-nio-8080-exec-1] WARN  org.atmosphere.cpr.AtmosphereFramework - SessionSupport error. Make sure you define org.atmosphere.cpr.SessionSupport as a listener in web.xml instead
2014-06-17 14:33:54,557 [http-nio-8080-exec-1] WARN  org.atmosphere.cpr.DefaultAsyncSupportResolver - Found multiple containers, please specify which one to use: org.atmosphere.container.Tomcat7CometSupport, org.atmosphere.container.TomcatCometSupport,  until you do, Atmosphere will use:class org.atmosphere.container.Tomcat7CometSupport
2014-06-17 14:33:54,577 [http-nio-8080-exec-1] WARN  org.atmosphere.cpr.AtmosphereFramework - Failed using comet support: org.atmosphere.container.Tomcat7CometSupport, error: Tomcat failed to detect this is a Comet application because context.xml is missing or the Http11NioProtocol Connector is not enabled.You must use the atmosphere-native-runtime dependency in order to use native Comet Support
If that's not the case, you can also remove META-INF/context.xml and WEB-INF/lib/atmosphere-compat-tomcat7.jar Is the NIO or APR Connector enabled?
2014-06-17 14:33:54,581 [http-nio-8080-exec-1] ERROR org.atmosphere.cpr.AtmosphereFramework - If you have more than one Connector enabled, make sure they both use the same protocol, e.g NIO/APR or HTTP for all. If not, org.atmosphere.container.BlockingIOCometSupport will be used and cannot be changed.
java.lang.IllegalStateException: Tomcat failed to detect this is a Comet application because context.xml is missing or the Http11NioProtocol Connector is not enabled.You must use the atmosphere-native-runtime dependency in order to use native Comet Support
If that's not the case, you can also remove META-INF/context.xml and WEB-INF/lib/atmosphere-compat-tomcat7.jar
    at org.atmosphere.container.Tomcat7CometSupport.<clinit>(Tomcat7CometSupport.java:56) ~[atmosphere-runtime-2.1.2.vaadin2.jar:2.1.2.vaadin2]
   (...)
2014-06-17 14:33:54,582 [http-nio-8080-exec-1] WARN  org.atmosphere.cpr.AtmosphereFramework - Using org.atmosphere.container.BlockingIOCometSupport

2014-06-17 14:34:07,338 [http-nio-8080-exec-3] ERROR com.vaadin.server.DefaultErrorHandler - 
java.lang.IllegalStateException: Cannot suspend a response longer than the session timeout. Increase the value of session-timeout in web.xml
    at org.atmosphere.cpr.AtmosphereResourceImpl.suspend(AtmosphereResourceImpl.java:314) ~[atmosphere-runtime-2.1.2.vaadin2.jar:2.1.2.vaadin2]
    (...)

为会话超时添加一个参数也没有改变任何东西:

@WebServlet(asyncSupported = true, initParams =  @WebInitParam(name = "session-timeout", value = "120") )
public static class Servlet extends VaadinServlet 


2014-06-17 14:36:18,636 [http-nio-8080-exec-1] WARN  org.atmosphere.cpr.AtmosphereFramework - SessionSupport error. Make sure you define org.atmosphere.cpr.SessionSupport as a listener in web.xml instead
2014-06-17 14:36:20,284 [http-nio-8080-exec-1] WARN  org.atmosphere.cpr.DefaultAsyncSupportResolver - Found multiple containers, please specify which one to use: org.atmosphere.container.Tomcat7CometSupport, org.atmosphere.container.TomcatCometSupport,  until you do, Atmosphere will use:class org.atmosphere.container.Tomcat7CometSupport
2014-06-17 14:36:20,303 [http-nio-8080-exec-1] WARN  org.atmosphere.cpr.AtmosphereFramework - Failed using comet support: org.atmosphere.container.Tomcat7CometSupport, error: Tomcat failed to detect this is a Comet application because context.xml is missing or the Http11NioProtocol Connector is not enabled.You must use the atmosphere-native-runtime dependency in order to use native Comet Support
If that's not the case, you can also remove META-INF/context.xml and WEB-INF/lib/atmosphere-compat-tomcat7.jar Is the NIO or APR Connector enabled?
2014-06-17 14:36:20,307 [http-nio-8080-exec-1] ERROR org.atmosphere.cpr.AtmosphereFramework - If you have more than one Connector enabled, make sure they both use the same protocol, e.g NIO/APR or HTTP for all. If not, org.atmosphere.container.BlockingIOCometSupport will be used and cannot be changed.
java.lang.IllegalStateException: Tomcat failed to detect this is a Comet application because context.xml is missing or the Http11NioProtocol Connector is not enabled.You must use the atmosphere-native-runtime dependency in order to use native Comet Support
If that's not the case, you can also remove META-INF/context.xml and WEB-INF/lib/atmosphere-compat-tomcat7.jar
    at org.atmosphere.container.Tomcat7CometSupport.<clinit>(Tomcat7CometSupport.java:56) ~[atmosphere-runtime-2.1.2.vaadin2.jar:2.1.2.vaadin2]
    (...)
2014-06-17 14:36:20,308 [http-nio-8080-exec-1] WARN  org.atmosphere.cpr.AtmosphereFramework - Using org.atmosphere.container.BlockingIOCometSupport

2014-06-17 14:36:44,341 [http-nio-8080-exec-9] ERROR com.vaadin.server.DefaultErrorHandler - 
java.lang.IllegalStateException: Cannot suspend a response longer than the session timeout. Increase the value of session-timeout in web.xml
    at org.atmosphere.cpr.AtmosphereResourceImpl.suspend(AtmosphereResourceImpl.java:314) ~[atmosphere-runtime-2.1.2.vaadin2.jar:2.1.2.vaadin2]
    (...)

您对解决方案有什么想法吗?

【问题讨论】:

因为这个帖子已经有一年了:你还有这个问题吗?我正在使用带有@Push 的 vaadin-spring-boot 没有任何问题 【参考方案1】:

目前,Spring + Vaadin 仅在您未将 WEBSOCKET 设置为传输模式时才有效。尝试将其更改为长轮询或流式传输:

@Push(transport = Transport.LONG_POLLING)

就我个人而言,我仍然为 Web 应用程序使用旧的 xml 配置。这是 Vaadin 7 的推送配置的一部分。

 <!-- Vaadin context -->
<servlet>
    <servlet-name>ISM Application</servlet-name>
    <servlet-class>org.vaadin.spring.servlet.SpringAwareVaadinServlet</servlet-class>
    <init-param>
        <param-name>closeIdleSessions</param-name>
        <param-value>true</param-value>
    </init-param>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:ism-web.xml</param-value>
    </init-param>
    <!-- Vaadin 7.1 @Push -->
    <init-param>
        <param-name>org.atmosphere.useWebSocketAndServlet3</param-name>
        <param-value>true</param-value>
    </init-param>
    <async-supported>true</async-supported>
</servlet>

【讨论】:

问题是我们使用Spring Boot,所以默认没有web.xml文件(创建web.xml文件很容易,但我不知道如何确保它被使用)。不幸的是,分别将 @Push(transport = Transport.LONG_POLLING) @Push(transport = Transport.STREAMING) 添加到 UI 类并没有改变任何东西。我还在@WebServlet 注释中添加了“@WebInitParam(name = "org.atmosphere.useWebSocketAndServlet3", value = "true")"。也没有成功。 :-( 查看 vaadin4spring 来源:github.com/peholmst/vaadin4spring Vaadin4spring 即将推出 Vaadin 官方插件。我很好奇你为什么使用弹簧靴?你可以在没有弹簧靴的情况下做所有这些事情。 最后,我记得注解配置必须在扩展 VaadinServlet 的 servlet 类中声明。 我们的项目已经包含了 vaadin4spring (org.vaadin.spring - spring-boot-vaadin)。我想我应该提到这一点。我知道服务器推送不需要 Spring Boot。我们使用它来启动一个嵌入式 Tomcat 服务器来托管我们的应用程序。 我该如何解释您的第二条评论?正如您在我的问题中看到的那样,我已经尝试了一个扩展 VaadinServlet 的内联 servlet。【参考方案2】:

观看以下 Vaadin 网络研讨会后,我从服务器推送切换到轮询,这解决了我的问题:

http://www.youtube.com/watch?v=GOKX-bGmi0k http://www.youtube.com/watch?v=ZUshNi_4Gqc

以下 wiki 描述了如何使用轮询:

https://vaadin.com/wiki/-/wiki/Main/Using+polling

【讨论】:

以上是关于为 Spring Boot 驱动的 Vaadin 应用程序启用服务器推送不起作用的主要内容,如果未能解决你的问题,请参考以下文章

带有 Spring MVC 的 Vaadin 8 Spring Boot QuickTickets 仪表板

Vaadin 10没有在Spring-Boot中使用模板

Vaadin 8(没有 Spring Boot/Security)和 Keycloak 不起作用

使用Maven和Spring Boot的Vaadin自定义组件/小部件

如何使 Spring Boot 适配器中的 Keycloak 策略执行器与 vaadin 一起使用

在基于 SPRING-BOOT JSP 的 web 应用程序中嵌入 VAADIN UI 的 HTTP 405