Java Websocket containers: the possibilities

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 complaint

Hello Tyrus !

Tyrus is the reference implementation for 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. 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, client implementations, a 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)
  • 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();
}
}

Additional reading

Cheers!

About Abhishek

Loves Go, NoSQL DBs and messaging systems
This entry was posted in Java EE and tagged , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s