Up until now we used DataObject.onEvent() for processing data object events. This method is executed synchronously, i.e. on the same execution thread that reads the object data from the communication channel. In many cases event processing is a relatively lengthy operation and doing it synchronously might not only defer execution of data objects of the same type but also slow down dramatically the entire application performance.
Spiderwiz offers an elegant solution to the problem with DataObject.onAsyncEvent(), a message that can be
overridden instead of onEvent() and operates
asynchronously.
For our exercise, we use the chat system that we have built so far and introduce an intentional suspension in the processing of chat messages. We will see how it affects both synchronous and asynchronous processing.
In order to increase the visibility of these effects we add
a time field to both ChatMessage library class (that handles
chat room messages) and Chat (that
handles private messages). Here is the first:
package org.spiderwiz.tutorial.objectLib;
import org.spiderwiz.annotation.WizField;
import org.spiderwiz.core.DataObject;
import org.spiderwiz.zutils.ZDate;
/**
* A data object representing a message sent in chat room
*/
public class ChatMessage extends DataObject {
/**
* Mandatory public static field for all data objects.
*/
public final static String ObjectCode = "CHTMSG";
@WizField private String message; // Contains a chat message
@WizField private ZDate time; // Contains message time
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public ZDate getTime() {
return time;
}
public void setTime(ZDate time) {
this.time = time;
}
/**
* @return Chatter object code
*/
@Override
protected String getParentCode() {
return Chatter.ObjectCode;
}
/**
* @return true as this object is disposable.
*/
@Override
protected boolean isDisposable() {
return true;
}
@Override
protected boolean isUrgent() {
return true;
}
}And the second:
package org.spiderwiz.tutorial.objectLib;
import org.spiderwiz.annotation.WizField;
import org.spiderwiz.core.DataObject;
import org.spiderwiz.zutils.ZDate;
/**
* Chat class for Spiderwiz tutorial's lesson 2.
*/
public class Chat extends DataObject {
/**
* Mandatory public static field for all data objects.
*/
public final static String ObjectCode = "CHAT";
@WizField private String name; // Contains the name of the chatter
@WizField private String message; // Contains a chat message
@WizField private ZDate time; // Contains message time
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public ZDate getTime() {
return time;
}
public void setTime(ZDate time) {
this.time = time;
}
/**
* @return null as this is a root object.
*/
@Override
protected String getParentCode() {
return null;
}
/**
* @return true as this object is disposable.
*/
@Override
protected boolean isDisposable() {
return true;
}
@Override
protected boolean isUrgent() {
return true;
}
}The introduction of this field requires a slight change to ChatMain:
package org.spiderwiz.tutorial.lesson10.chat;
import java.io.PrintStream;
import java.text.ParseException;
import java.util.Collection;
import java.util.List;
import java.util.UUID;
import org.spiderwiz.core.DataObject;
import org.spiderwiz.tutorial.objectLib.ActiveUserQuery;
import org.spiderwiz.tutorial.objectLib.Chat;
import org.spiderwiz.tutorial.objectLib.ChatHistoryQuery;
import org.spiderwiz.tutorial.objectLib.ChatMessage;
import org.spiderwiz.tutorial.objectLib.ChatRoom;
import org.spiderwiz.tutorial.objectLib.Chatter;
import org.spiderwiz.tutorial.objectLib.ConsoleMain;
import org.spiderwiz.tutorial.objectLib.LoginQuery;
import org.spiderwiz.zutils.ZDate;
import org.spiderwiz.zutils.ZUtilities;
/**
* Provides the entry point of the application. Initializes and executes the Spiderwiz framework.
*/
public class ChatMain extends ConsoleMain{
private static final String ROOT_DIRECTORY = "";
private static final String APP_NAME = "Chat Room Client";
private static final String APP_VERSION = "Z8.01"; // Version Z8.01: Lesson 8 modifications
private static final String JOIN = "!join"; // join a chat room command
private static final String LEAVE = "!leave"; // leave current chat room command
private static final String LOGIN = "!login"; // start login procedure
private static final String LOGOUT = "!logout"; // log the user out
private static final String REGISTER = "!register"; // start registration procedure
private static final String HISTORY = "!history"; // print my chat history
private String myName = null; // The user login name
private ZDate birthday = null; // User birth date. Get it when logging in
private Chatter chatter = null; // A Chatter object representing the user chatting in a specific room
private ChatMessage message = null; // A ChatMessage object for committing chat messages
private Chat privateMessage = null; // A Chat object for committing private messages
/**
* Class constructor with constant parameters.
* @param confFileName configuration file name, provided as a command argument
*/
public ChatMain(String confFileName) {
super(ROOT_DIRECTORY, confFileName, APP_NAME, APP_VERSION);
}
/**
* @return the class instance as ChatMain type.
*/
public static ChatMain getInstance() {
return (ChatMain)ConsoleMain.getInstance();
}
/**
* Application entry point. Instantiate the class and initialize the instance.
*
* @param args the command line arguments. The first argument is used as the configuration file name.
*/
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// Don't do anything if there is no configuration file name
if (args.length == 0) {
System.out.println("Configuration file has not been defined");
return;
}
new ChatMain(args[0]).init();
}
/**
* Print user instructions to the console.
* @return true
*/
@Override
protected boolean preStart() {
System.out.printf(
"Welcome to the dynamic chat system.n"
+ "To log in type !login.n"
+ "To log out type !logout.n"
+ "To register type !register.n"
+ "To join a chat room or change your current chat room, type "!join <room name>".n"
+ "To leave your chat room, type "!leave".n"
+ "To send a message in the current chat room just type the message "
+ "(a message cannot start with either '>' or '!').n"
+ "To send a private message type '>' followed by name of the user you want to send "
+ "the private message to, followed by ':' for by the message.n"
+ "To print the history of your own messages, type "!history <hours>h" or "!history <days>d"n"
+ "(for instance "!history 3h" to see messages since 3 hours ago and "!history 1d" to see messages since 1 day"
+ " ago).n"
+ "or type just "!history" to see all your messages.n"
+ "To exit type 'exit'.n"
);
return true;
}
/**
* @return the list of produced objects - Chatter, Chat, ChatMessage and LoginQuery
*/
@Override
protected String[] getProducedObjects() {
return new String[]{
Chatter.ObjectCode,
Chat.ObjectCode,
ChatMessage.ObjectCode,
LoginQuery.ObjectCode,
ChatHistoryQuery.ObjectCode
};
}
/**
* @return the list of consumed objects - ChatRoom, Chatter, Chat, ChatMessage and ActiveUserQuery
*/
@Override
protected String[] getConsumedObjects() {
return new String[]{
ChatRoom.ObjectCode,
Chatter.ObjectCode,
Chat.ObjectCode,
ChatMessage.ObjectCode,
ActiveUserQuery.ObjectCode
};
}
/**
* Add implementation classes to the object factory list of this application.
* @param factoryList
*/
@Override
protected void populateObjectFactory(List<Class<? extends DataObject>> factoryList) {
super.populateObjectFactory(factoryList);
factoryList.add(ChatRoomConsume.class);
factoryList.add(ChatterConsume.class);
factoryList.add(ChatConsume.class);
factoryList.add(ChatMessageImp.class);
factoryList.add(LoginQuery.class);
factoryList.add(ActiveUserQueryReply.class);
factoryList.add(ChatHistoryQueryInquire.class);
}
/**
* Process an input line
* @param line the line to be broadcast as a chat message.
* @return true if processed successfully
*/
@Override
protected boolean processConsoleLine(String line) {
try {
// If the line starts with '!' this is a command line
if (line.startsWith("!")) {
String command[] = line.trim().split("s+", 2);
switch (command[0].toLowerCase()) {
case JOIN:
// if room name is provided join the room
if (command.length > 1)
joinRoom(command[1]);
break;
case LEAVE:
// If the user is in a room, leave it
leaveRoom(null);
break;
case LOGIN:
login();
break;
case LOGOUT:
logout();
break;
case REGISTER:
register();
break;
case HISTORY:
if (myName == null)
System.out.println("You must log in before requesting history.");
else if (!getHistory(command.length < 2 ? null : command[1]))
System.out.println("Invalid command argument");
break;
}
return true;
}
// If the line starts with '>' split the line on the colon (':') and send the test
// after the colon as a private message to the user with the name before the colon.
if (line.startsWith(">")) {
String cmd[] = line.substring(1).split(":");
if (cmd.length < 2)
return true;
String destination = findUser(cmd[0]);
if (destination == null)
return true;
// If didn't do yet, create an orphan Chatter object for sending private messages,
// then prepare a ChatMessage object for sending private messages
if (privateMessage == null) {
privateMessage = createTopLevelObject(Chat.class, null);
privateMessage.setName(myName);
}
// Set message and commit to destination
privateMessage.setMessage(cmd[1]);
privateMessage.setTime(ZDate.now());
privateMessage.commit(destination);
return true;
}
// If this is a normal message, make sure the user is in a room and broadcast the message
if (message != null) {
message.setMessage(line);
message.setTime(ZDate.now());
message.commit();
}
return true;
} catch (NoSuchFieldException | IllegalAccessException ex) {
// Theoretically arriving here if a data object does not contain ObjectCode static field or the field is not public.
// In this case, send a command exception message.
sendExceptionMail(ex, "Cannot instantiate a data object class", null, false);
return false;
}
}
public String getMyName() {
return myName;
}
public ZDate getBirthday() {
return birthday;
}
/**
* Get user name and password and log in.
* @return a PrintStream object. Not needed by the caller but we use it as a trick to save coding.
* @throws NoSuchFieldException If LoginQuery does not define ObjectCode static field.
* @throws IllegalAccessException If LoginQuery.ObjectCode is not public.
*/
private PrintStream login() throws NoSuchFieldException, IllegalAccessException {
String username;
String password;
// Check if already logged in
if (myName != null)
return System.out.printf("You are already logged in as %s. To log in with another name log out first.n", myName);
// Create a login query
LoginQuery query = createQuery(LoginQuery.class);
while(true) {
// Get user name
System.out.printf("(type Enter to exit login)n");
username = readConsoleLine("User name:");
if (username.isBlank())
return exitLogin();
do {
// Get password
password = readConsoleLine("Password:");
if (password.isBlank())
return exitLogin();
// Post the login query
query.setNewUser(false);
query.setName(username);
query.setPassword(password);
query.post(5 * ZDate.SECOND);
// If the query did not expire check login results, if it did, repeat.
if (query.waitForReply()) {
switch (query.getResult()) {
case OK:
myName = query.getName();
birthday = query.getBirthday();
return System.out.printf("Logged in successfully as %s.n", myName);
case NOT_EXISTS:
System.out.printf("User %s does not exist. Please try again.n", username);
break;
case WRONG_PASSWORD:
System.out.printf("Password did not match, please try again.n");
break;
}
} else
return unavailableUserManager();
} while (query.getResult() == LoginQuery.ResultCode.WRONG_PASSWORD);
}
}
/**
* Log out if already logged in
*/
private void logout() {
if (myName == null) {
System.out.printf("No user is logged in.n");
return;
}
leaveRoom(null);
System.out.printf("%s is logged out.n", myName);
myName = null;
birthday = null;
}
private PrintStream register() throws NoSuchFieldException, IllegalAccessException {
String username;
String password, password2;
// Check if already logged in
if (myName != null)
return System.out.printf("You are already logged in as %s. To register a new user log out first.n", myName);
// Create a registration query
LoginQuery query = createQuery(LoginQuery.class);
while(true) {
// Get user name
System.out.printf("(type Enter to exit login)n");
username = readConsoleLine("User name:");
if (username.isBlank())
return exitRegistration();
do {
// Get password
password = readConsoleLine("Password:");
if (password.isBlank())
return exitRegistration();
// Repeat the password
password2 = readConsoleLine("Repeat password:");
if (password2.isBlank())
return exitRegistration();
// Check if passwords are equal
if (password.equals(password2))
break;
System.out.println("Passwords do not match.");
} while (true);
// Get and parse birth date
do {
String s = readConsoleLine("Date of birth (yyyy/mm/dd):");
if (s.isBlank())
return exitRegistration();
try {
birthday = ZDate.parseTime(s, "yyyy/MM/dd", null);
} catch (ParseException ex) {
birthday = null;
System.out.printf("Invalid date %s. Please try again.n", s);
}
} while (birthday == null);
// Post the registration query
query.setNewUser(true);
query.setName(username);
query.setPassword(password);
query.setBirthday(birthday);
query.post(5 * ZDate.SECOND);
// If the query did not expire check login results, if it did, repeat.
if (query.waitForReply()) {
switch (query.getResult()) {
case OK:
myName = username;
return System.out.printf("Registered and logged in successfully as %s.n", myName);
case EXISTS:
System.out.printf("User %s already exista. Please try again with another name.n", username);
break;
}
} else
return unavailableUserManager();
}
}
/**
* Print a message when login existed by typing a blank Enter
* @return a PrintStream object. Not needed by the caller but we use it as a trick to save coding.
*/
private PrintStream exitLogin() {
return System.out.printf("Login exitedn");
}
/**
* Print a message when registration existed by typing a blank Enter
* @return a PrintStream object. Not needed by the caller but we use it as a trick to save coding.
*/
private PrintStream exitRegistration() {
return System.out.printf("Registration exitedn");
}
/**
* Print a message when user management is not available
* @return a PrintStream object. Not needed by the caller but we use it as a trick to save coding.
*/
private PrintStream unavailableUserManager() {
return System.out.printf("No user management entity is available to handle this requestn");
}
/**
* Join a room after checking that the room exists, its adult setting matches user configuration,
* and the user is not already there. If the user is in another room leave that room first.
* @param room the new room name
* @throws NoSuchFieldException If any of the created data objects does not define ObjectCode static field.
* @throws IllegalAccessException If ObjectCode is not public.
*/
private synchronized void joinRoom(String room) throws NoSuchFieldException, IllegalAccessException {
// User must be logged in
if (myName == null) {
System.out.printf("You must log in before joining a room.n");
return;
}
// Check whether room exists and the user is not already in the room
ChatRoomConsume chatRoom = getRootObject().getChild(ChatRoomConsume.class, room);
if (chatRoom == null) {
System.out.printf("Room %s does not existn", room);
return;
}
if (chatter != null && room.equalsIgnoreCase(chatter.getParent().getObjectID()))
return;
// Check if a young chatters is trying to join an adult room
if (!chatRoom.canJoin(birthday)) {
System.out.printf("You cannot join room %s because it is only for adultsn", room);
return;
}
// Leave current room if any
leaveRoom(null);
// Create a Chatter object for the user that joins the specified room
chatter = chatRoom.createChild(Chatter.class, getAppUUID().toString());
chatter.setName(myName);
chatter.setBirthday(birthday);
chatter.commit(); // let everybody know I joined the room
// Prepare a ChatMessage object for sending messages from now on
message = chatter.createChild(ChatMessage.class, null);
System.out.printf("Joined room %sn", room);
}
/**
* If the user is in the given room, leave it.
* @param room the name of the room to be checked if the user is there. If null, leave the current room without checking
*/
public synchronized void leaveRoom(String room) {
if (isSameRoom(room)) {
DataObject removed = chatter.remove(); // leave the room
if (removed != null) // can be null if the 'chatter' object has already been removed.
removed.commit(); // let everybody know I did
System.out.printf("Left room %sn", chatter.getParent().getObjectID());
chatter = null;
message = null;
}
}
/**
* Check if the user is in the same room as the parameter
* @param room the name of the room to be checked if the user is there. If null, return true
* @return true if the user in 'room' or 'room' is null
*/
public synchronized boolean isSameRoom(String room) {
return chatter != null &&
(room == null || room.equalsIgnoreCase(chatter.getParent().getObjectID()));
}
/**
* Check whether the user running the application whose UUID is given is on the same room as us. This is done by getting
* our current ChatRoom object and see if it has a child with that ID.
* @param appUUID Application UUID to check
* @return true if the application is in the same room as us.
*/
public synchronized boolean isMemberOfMyRoom(UUID appUUID) {
try {
if (chatter == null)
return false;
ChatRoom chatRoom = (ChatRoom)chatter.getParent();
return chatRoom != null && chatRoom.getChild(Chatter.class, appUUID.toString()) != null;
} catch (NoSuchFieldException | IllegalAccessException ex) {
// Theoretically arriving here if a data object does not contain ObjectCode static field or the field is not public.
// In this case, send a command exception message.
sendExceptionMail(ex, "Cannot instantiate a data object class", null, false);
return false;
}
}
/**
* Get current room name, if any
* @return if the user is currently in a room return the room name, otherwise return null.
*/
public synchronized String getCurrentRoom() {
return chatter == null ? null : chatter.getParent().getObjectID();
}
/**
* Find the user whose name equals the parameter
* @param name user name to look for
* @return If a Chatter object for this user found then return its object ID, which is the UUID of the application
* that the given user runs, otherwise return null.
* @throws NoSuchFieldException If any of the referred data objects does not define ObjectCode static field.
* @throws IllegalAccessException If ObjectCode is not public.
*/
private String findUser(String name) throws NoSuchFieldException, IllegalAccessException {
FindUserFilter filter = new FindUserFilter(name);
Collection<Chatter> users = getRootObject().getFilteredChildren(filter);
// If the returned collection is not empty return the object ID (which is a UUID) of the first,
// otherwise return null.
for (Chatter user : users) {
return user.getObjectID();
}
return null;
}
/**
* Execute !history command
* @param arg command argument - <n>H or <n>D
* @return true if command has been parsed successfully.
* @throws NoSuchFieldException If ChatHistoryQuery does not define ObjectCode static field.
* @throws IllegalAccessException If ChatHistoryQuery.ObjectCode is not public.
*/
private boolean getHistory(String arg) throws NoSuchFieldException, IllegalAccessException {
// Parse the argument and calculate time
ZDate since = null;
if (arg != null) {
arg = arg.trim().toLowerCase();
int n = ZUtilities.parseInt(arg.substring(0, arg.length() - 1));
if (n <= 0)
return false;
int unit;
switch (arg.substring(arg.length() - 1)) {
case "h":
unit = ZDate.HOUR;
break;
case "d":
unit = ZDate.DAY;
break;
default:
return false;
}
since = ZDate.now().add(-n * unit);
}
// create a query and post it
ChatHistoryQuery query = createQuery(ChatHistoryQuery.class);
query.setUsername(myName);
query.setSince(since);
query.post(5 * ZDate.SECOND);
return true;
}
}The changes are in lines 201 and 209 that set the current
time in the time fields of the
created messages.
The other changes are in the classes that process messages.
Here is ChatMessageImp:
package org.spiderwiz.tutorial.lesson10.chat;
import java.util.Map;
import java.util.UUID;
import org.spiderwiz.tutorial.objectLib.ChatMessage;
import org.spiderwiz.tutorial.objectLib.Chatter;
import org.spiderwiz.zutils.ZDate;
/**
* Consumer implementation of the ChatMessage class
*/
public class ChatMessageImp extends ChatMessage {
/**
* Called synchronously when a chat message is received. If the message was not sent in the room we are in then ignore it.
* Otherwise if the message starts with '$' then print it and suspend execution for 20 seconds.
* If the message does not start with '$' return false to indicate that the message was not handled and shall be handled
* by onAsyncEvent().
* @return true to indicate that the event has been handled, false if it was not.
*/
@Override
protected boolean onEvent() {
if (ChatMain.getInstance().isSameRoom(getParent().getParent().getObjectID())) {
if (!getMessage().startsWith("$"))
return false;
printMessage();
}
return true;
}
/**
* Print out the message and suspend execution for 20 seconds.
* @return true
*/
@Override
protected boolean onAsyncEvent() {
printMessage();
return true;
}
/**
* Restrict message sending to:
* 1. applications that are not the current application.
* 2. applications that are on the same chat room as the current user, or the message is private.
* @param appUUID destination application UUID
* @param appName destination application name
* @param userID destination user ID
* @param remoteAddress remote network address
* @param appParams application parameters of the destination application
* @return true if the object should be sent to this destination
*/
@Override
protected boolean filterDestination(UUID appUUID, String appName, String userID, String remoteAddress,
Map<String, String> appParams
) {
return
!ChatMain.getInstance().getAppUUID().equals(appUUID) &&
ChatMain.getInstance().isMemberOfMyRoom(appUUID);
}
/**
* Allocate 5 threads to asynchronous processing of this object type.
* @return 5
*/
@Override
protected int getThreadAllocation() {
return 5;
}
/**
* Suspend execution for a random interval between 0 and 20 seconds, then Print out the message and its details.
*/
private void printMessage() {
try {
Thread.sleep(Math.round(Math.random() * ZDate.SECOND * 20));
System.out.printf("%4$s: From %1$s(%3$s): %2$sn",
((Chatter)getParent()).getName(), getMessage(), getTime().format(ZDate.FULL_DATE), ZDate.now(ZDate.FULL_DATE));
} catch (InterruptedException ex) {
}
}
}We changed the code of onEvent() (line 21) so that if the handled
message starts with a dollar sign (‘$’) then it is handled within the same
method synchronously. In this case the method returns true to indicate that the message has been processed. In other
cases the message returns false.
The Spiderwiz engine always tries onEvent() first, and if it returns false it calls onAsyncEvent() (line 35) asynchronously. Both of
these methods call the private printMessage()
method (line 72), which suspends the executing thread for a random time
interval between 0 and 20 seconds, and then prints the message details
including message creation time and the current time.
The other interesting override is of getThreadAllocation() (line 65). This tells the Spiderwiz engine how many execution threads to allocate for asynchronous processing of data objects of this type. We set it to 5. The default value, if the method is not overridden, is -1, which means that the framework determines the number of threads by the number of CPUs available on this machine.
There is also a change in ChatConsume that prints private messages:
package org.spiderwiz.tutorial.lesson10.chat;
import org.spiderwiz.tutorial.objectLib.Chat;
import org.spiderwiz.zutils.ZDate;
/**
* Consumer implementation of the Chat class
*/
public class ChatConsume extends Chat{
/**
* Called asynchronously when a private message is received. Print the sender name, time of the message and the message.
* @return true to indicate that the event has been handled.
*/
@Override
protected boolean onAsyncEvent() {
System.out.printf("%4$s: From %1$s(sent private-%3$s): %2$sn",
getName(), getMessage(), getTime().format(ZDate.FULL_DATE), ZDate.now(ZDate.FULL_DATE));
return true;
}
}Here we replace onEvent() by onAsyncEvent() and print message details including message creation time and current time.
There is no need for any other change, including not in the
configuration files. Just run everything (we tested it with User Manager and Room Producer of the previous lessons) and see what happens. With
the code as above, you should see that not more than 5 chat room messages are
processed simultaneously and that they are not printed in order (because they
have different suspension times). If a message starts with a dollar sign, which
causes synchronous processing, then you should see that everything hangs until
the message is processed and printed out, including the process of private
messages. If you change getThreadAllocation()
to return 1 then chat rooms messages are printed in their order with random
delays between them, and private messages are printed promptly.
An important point to note is that usually only the onEvent() method is processed synchronously. Other events, such as onRemoval(), onRename(), onInquire(), onReply() etc. are always handled asynchronously (onNew() is always synchronous because the notification of a new object must be handled before object events are processed). If you want to handle them synchronously override getThreadAllocation() to return zero.
A word about performance
The goal of Spiderwiz is not only to make the life of programmers easier, but also to let them develop robust and efficient applications. One of the most resource consuming operations in Java is the spawning of new execution threads. For this reason, Spiderwiz never spawns threads on the fly. All of them are created during application initialization and a sophisticated queuing mechanism is applied in order to execute all tasks quickly, efficiently and with the least resource consumption as possible.
You are apparently convinced that Spiderwiz is the state-of-the-art in service-mesh development and Java development in general. You may realize, however, that for the framework to work efficiently the entire system has to be developed under it, and wonder if there is an easy way to bridge between Spiderwiz applications and legacy frameworks. Well, definitely there is, and you will learn about it in the next lesson.
