Sep 4, 2014

Chat Application using Java EE 7 - Web Sockets

Use Case - 

Users can join a public Chat room. Users can ask questions and Agent will answer the questions.

High Level Design - 

  • A WebSocketEndPoint which receives & responds to Client (/User) messages. It allows new client to join and maintains list of active users. 
  • Agent is an EnterpriseBean with business logic and answer to all questions. 
  • User will send JSON. Decoders will convert them to JoinMessage or ChatMessage and pass them to WebSocketEndpoint. 
  • EnterpriseBean will call WebSocketEndPoint's sendAll method to send answer to Users. Answer will be a ChatMessage. Encoders will convert the ChatMessage into JSON, before actually passing to Clients. 
  • We have named WebSocketEndPoint class as BotEndPoint, EnterpriseBean as BotBean. 

Design Consideration & Classes -
1. CDI Bean "BotBean" holds business logic and has answer to all question.
2. BotEndPoint (Web socket end point class) allows user to join and receives questions from User. BotEndPoint maintains list of active users. BotEndPoint informs all users if any new user joins, any new question is asked by user, and sends answers to all users.
3. Four different types of Message: ChatMessage, InfoMessage, JoinMessage, and UserMessage. Four type of messages BotEndPoint can send to users. These messages should be converted to JSON, before passing to client. So we have four Encoders to convert one type of message into JSON.
4. Users can send a JoinMessage (to join chat room) or ChatMessage (to ask question). Indeed users will send JSON and Decoders - JoinMessageDecoder, ChatMessageDecoder will convert the JSON into either JoinMessage or ChatMessage correspondingly.
5. BotEndPoint will invoke BotBean to handle the user question. BotBean uses BotEndPoint's sendAll method to send answer to users.
6. User sends a question and BotEndPoint invokes BotBean to prepare answer, and later BotBean invokes BoTEndPoint to send answer to users. But if all this is done in single thread, then user will be blocked for sending second message, until he or she receives respond of first message.
Handling of message by BotBean should be done in separate thread. Indeed we should create pool of BotBean to further imporve performace.

Flow Chart - 

Design - 

More discussion on the design along with code can be found on Oracle site - Java EE 7 tutorial.


Common symbols for Java EE Design

My past few posts are on Java / Java EE designs. In which I have to create a design of solution of some use cases, using Java/JavaEE technologies. Generally I am mostly using UML symbols, but UML has versions and many symbols. Along with UML symbols we can use some more symbols. Some of the common symbols and which I prefer are below -


Sep 1, 2014

Periodic data updates to Web Clients

Using Java EE7 - Web Sockets

We have several ways to publish periodic data updates from server to client. Data updates like Stock price update, or Account available balance, or Next auction time, etc. might be decided by business logic on server and might be need to propagated to Web clients to display. If server application not itself generates such data, it might be receiving from some other source, and it needs to pass to web clients, because Web clients might now know when data is available.

One way is to pass the Event to client and one way is to provide update on Web Socket.

A sample design using Web Socket can be like -

Below is the sample code, which is there on Oracle site.
WebSocketEndpoint is a java class whose code is very simple like below -

And JavaScript code on Web Client is like below -


Real Time Stock Market Data Processing - Design

Use case - Processing real time stock market data like quote & trade.  Generally Stock market publish the updates, intra-day live data, via API's, which an application can listen and publish to its clients.

Flow - Steps 
1. Connect to Stock Exchange server and register for Events (trade, quote. depth).
2. On receiving event, validate event, log event, and pass it on to Handler.
3. Handler knows EVENT_TYPE and handles events appropriately.
4. Handler converts events into domain objects, which are usable by the Business layer or other clients.
5. Business layer generates Reports/Metrics and might make decisions on investment. Business layer might not understand the event, so it is required to map the events into domain objects which are more understood by the higher layers.
6. Domain objects might be distributed to other applications or clients. Other clients may desire objects in their required format, like XML, EDI, etc.

Design Considerations - 

  • MBeans should be created & registered on application startup. MBeans are used for monitoring, like - Memory Consumption, Statistics, Reprocessing and event, Re-sending an message (/domain object) to a client. 
  • We might receive thousands of thousands of events in a day, so we can't process events sequentially. We should maintain pool of handlers. 
  • Events should be logged into Database with event stage. Event stage is optional but it should be different from the EVENT_TYPE & EVENT_STATUS which we receive in the Event. 
  • User details for Stock Exchange Server can be maintained in properties file. 
  • Different Event types can have different structure. 
  • Events should be placed in Queue, before passing to Handler. OR if Java Thread Pool is used, which comes with a blocking queue, where tasks are placed before actually getting handled by a thread. 
  • Events should be validated, possibly for checksum, to ignore duplicate events. 
Flowchart - 

 Detailed Design - 

© Shift, ShEkUP, Shape, and Surprise | All rights reserved.
Blogger Template Crafted by pipdig