我在一个项目中工作,我需要创建一个实时的Web应用程序。我已经开发了一个用Apache Tomcat测试WebSockets的小程序,但是它不起作用而是抛出AbstractMethodError
异常。 servlet编译并且Web应用程序部署得很好。
这是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 :(得分:0)
问题解决了。基本上我使用的是旧版本的eclipse,它根据tomcat的早期配置在其中运行tomcat来配置tomcat。因此,当我使用eclipse的最新版本JUNO时,它使用tomcats最新配置配置它,其中包括catilina.policy文件中的websocket配置。
//使用WebSocket的应用程序需要能够访问此程序包权限java.lang.RuntimePermission "accessClassInPackage.org.apache.catalina.websocket";