Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tyrus - simple java application - Could not find an implementation class

I am developing simple application to help me in learning WebSocket and Tyrus concepts in Java. Here is my server-side (server endpoint) and my pom.xml and my client side and pom.xml:

SERVER SIDE

 @ServerEndpoint(value="/websocket/{client-id}")
    public class MyServerEndpoint {

        private static final LinkedList<Session> clients = new LinkedList<Session>();

        @OnOpen
        public void onOpen(Session session) {
            clients.add(session);

        }
        @OnMessage
        public void onMessage(String message,@PathParam("client-id") String clientId) {
            for (Session client : clients) {
                try {
                    client.getBasicRemote().sendObject(clientId+": "+message);          

                } catch (IOException e) {
                    e.printStackTrace();
                } catch (EncodeException e) {
                    e.printStackTrace();
                }
            }
        }
        @OnClose
        public void onClose(Session peer) {
            clients.remove(peer);
        }
    }

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>TyrusJacpFXServer</groupId>
      <artifactId>TyrusJacpFXServer</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <packaging>war</packaging>

      <dependencies>

       <dependency>
        <groupId>javax.websocket</groupId>
        <artifactId>javax.websocket-api</artifactId>
        <scope>compile</scope>
        <version>1.0</version>
     </dependency>

      </dependencies>

      <build>
        <sourceDirectory>src</sourceDirectory>
        <plugins>
          <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
              <source>1.7</source>
              <target>1.7</target>
            </configuration>
          </plugin>
          <plugin>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.4</version>
            <configuration>
              <warSourceDirectory>WebContent</warSourceDirectory>
              <failOnMissingWebXml>false</failOnMissingWebXml>
            </configuration>
          </plugin>
        </plugins>
      </build>
    </project>

CLIENT SIDE

MyClient.java 

@ClientEndpoint
public class MyClient {
    @OnOpen
    public void onOpen(Session session) {
        System.out.println("Connected to endpoint: " + session.getBasicRemote());
        try {
            session.getBasicRemote().sendText("Hello");
        } catch (IOException ex) {
        }
    }

    @OnMessage
    public void onMessage(String message) {
        System.out.println(message);
    }

    @OnError
    public void onError(Throwable t) {
        t.printStackTrace();
    }
}

App.java

public class App {

    public Session session;

    protected void start()
             {

            WebSocketContainer container = ContainerProvider.getWebSocketContainer();

            String uri = "ws://localhost:8080/TyrusJacpFXClient/websocket/desktop-client";
            System.out.println("Connecting to " + uri);
            try {
                session = container.connectToServer(MyClient.class, URI.create(uri));
            } catch (DeploymentException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }             

    }
    public static void main(String args[]){
        App client = new App();
        client.start();

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String input = "";
        try {
            do{
                input = br.readLine();
                if(!input.equals("exit"))
                    client.session.getBasicRemote().sendText(input);

            }while(!input.equals("exit"));

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>TyrusJacpFXClient</groupId>
  <artifactId>TyrusJacpFXClient</artifactId>
  <version>0.0.1-SNAPSHOT</version>

  <dependencies>

  <dependency>
    <groupId>javax.websocket</groupId>
    <artifactId>javax.websocket-api</artifactId>
    <scope>compile</scope>
    <version>1.0</version>
 </dependency>

   <dependency>
    <groupId>org.glassfish.tyrus</groupId>
    <artifactId>tyrus-client</artifactId>
    <version>1.8.3</version>
   </dependency>


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


  </dependencies>

  <build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

I am getting this error:

Exception in thread "main" java.lang.RuntimeException: Could not find an implementation class.
at javax.websocket.ContainerProvider.getWebSocketContainer(ContainerProvider.java:73)
at org.hwc.App.start(App.java:25)
at org.hwc.App.main(App.java:40)

Can someone help me see the problem ? Server is running inside GlassFish 4.0

like image 300
Sysrq147 Avatar asked Nov 06 '14 11:11

Sysrq147


1 Answers

you have dependency issue. This one:

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

is not correct. You need to use [1] (grizzly based client):

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

or [2] (jdk 7 NIO client impl)

  <dependency>
        <groupId>org.glassfish.tyrus</groupId>
        <artifactId>tyrus-container-jdk-client</artifactId>
        <version>1.8.3</version>
  </dependency>
like image 84
Pavel Bucek Avatar answered Sep 22 '22 23:09

Pavel Bucek