Jetty WebSocketServlet NPE

时间:2013-07-18 19:33:30

标签: java websocket jetty

我尝试使用jetty web socket并遇到一些麻烦,可能你知道如何修复它们。

有SocketServlet(来自示例):

public class SocketServlet extends WebSocketServlet {

  @Override
  public void configure(WebSocketServletFactory factory) {
      factory.register(MyEchoSocket.class);
  }
}

和EchoSocket

@WebSocket
public class MyEchoSocket {

   @OnWebSocketConnect
   public void onConnect(Session session){
       System.out.println("Connect");
   }
}

web.xml(部分)

<servlet>
    <servlet-name>SocketServlet</servlet-name>
    <servlet-class>ru.servlets.SocketServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>SocketServlet</servlet-name>
    <url-pattern>/hello/*</url-pattern>
</servlet-mapping>

当我用maven构建并运行它时,我有NPE:

2013-07-18 23:20:29.488:警告:oejs.ServletHandler:/你好 显示java.lang.NullPointerException     在org.eclipse.jetty.websocket.server.WebSocketServerFactory.upgrade(WebSocketServerFactory.java:415)     在org.eclipse.jetty.websocket.server.WebSocketServerFactory.acceptWebSocket(WebSocketServerFactory.java:178)     在org.eclipse.jetty.websocket.servlet.WebSocketServlet.service(WebSocketServlet.java:160)     在javax.servlet.http.HttpServlet.service(HttpServlet.java:848)     在org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:669)     在org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:457)     在org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:137)     在org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:557)     在org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:231)     在org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1075)     在org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:384)     在org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:193)     在org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1009)     在org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:135)     at org.eclipse.jetty.server.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:255)     在org.eclipse.jetty.server.handler.HandlerCollection.handle(HandlerCollection.java:154)     在org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:116)     在org.eclipse.jetty.server.Server.handle(Server.java:368)     at org.eclipse.jetty.server.AbstractHttpConnection.handleRequest(AbstractHttpConnection.java:489)     at org.eclipse.jetty.server.AbstractHttpConnection.headerComplete(AbstractHttpConnection.java:942)     at org.eclipse.jetty.server.AbstractHttpConnection $ RequestHandler.headerComplete(AbstractHttpConnection.java:1004)     在org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:640)     在org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:235)     at org.eclipse.jetty.server.AsyncHttpConnection.handle(AsyncHttpConnection.java:82)     在org.eclipse.jetty.io.nio.SelectChannelEndPoint.handle(SelectChannelEndPoint.java:628)     在org.eclipse.jetty.io.nio.SelectChannelEndPoint $ 1.run(SelectChannelEndPoint.java:52)     在org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:608)     在org.eclipse.jetty.util.thread.QueuedThreadPool $ 3.run(QueuedThreadPool.java:543)     在java.lang.Thread.run(Thread.java:724)

我用 码头版本9.0.4.v20130625 jettyPlugin.version 8.1.10.v20130312 java 1.7.25

谢谢你的回答!

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题,这只是由HttpConnection中的静态方法引起的,它会生成一个threadLocale。它使用了initValue()方法,但我无法找到为什么这个方法很难在短时间内返回null 我们可以发现所有这些都发生在tomcat下。

当我改为在码头上运行时,一切顺利。

因此我们可以认识到jetty的websocket模块不是独立的,而是基于jetty服务器。我们最好实现一个websocketServlet。