Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Question about servlet architecture

843842Jan 14 2009 — edited Jan 14 2009
Hello,

I have a question about Web Application / servlet architecture and development, and haven't had any luck finding an answer so far. My plan must be either unusual, or wrong, or both, so I'm hoping the community here could point me in the right direction.

I have quite a bit of background in OSGi, but very little in servlets. I know my architecture would work with something such as sever-side Equinox, but for the open source project I'm contemplating, I think that the more mainstream Tomcat/servler route would be better -- assuming it can be done. Here is a rather simplified scenario:

There is a class, Manager. It could be a Java class that implements Runnable, or a non-HTTP servlet (GenericServlet?), but somehow it should be configured such that it runs as soon as Tomcat starts.

When Manager runs, it instantiates its primary member variable: Hashtable msgHash; mshHash will map Strings (usernames) to ArrayList<String> (list of messages awaiting that user). Manager also has two primary public methods: addMsg() and getMsgs().

The pseudocode for addMsg is:
void addMsg(String username, String msg) {
if username=="*" { for each msgList in msgHash { msgList.add(msg) } return;
if username not in msgHash { msgHash.add(username, new ArrayList<String>) }
msgHash[username].add(msg)
}

And for getMsgs():
String getMsgs(username) {
String s = "";
for each msg in msgHash[username] { s = s + msg; }
return s;
}

Once Manager has started, it starts two HTTP Servlets: Sender and Receiver, passing a reference back to itself as it creates the servlets.

The HTML associated with Sender contains two text inputs (username and message) and a button (send). When the send button is pressed and data is POSTed to Sender, Sender takes it and calls Manager.addMsg(username.value, message.value);

Receiver servlet is display only. It looks in the request for HTTP GET parameter "username". When the request comes in, Receiver calls Manager.getMsgs(username) and dumps the resulting string to the browser.

In addition to starting servlets Sender and Receiver, the Manager object is also spawning other threads. For example, it might spawn a SystemResourceMonitor thread. This thread periodically wakes up, reads the available disk space and average CPU load, calls Manager.addMsg("*", diskAndLoadMsg), and goes back to sleep for a while.

I've ignored the synchronization issues in this example, but during its lifecycle Manager should be idle until it has a task to do, using for example wait() and notify(). If ever addMsg() is called where the message string is "shutdown", Manager's main loop wakes up, and Manager shuts down the two servlets and any threads that it started. Manager itself may then quit, or remain resident, awaiting a command to turn things back on.

Is any of this appropriate for servlets/Tomcat? Is there a way to do it as I outlined, or with minor tweaks? Or is this kind of application just not suited to servlets, and I should go with server-side Equinox OSGi?

Thanks very much for any guidance!

-Mike
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 11 2009
Added on Jan 14 2009
1 comment
186 views