2

im new to WebSockets . for start learning i followed this tutorial . and simply copied the ClientEndpoint and ServerEndpoint and deployed the server with tyrus .

but when i try to run the server it will throw this error :

Failed to load container provider class: org.glassfish.tyrus.container.grizzly.GrizzlyEngine

here is the deploy code ( i've commented the line which i get error on ) :

import java.io.BufferedReader;
import java.io.InputStreamReader;

import org.glassfish.tyrus.server.Server;

public class WebSocketServer {

    public static void main(String[] args) {
        runServer();
    }

    public static void runServer() {

        Server server = new Server("localhost", 8000, "/websockets", WordgameServerEndpoint.class);

        try {
            server.start(); //i get the error on this line
            BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
            System.out.print("Please press a key to stop the server.");
            reader.readLine();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        } finally {
            server.stop();
        }
    }
}

here is the server code :

import java.io.IOException;
import java.util.logging.Logger;

import javax.websocket.CloseReason;
import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.CloseReason.CloseCodes;
import javax.websocket.server.ServerEndpoint;

@ServerEndpoint(value = "/game")
public class WordgameServerEndpoint {

    private Logger logger = Logger.getLogger(this.getClass().getName());

    @OnOpen
    public void onOpen(Session session) {
        logger.info("Connected ... " + session.getId());
    }

    @OnMessage
    public String onMessage(String message, Session session) {
        switch (message) {
            case "quit":
                try {
                    session.close(new CloseReason(CloseCodes.NORMAL_CLOSURE, "Game ended"));
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
                break;
        }
        return message;
    }

    @OnClose
    public void onClose(Session session, CloseReason closeReason) {
        logger.info(String.format("Session %s closed because of %s", session.getId(), closeReason));
    }
}

i'd appreciate any help :)

4
  • were you able to resolve it ?? Apr 7, 2016 at 0:06
  • no not really . i changed my way to OkHTTP library , which was much easier to handle . Apr 7, 2016 at 20:49
  • oh ok.. Our bundling is different as we are using Apache karaf for bundling, and issue seems to be with classloaders not loading this GrizzlyEngine(its OSGI framework issue). This jar happens to run fine if run as stand alone app. Did you run this app as independent app or inside of a web container ? Apr 8, 2016 at 20:11
  • no i just tried it as independent app. Apr 8, 2016 at 21:28

1 Answer 1

2

it seems to be a dependencies issue,

if you are a maven user:

<dependencies>
    <dependency>
        <groupId>javax.websocket</groupId>
        <artifactId>javax.websocket-api</artifactId>
        <version>1.0</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.tyrus</groupId>
        <artifactId>tyrus-server</artifactId>
        <version>1.1</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.tyrus</groupId>
        <artifactId>tyrus-client</artifactId>
        <version>1.1</version>
    </dependency>

    <dependency>
        <groupId>org.glassfish.tyrus</groupId>
        <artifactId>tyrus-container-grizzly</artifactId>
        <version>1.1</version>
    </dependency>

    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.5</version>
    </dependency>
</dependencies>

or load the following jars:

  • grizzly-framework-2.3.3.jar
  • grizzly-http-2.3.3.jar
  • grizzly-http-server-2.3.3.jar
  • grizzly-rcm-2.3.3.jar
  • javax.websocket-api-1.0.jar
  • tyrus-client-1.1.jar
  • tyrus-container-grizzly-1.1.jar
  • tyrus-core-1.1.jar
  • tyrus-server-1.1.jar
  • tyrus-spi-1.1.jar
  • tyrus-websocket-core-1.1.jar

please keep in mind these dependencies for both client and server implementations.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.