• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

servlet to respond when a condition is matched...

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was just wondering if u can use Observable interface.Only when the message is there then it is sent across otherwise not
 
Kruger Brent
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Observer interface can be used if the message is to be broadcasted to all waiting threads. But in this case, there is one thread to be broadcasted.
KB
 
Kruger Brent
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
Why no reply?
KB
 
reply
    Bookmark Topic Watch Topic
  • New Topic