Hello all.
I am using applet <---> servlet communication for a chat (one to one) program.
Currently, the applet makes requests every 2 sec. to get new messages. The Servlet on the otherend responds immediately with a message (if any) or responds as a String 'nothing'.
As its clear that there are many useless req - res cycle. To avoid this, I wanted to have the servlet respond a particular request from an applet only when there is a valid message.
Therefore, soon as the request is recieved by the servlet, a method is called where it looks for a new message in a loop.
If the message is found the response is made else Thread.sleep(4000) is used and it's repeated till the condition is matched. Here is the outline....
public void doGet(req,res)......
{
checkCondition(res);
}
void checkCondition(Http.. res)
{
String cond = "nothing";
while(cond.equals("nothing"))
{
..... checking for new message
..............................
cond = "a new message";
if(cond.equals("nothing"))
{
Thread.Sleep(4000);
if(requester_Does_Not_Exist_Anymore)
{
cond = "The End";
}
}
}
sendResponse(cond,res);
}
......note that I am also using a mechanism to check if the applet is still existing.
Firstly, is it a correct approach? if not, please suggest one.
When I tried as above, the chat program executes, but the server's CPU Usage reaches 100%.
What could be the problem? Is it because the servlet-engine (JRun) tries to forcefully keep the http connection open? I also considered J.Hunter's method of using Observer and Observable interface and class technique (servlet programing, Oreily) but, that does not suit for one to one chat concept as it notifies all the waiting threads when a new message is added.
Any suggestion over this will be of great help to me.
KB
[This message has been edited by Kruger Brent (edited July 24, 2001).]
[This message has been edited by Kruger Brent (edited July 25, 2001).]