DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. Java Websocket Containers: The Possibilities

Java Websocket Containers: The Possibilities

With the Java Websocket API being integrated directly into Java EE7, check out how Tyrus, the reference implementation, integrated with containers for maximum effect.

Abhishek Gupta user avatar by
Abhishek Gupta
CORE ·
Jun. 21, 16 · Tutorial
Like (4)
Save
Tweet
Share
6.78K Views

Join the DZone community and get the full member experience.

Join For Free

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

  • Tyrus modules
  • Tyrus javadocs
  • JSR 356 (Websocket) specification

Cheers!

Container Java (programming language) WebSocket

Published at DZone with permission of Abhishek Gupta, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • What Was the Question Again, ChatGPT?
  • How to Submit a Post to DZone
  • API Design Patterns Review
  • Microservices Discovery With Eureka

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: