It's time to take Java EE 8 for a spin and try out Glassfish 5 builds on Docker using Oracle Container Cloud. Java EE specifications covered

 

  • Server Sent Events in JAX-RS 2.1 (JSR 370) - new in Java EE 8
  • Asynchronous Events in CDI 2.0 (JSR 365) - new in Java EE 8
  • Websocket 1.1 (JSR 356) - part of the existing Java EE 7 specification

 

 

Application

 

Here is a quick summary of what's going on

 

  • A Java EE scheduler triggers asynchronous CDI events (fireAsync())
    • These CDI events are qualified (using a custom Qualifier)
    • It also uses a custom java.util.concurrent.Executor (based on the Java EE Concurrency Utility ManagedExecutorService) – thanks to the NotificationOptions supported by the CDI API
  • Two (asynchronous) CDI observers (@ObservesAsync) – a JAX-RS SSE broadcaster and a Websocket endpoint
  • SSE & Websocket endpoints cater to their respective clients

 

Notice the asynchronous events running in Managed Executor service thread

action-2.jpg?w=768

You can choose to let things run in the default (container) chosen thread

 

cdi-2-async-events-in-action.jpg?w=768

 

Build the Docker images

 

Please note that I have used my personal Docker Hub account (abhirockzz) as the registry. Feel free to use any Docker registry of your choice

 

git clone https://github.com/abhirockzz/cdi-async-events.git
mvn clean install
docker build -t abhirockzz/gf5-nightly -f Dockerfile_gf5_nightly .
docker build -t abhirockzz/gf5-cdi-example -f Dockerfile_app .

 

Push it to a registry

 

docker push abhirockzz/gf5-cdi-example

 

Run in Oracle Container Cloud

 

Create a service

 

 

 

Deploy it

 

 

You see this once the container (and the application) start..

 

 

 

Drill down into the (Docker) container and check the IP for the host where it's running and note it down

Test it

 

Make use of the Host IP you just noted down

 

http://<occs_host_ip>:8080/cdi-async-events/events/subscribe - You should see a continuous stream of (SSE) events

 

68747470733a2f2f61626869726f636b7a7a2e66696c65732e776f726470726573732e636f6d2f323031372f30362f7373652d6f75747075742e6a7067

 

Pick a Websocket client and use it connect to the Websocket endpoint ws://<occs_host_ip>:8080/cdi-async-events/

 

You will see the same event stream... this time, delivered by a Websocket endpoint

 

 

 

You can try this with multiple clients - for both SSE and Websocket

 

Enjoy Java EE 8 and Glassfish !

 

**The views expressed in this post are my own and do not necessarily reflect the views of Oracle