Tomcat 8和Websocket [英] Tomcat 8 and Websocket

查看:699
本文介绍了Tomcat 8和Websocket的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难将应用程序添加到tomcat8.我正在使用websocket和spring 4,但是我不想使用spring内部的STOMP机制,因此我决定遵循

i've troubles getting my application to tomcat 8. I'm using websocket and spring 4 but i don't want to use the spring internal STOMP mechanism so i've decided to follow this tutorial and implemented my websocket routines my way. I'm developing since a couple of weeks now and tested it always with jetty (maven jetty plugin) and everything works fine. But now i want to deploy my application to our production server running tomcat 8.0.15 on java 8 and CentOS but it's not working.

这是源代码:

@WebListener
public class MyApplication implements ServletContextListener {

private final static String SERVER_CONTAINER_ATTRIBUTE = "javax.websocket.server.ServerContainer";

@Override
public void contextInitialized(ServletContextEvent sce) {

    ServletContext container = sce.getServletContext();

    final ServerContainer serverContainer = (ServerContainer) container.getAttribute(SERVER_CONTAINER_ATTRIBUTE);
    try {
        serverContainer.addEndpoint(new MyEndpointConfig(MyEndpoint.class, "/wstest"));
    } catch (DeploymentException e) {
        e.printStackTrace();
    }
}
}

这是错误:

java.lang.ClassCastException: org.apache.tomcat.websocket.server.WsServerContainer cannot be cast to javax.websocket.server.ServerContainer
at my.package.contextInitialized(MyApplication.java:23)

第23行是我对ServerContainer进行强制转换的地方. 我猜"container.getAttribute(SERVER_CONTAINER_ATTRIBUTE)"返回null,因此强制转换失败,但是为什么?

Line 23 is where i do the cast to ServerContainer. I guess that "container.getAttribute(SERVER_CONTAINER_ATTRIBUTE)" returns null and therefore the cast fails, but why??

Jetty 9.2.3一切正常.我还对它进行了测试,并安装了本地tomcat 8(最新的8.0.18)和Windows 7上的最新JDK 8,并且具有相同的行为.

It's all working fine with Jetty 9.2.3. I've tested it also with a local tomcat 8 installed (newest 8.0.18) and the latest JDK 8 on Windows 7 and same behaviour.

您有任何解决办法的想法吗?

Do you have any ideas how to fix this?

非常感谢您!

推荐答案

Appleman1234指向了一个非常有用的错误报告.请参阅Iyad Elian评论:

Appleman1234 is pointing to a very useful bug report. See Iyad Elian comment:

仅供参考,我发现了为什么异常发生的原因像javax.servlet-api这样的javax.websocket-api需要在运行时在tomcat中排除. Jetty更喜欢使用应用程序类加载器,但是这不是问题.

FYI, I found why the exception happens javax.websocket-api like javax.servlet-api needs to be excluded at runtime in tomcat. jetty prefers application classloader to it's own however so it's not a problem.

<dependency>
    <groupId>javax.websocket</groupId>
    <artifactId>javax.websocket-api</artifactId>
    <version>1.0</version>
    <scope>provided</scope>
</dependency>

这就是为什么它不能在tomcat上工作,而在码头上工作的原因.我刚刚添加了

And that's exactly why it was not working on tomcat but on jetty. I've just added

<scope>provided</scope>

,现在工作正常.感谢Appleman1234!

and now it's working fine. Thanks to Appleman1234!

这篇关于Tomcat 8和Websocket的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆