This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
Hi all! I've a problme with my servlet application. I'm developing a system where I expect heavy load on some occations. Almost every request ends up in me doing lookups in a database. My problme is how to not stack the request up, instead I would like to handle them as fast as possible. I have implemented this functionality so that each request in to the servlet (either a POST or a GET) is received, it finds out which kind of task this is about and puts the HttpServletRequest and HttpServletResponse object in a queue for that speciall task. It then finnished the doGet() or doPost() and is ready to receive more requests. Each task has its own queue and another thread that is polling the queue. When that thread gets the request and response object, it does all database stuff and writes the response to the response object and it consider the job done. This was what I thougt would solve my problem with heavy load on my servlet. But when I now tested it I think I have some problems. When doGet or doPost returns, it sends the response to the "caller" (is this correct?). I guess its that that happens since when I try to write the response later it is not possilbe since the HttpServletResponse object is in a illegal state. Can someone tell me if there is some way to solve this? How about services, how does that thing work? Or should it be possible to solve the way I have done it? Any suggestions are appriciated! Regards, // Andreas
while running , wht type of problems u are facing ?.
Joined: Mar 05, 2003
Well, as soon as the doGet() or doPost() returns, a empty page is sent to the browser. This page is empty since I havn't put anything into the HttpServletResponse object (I guess). There is also not possible to do a sendRedirect on the HttpServletResponse message (for the same reason?). I get a IllegalState exception. More precisly what I need is some way to tell something that "-hey, the response is finished, send it!". I guess that now the response is sent when the doGet() or doPost returns // Andreas
I have implemented this functionality so that each request in to the servlet (either a POST or a GET) is received, it finds out which kind of task this is about and puts the HttpServletRequest and HttpServletResponse object in a queue for that speciall task. It then finnished the doGet() or doPost() and is ready to receive more requests.
That is REALLY REALLY wrong. The request and response objects are managed by the servlet container and you only get to use them during a single request-response cycle. For example when you finished the doGet the output stream is closed and can't be reused which is why you get the illegal state. You should create an object that encapsulates the information you need to do the query to put in your queue, and also put that object in the session for that user. Subsequent requests can look at that object and see if it is done. The server can't initiate anything to send the data. The comment
ready to receive more requests
reflects a misunderstanding. Your servlet object may be handling any number of requests "at one time" Bill
i agree, you are misunderstanding on a fundamental level, the way that servlets work. The server is queueing the work for you by default. If the work involved is very time consuming (1000s milliseconds) then you can do some tricks like setting message queues but i wonder how heavy this work really is. I;ve seen servlets with 40 database hits cope with significant load well.
chanoch<p><a href="http://www.amazon.com/exec/obidos/ASIN/1861007736/" target="_blank" rel="nofollow">Author of Professional Apache Tomcat</a></p>
Joined: Mar 05, 2003
So what do you think of this solution: I do as before, save the response and request in a queue, but I also add a semaphore object in that encapsulation. After the servlet has put the stuff in the queue it waits for the semaphore to "release". On the other hand, when some service finally get the job from the queue and is finsished, it takes the semaphore from the job and releases it. The semaphore now "calls" the servlet so that it can continue, i.e send the response back. I realize I missunderstod the threading of the servlet object in the beginning, thanks for clarifying that. Many request can work on the same servlet at the same time. Some more about each service: I want to have the object in a queue so that I can myself decide how many service instances that works on that queue. It is also a way to be able to split the services on different machines. So what do you think of this semaphore solution? // Andreas