wood burning stoves 2.0*
The moose likes Servlets and the fly likes accept and process commands in the same servlet? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "accept and process commands in the same servlet?" Watch "accept and process commands in the same servlet?" New topic
Author

accept and process commands in the same servlet?

sajjad ahmad
Ranch Hand

Joined: Jan 23, 2003
Posts: 78
HI !

I am currently working on a HTTP based chat server .In my server I have a single servlet which gets commands from clients , to process those commands I have used command pattern .Since there could be any no of clients connected to my server at a time say 1-10000 I don�t want my servlet to process each request for performance issues. what I do in servlet that I make a command object as soon as I get a command from client and store it to a queue (LinkedListBlockingQueue).Using this technique my servlet is basically receiving requests and storing them to a queue which obviously is a lighter task as compared to the process in whihc servlet had to accept and process each request. Now the thread(s) listening to the queue (LinkedListBlockingQueue) will be notified as soon as a command is stored in the queue and it will get all those commands from queue and execute them one by one.
I just need your suggestion that is it a good design strategy or I should accept and process requests in the same servlet?

Waiting for your kind reply
Sajjad Ahmed Paracha
[ August 21, 2007: Message edited by: Bear Bibeault ]
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Yoiks.

JEE and containers in particular take care of most of these issues for you. Trying to implement it yourself, particularly when you aren't sure whether it is required or not, is more likely to make things worse than better.

Just my opinion, but not something I'd try.
sajjad ahmad
Ranch Hand

Joined: Jan 23, 2003
Posts: 78
Dear David O'Meara !
Thanks for your quick reply , i am affraid i couldnt understand your point here can you please describe it furthur more.
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6661
    
    5

Each request to the server is executed in a separate thread. I am not sure if your scenario calls for the use of the command or observer patterns. Are you experiencing high CPU times when so many clients are accessing your application ? There might be other bottle necks


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Containers manage thread pools to improve running of multiple tasks and depending on the server you are using you may have access to worker and queue threads which already provide handling of high volume periods.

Trying to implement something that already exists may cause problems and worse you may get it wrong.
sajjad ahmad
Ranch Hand

Joined: Jan 23, 2003
Posts: 78
In case of a Chat server implementation we have to make sure that if a client (say a@ourdomain.com) is sending chat messages to another client(say b@ourdomain.com) those messages should be sent to the other client(b@ourdomain.com) in the same order in which our client (a@ourdomain.com) sent.
say client a sent following 10 messages to client b , from it's chat window
a says :
1
2
3
4
5
6
7
8
9
10

client b must also see the messages in the same order , as sending messages without the order wil not make sense to b.
Now if i use the server implementation of managing requests in seprate Threads there might be a possibilty that thread having message 9 executes before thread having messages 8 which will result that disorder in messages and b will see those disordered messages.might be like this

a says :
1
3
2
4
5
8
7
6
10
9

so in order to send b message in exectly the same order we need to first queue all those messages (command objects) in a FILO queue and then send them to b in same order.
so i guess for this case we cant rely on the server's implementation of request processing.
so what do you think?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: accept and process commands in the same servlet?