Java Websocket Containers: The Possibilities

Datetime:2016-08-22 22:31:24          Topic: Java  WebSocket           Share

The Java Websocket API (JSR 356) specification supports different containers:

  • Good old Java EE 7 app servers – since Websocket API is integrated directly into the Java EE 7 Platform.
  • Servlet 3.1 containers.
  • Standalone containers – for runtimes which are not servlet compliant.

Hello Tyrus!

Tyrus  is the reference implementation for the Java Websocket API.

  • What’s important to understand is that it’s the implementation of the Websocket specification (i.e. it provides both Server and Client side support for building Websocket applications using the standard JSR 356 APIs).
  • It’s not an out-of-the-box container (i.e. it does not have a runtime as such).

So, How Does Tyrus Support the Above-Mentioned Runtimes?

Here is how:

  • Tyrus has a modular architecture (i.e. it has different modules for server and client implementations, an SPI, etc.).
  • It has the concepts of containers (you can think of them as connectors) for specific runtime support (these build on the modular setup).

Tyrus Containers

Servlet Container a.k.a Tyrus-container-servlet

  • Used to integrate with existing Servlet 3.1 containers.
  • Leveraged to plug into the Web (Servlet) Container in Java EE 7 compliant app servers.

Standalone container

You have two options:

Grizzly Container (tyrus-container-grizzly module)

  • This is achieved with Grizzly  (which provides the runtime).
  • It can be used for server or client (or both) modes as per your requirements.

Here are the Maven dependencies:

...
<dependencies>
        <dependency>
            <groupId>javax.websocket</groupId>
            <artifactId>javax.websocket-api</artifactId>
            <version>1.1</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.tyrus</groupId>
            <artifactId>tyrus-server</artifactId>
            <version>1.12</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.tyrus</groupId>
            <artifactId>tyrus-container-grizzly-server</artifactId>
            <version>1.12</version>
        </dependency>
</dependencies>
...

Here is the Websocket (annotated) endpoint:

//imports ommitted

@ServerEndpoint("/testwsep")
public class MyWsendpoint {

    static Set<Session> clients = new HashSet<Session>();

    @OnOpen
    public void open(Session s) {
        clients.add(s);
    }

    @OnMessage
    public void msg(String m) {
        for (Session client : clients) {
            try {
                client.getBasicRemote().sendText("Catch this! "+ m);
            } catch (IOException ex) {
                Logger.getLogger(MyWsendpoint.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
//.. other callback methods ommitted - @onClose, @onError
}

Here is how to start it (embedded):

public class TyrusGrizzlyWebsocketRunner {

    public static void main(String[] args) throws DeploymentException, IOException, InterruptedException {
        Server server = new Server("localhost", 8080, "", null, MyWsendpoint.class);
        server.start();
        System.out.print("---- Server Started -----");
        new CountDownLatch(1).await();
    }
}

Pure JDK container (tyrus-container-jdk-client module)

  • Client only mode.
  • Vanilla JDK (i.e. no additional dependencies).
  • Leverages JDK 1.7 non-blocking I/O ( Asynchronous Channel ).

Maven dependencies:

<dependencies>
        <dependency>
            <groupId>javax.websocket</groupId>
            <artifactId>javax.websocket-api</artifactId>
            <version>1.1</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.tyrus</groupId>
            <artifactId>tyrus-client</artifactId>
            <version>1.12</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.tyrus</groupId>
            <artifactId>tyrus-container-jdk-client</artifactId>
            <version>1.12</version>
        </dependency>
</dependencies>

The (annotated) client endpoint:

@ClientEndpoint
public class WebsocketAnotatedClient {

    @OnOpen
    public void onopen(){
        System.out.println("connected to server.... ");
    }

    @OnMessage
    public void onmsg(String msg){
        System.out.println("recieved from server.... "+ msg);
    }
}

Client code to connect to Websocket endpoint (outlined above):

public class WebsocketClientOnVaniallJDKRunner {

    public static void main(String[] args) throws DeploymentException, IOException, InterruptedException {
        WebSocketContainer cc = ContainerProvider.getWebSocketContainer();
        Session connectToServer = cc.connectToServer(WebsocketAnotatedClient.class, URI.create("ws://localhost:8080/testwsep"));

        new CountDownLatch(1).await();
    }

}

References

Cheers!





About List