2
votes

I want to use java websocket with Repast simphony but it doesn't work, I get the following errors:

    java.lang.NoSuchMethodError: javax.servlet.http.HttpServletRequest.getServletContext()Ljavax/servlet/ServletContext;
        at org.eclipse.jetty.websocket.servlet.UpgradeHttpServletRequest.(UpgradeHttpServletRequest.java:91)
        at org.eclipse.jetty.websocket.servlet.ServletUpgradeRequest.(ServletUpgradeRequest.java:52)
        at org.eclipse.jetty.websocket.server.WebSocketServerFactory.acceptWebSocket(WebSocketServerFactory.java:156)
        at org.eclipse.jetty.websocket.server.WebSocketServerFactory.acceptWebSocket(WebSocketServerFactory.java:148)
        at org.eclipse.jetty.websocket.server.WebSocketHandler.handle(WebSocketHandler.java:97)
        at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97)
        at org.eclipse.jetty.server.Server.handle(Server.java:497)
        at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:311)
        at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:248)
        at org.eclipse.jetty.io.AbstractConnection$2.run(AbstractConnection.java:540)
        at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:610)
        at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:539)
        at java.lang.Thread.run(Thread.java:745)

Could someone explain me how to solve this error? Thank you

I tested code taken from the internet and it works if I don't run it with repast simphony

My server

    package webSocketTest;

    import org.eclipse.jetty.server.Server;
    import org.eclipse.jetty.websocket.server.WebSocketHandler;
    import org.eclipse.jetty.websocket.servlet.WebSocketServletFactory;

    public class WebSocketTest {

        public static void main(String[] args) throws Exception {
            Server server = new Server(8080);
            WebSocketHandler wsHandler = new WebSocketHandler() {
                @Override
                public void configure(WebSocketServletFactory factory) {
                    factory.register(MyWebSocketHandler.class);
                }
            };
            server.setHandler(wsHandler);
            server.start();
            server.join();
        }
    }


    package webSocketTest;

    import java.io.IOException;
    import org.eclipse.jetty.websocket.api.Session;
    import org.eclipse.jetty.websocket.api.annotations.OnWebSocketClose;
    import org.eclipse.jetty.websocket.api.annotations.OnWebSocketConnect;
    import org.eclipse.jetty.websocket.api.annotations.OnWebSocketError;
    import org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage;
    import org.eclipse.jetty.websocket.api.annotations.WebSocket;

    @WebSocket
    public class MyWebSocketHandler {

        @OnWebSocketClose
        public void onClose(int statusCode, String reason) {
            System.out.println("Close: statusCode=" + statusCode + ", reason=" + reason);
        }

        @OnWebSocketError
        public void onError(Throwable t) {
            System.out.println("Error: " + t.getMessage());
        }

        @OnWebSocketConnect
        public void onConnect(Session session) {
            System.out.println("Connect: " + session.getRemoteAddress().getAddress());
            try {
                session.getRemote().sendString("Hello Webbrowser");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        @OnWebSocketMessage
        public void onMessage(String message) {
            System.out.println("Message: " + message);
        }
    }

2

2 Answers

2
votes

Another alternative to the removing of the groovy jars: In fact Repast Simphony 2.2 uses servlet-api 2.4. which doesn't contain javax.servlet.http.HttpServletRequest.getServletContext() method. Another solution is then to replace the servlet-api jar used by default by Repast Simphony by a recent javax servlet-api

1
votes

I finally found the solution. I removed all the groovy jars which are by default present in the project build path and it works.