Java EE 7: EJB publishing CDI Events that are pushed over WebSocket to browser client image47

Java EE 7: EJB publishing CDI Events that are pushed over WebSocket to browser client

In this article, I show how I have created a simple and fairly meaningless Java EE 7 application that uses a number of smart Java EE features:

  • a stateless session bean has a scheduled method (that is triggered every 3 seconds)
  • the stateless session bean publishes custom CDI events
  • a class annotated as WebSocket EndPoint manages WebSocket channels with WS clients (in this case simple HTML browser applications) – echoing messages between the clients
  • the WebSocket Endpoint also observes the CDI event and pushes the event payload to each of the WebSocket clients

Using NetBeans 7.3.1 and GlassFish 4.0 – as well as a number of blog-articles and sample applications – this turned out to be very simple to put together.

This next figure visualizes what I did:

image

The implementation steps

The important steps to come to a running applications are:

  • create new Java EE 7 project in NetBeans
  • create class TimeEvent that will be used to publish as CDI event
  • create class EventProducer – a stateless EJB session bean that is scheduled to trigger every 3 seconds; it publishes a TimeEvent whenever it is triggered
  • create class SocketMediator – an annotated POJO that has the ServerEndpoint annotation (to handle incoming WebSocket messages) as well as a method that observes the TimeEvent and pushes the payload of the event to every WebSocket session maintained by the POJO
  • a simple HTML page and JavaScript library that connect to the WebSocket channel established by the POJO and that write every message received to the screen

1. Create new Java EE 7 project in NetBeans

Using the wizard, creating a new project is straightforward.

2. Create class TimeEvent that will be used to publish as CDI event

This class itself is simple; it has not interface to implement nor annotations to contain.

image

The container – and therefore our code – can discern between different types of events. Using an Event Qualifier – such as WBTimeEvent shown below – we can guide our observer methods in the specific events and conditions they listen for.

image

3. Create class EventProducer – a stateless EJB session bean that is scheduled to trigger every 3 seconds; it publishes a TimeEvent whenever it is triggered

This class is more interesting. The class is instantiated by the Java EE container. It is triggered by the container according to the schedule (every three seconds) to execute the produceRegularEvent method. This method creates a new instance of the TimeEvent. Then it uses the injected Event object (annotated with the Event Qualifier) – that will contain a TimeEvent as payload – to actually publish a CDI event:

image

4. create class SocketMediator

This class is arguably the most interesting one of the lot. It exposes a WebSocket endpoint – at the path socketmediator. That means that messages sent to ws://host:port/appname/socketmediator will end up in the onMessage() method in this class.

image

Whenever a client opens a connect to the websocket at mediatorendpoint, the session for that client is retained in the peers collection. This collection is used whenever a message is to be pushed to every client.

When a message arrives on the WebSocket – onMessage method – its content is forwarded to each of the connected clients , except the sender of the message. This sender receives a message that advises about the message having been received and processed.

The onTimeEvent method observes the TimeEvent (with WBTimeEvent qualifier). Whenever that event occurs, this method will be invoked by the container. In the method, the payload is taken from the event and sent in a WebSocket message to each of the registered clients.

5. create HTML page and JavaScript library

An important aspect of this application is the JavaScript code that establishes the WebSocket connection and handles the message traffic – sending a message whenever the link is clicked and writing the contents of received messages to the HTML client. Notice how the endpoint defined in the ServerEndPoint annotation in SocketMediator is used to define the wsUri. Also notice how the onMessage function is used to write the contents of the messages received on the WebSocket channel to the output element in the HTML page (through function writeToScreen).

image

The HTML required to get this to work is simple:

image

The real work is done in the JavaScript.

6. Run the application

The application can easily be run from NetBeans, deployed directly onto the GlassFish 4.0 Server.

As soon as the browser has started, the web socket connection is established and the web socket messages based on the time events start arriving in the browser. When the link Click is clicked, a message is pushed over the web socket channel to the server where it is forwarded to each of the connected clients.

image

Resources

Download the NetBeans/Maven Project with the sources for this article: JavaEE7_WebSocketEJBTimerCDIEvents.

Blog article – Integrating WebSockets and JMS with CDI Events in Java EE 7 by Bruno Borges – https://blogs.oracle.com/brunoborges/entry/integrating_websockets_and_jms_with

Blog article – Getting Started with JSF 2.0 and CDI part 3 – Events – http://www.andygibson.net/blog/2010/01/11/getting-started-with-jsf-2-0-and-cdi-part-3/

Tutorial for NetBeans – Using the WebSocket API in a Web Application – https://netbeans.org/kb/docs/javaee/maven-websocketapi.html