1 year ago

#374430

test-img

Joe Almore

OSGi+Websocket+Jetty = ClassNotFoundException: org....WebSocketServerFactory

I am trying to create a WebSocket Server in an OSGi environment:

public class MyServer {
    private Server server = null;

    public void startServer(String wsHost, int wsPort, String wsPath) throws Exception {
        server = new Server();
        ServerConnector connector = new ServerConnector(server);
        connector.setHost(wsHost);
        connector.setPort(wsPort);
        server.addConnector(connector);

        ServletContextHandler context = new ServletContextHandler(server, "/", ServletContextHandler.SESSIONS);
        context.addServlet(MessagingServlet.class, "/"+wsPath);
        server.start();
    }

    public void stopServer() throws Exception {
        if (server != null)
            server.stop();
    }
}

public class MessagingServlet extends WebSocketServlet {
    @Override
    public void configure(WebSocketServletFactory factory) {
        factory.register(EventSocket.class);
    }
}

public class EventSocket extends WebSocketAdapter {
    private final CountDownLatch closureLatch = new CountDownLatch(1);

    @Override
    public void onWebSocketConnect(Session sess) {
        super.onWebSocketConnect(sess);
    }

    @Override
    public void onWebSocketText(String message) {
        super.onWebSocketText(message);
    }

    @Override
    public void onWebSocketClose(int statusCode, String reason) {
        super.onWebSocketClose(statusCode, reason);
        closureLatch.countDown();
    }

    @Override
    public void onWebSocketError(Throwable cause) {
        super.onWebSocketError(cause);
    }
}

This code above works well outside an OSGi environment, however in OSGi it throws the exception: java.lang.ClassNotFoundException: org.eclipse.jetty.websocket.server.WebSocketServerFactory not found by org.eclipse.jetty.util.

I found a website (http://eriklievaart.com/web/osgiwebsockets.html) that proposes a solution for this, but it does not work with the dependencies it shows there, the dependencies are somewhat old. Does anyone knows how to get this (potential solution) working with the approach I describe above?

UPDATE:

OSGi Felix version: 7.0.1, Jetty Version: 9.4.46

Thank you.

websocket

jetty

osgi

0 Answers

Your Answer

Accepted video resources