2012-12-17 37 views
2

我在一個需要創建實時Web應用程序的項目上工作。我開發了一個小程序來用Apache Tomcat測試WebSockets,但它不起作用,而是拋出一個AbstractMethodError異常。該servlet編譯並且Web應用程序正常部署。當自定義servlet運行時,Tomcat爲WebSocketServlet.createWebSocketInbound()拋出AbstractMethodError

這是servlet的代碼。

public class TestWebSocket extends WebSocketServlet { 

    private static final long serialVersionUID = 1L; 

    @Override 
    protected boolean verifyOrigin(String origin) { 
     System.out.println("Origin: {}" + origin); 
     return true; 
    } 

    @Override 
    protected StreamInbound createWebSocketInbound(String subProtocol, HttpServletRequest request) { 
     return new WebSocketConnection(); 
    } 

    private static class WebSocketConnection extends MessageInbound { 

     @Override 
     protected void onOpen(WsOutbound outbound) { 
      System.out.println("Conexión abierta"); 
     } 

     @Override 
     protected void onClose(int status) { 
      System.out.println("Conexión cerrada"); 
     } 

     @Override 
     protected void onBinaryMessage(ByteBuffer byteBuffer) throws IOException { 
      System.out.println("No se soportan mensajes binarios"); 
      throw new UnsupportedOperationException("No se soportan mensajes binarios"); 
     } 

     @Override 
     protected void onTextMessage(CharBuffer charBuffer) throws IOException { 
      final String user = charBuffer.toString(); 
      System.out.println("Mensaje recibido: {}" + user); 
      getWsOutbound().writeTextMessage(CharBuffer.wrap("Hola " + user + " desde WebSocket")); 
     } 
    } 
} 

這裏有雲的部署描述符 - web.xml

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> 
    <display-name>TestWebSocket</display-name> 
    <servlet> 
    <servlet-name>TestWebSocket</servlet-name> 
    <servlet-class>com.test.TestWebSocket</servlet-class> 
    </servlet> 
    <servlet-mapping> 
    <servlet-name>TestWebSocket</servlet-name> 
    <url-pattern>/testWebSocket</url-pattern> 
    </servlet-mapping> 
</web-app> 

而且客戶端代碼 - testSocket.js

/* 
* For Testing websockets 
*/ 
$(function(){ 

    console.log("URL :" + 'ws://' + location.host + '/TestTomcatWebSocket/testWebSocket'); 
    var ws = new WebSocket('ws://' + location.host + '/TestTomcatWebSocket/testWebSocket'); 

    ws.onopen = function() { 
     console.log("Websocket Ready!!"); 
     sendMessage(); 
    }; 

    ws.onmessage= function(data) { 
     console.log("message received : " + data); 
    }; 

    function sendMessage() { ws.send("Test"); }; 
}); 

在執行應用程序完成與以下java.lang.AbstractMethodError例外:

INFO: Server startup in 495 ms 
Origin: {}http://localhost:8080 
Dec 17, 2012 1:58:51 AM org.apache.catalina.core.StandardWrapperValve invoke 
SEVERE: Servlet.service() for servlet [TestWebSocket] in context with path [/TestTomcatWebSocket] threw exception [Servlet execution threw an exception] with root cause 
java.lang.AbstractMethodError: org.apache.catalina.websocket.WebSocketServlet.createWebSocketInbound(Ljava/lang/String;)Lorg/apache/catalina/websocket/StreamInbound; 
    at org.apache.catalina.websocket.WebSocketServlet.doGet(WebSocketServlet.java:125) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:621) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) 
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:225) 
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169) 
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472) 
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168) 
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98) 
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927) 
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) 
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407) 
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:999) 
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:565) 
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:307) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) 
    at java.lang.Thread.run(Unknown Source) 

我使用Apache Tomcat 7.0.33。

+0

你有Tomcat的JAR文件在'WEB-INF/lib'? –

+0

我應該明確地包括它,當tomcat已經在它的庫文件夾中嗎? –

+0

不,我不會在'WEB-INF/lib'中捆綁Tomcat JAR。這樣你應該自動從Tomcat中獲取它們。 –

回答

0

問題解決了。基本上我使用的是早期版本的eclipse,它根據tomcat的早期配置來配置tomcat,以便在其中運行tomcat。所以當我使用eclipse的最新版本JUNO時,它配置了tomcat的最新配置,其中包括catilina.policy文件中的websocket配置。使用的WebSocket

//應用程序需要能夠訪問這個包的權限java.lang.RuntimePermission "accessClassInPackage.org.apache.catalina.websocket";

相關問題