hi all, i have a question regarding invoking a thread from a servlet. i have a servlet that has to do long processing and can't respond client fast. the problem in this is the proxy times out before servlet can come back with a response and then users see "proxy time out error". we want to ignore it. we want to assume that everything worked fine in the backend once users hit on "Submit" button of the form and they should get a response back immediately. now, we can't do both in the same servlet-processing + getting back to client with a response. so what i want to do is- invoke a thread and pass appropriate parameters to it so that even when the servlet gets back to client saying "processing is done" that thread keeps running and so we can avoid "proxy time out error". what might be the problems in this? - if my servlet is over then what happens to the child thread? - how does this will get affected across various instances of webservers (say it works in development but fails to work similarly in production due to some class loading differences b/w the webservers etc...) please guide me. thanks maulin.
You can certainly use a servlet to start a Thread to do long processing. Probably the best approach is to create a helper class that implements Runnable and conduct all operations in an instance that you store in the session for that user. The helper should have some variable that indicates how close to done it is. Your servlet can then report how close to done the job is when it isn't finished, and the final result when it is. By carefully isolating all servlet specific methods from the helper class, you can test it off-line, which is always a help to debugging. Bill
You can create a class to perform the thread operation and when the thread is finished it can set a variable to true. After the thread is invoked forward to the client and inform them that processing is occuring. Hit the servlet every so often and check for to see if that variable has been set to true. If it has you can then perform some other operation, otherwise, redirect back to the 'waiting.jsp'.
No more rhymes! I mean it!<br /> <br />Does anybody want a peanut?
Joined: Nov 04, 2001
hi, thanks to both Williams well, i don't really need to keep user aware that sth is being done...its not a end user interface. only one/two persons are going to see that interface (just we had to make certain functionality via the interface rather than the prompt invocation to keep away problems from the person who is going to use it) and they know the problem of proxy timeout by the browser but its just that we want to avoid the proxy timeout error in the display you know. but sure, your solutions i have to think about. in both solutions it seems i have to keep Servlet running in loop till my thread is running, right? William Borgden, if i store a thread object in the session will it really help? because the thread is executing in the webserver so when i put it in a session it will get the "the then" status and push it to session so when we get back the same object from session it will have the same status we earlier had (the actual object on webserver keeps changing it's status right?).
in the second solution (William Duncan), i will have to make request to my servlets periodically from the response page that says "wait.." message right? (i can do it via Refresh header i guess) but here how the servlet knows the status of the initiated thread object (which was created by a perivious servlet call) ? thank you for response. i will think and let you know what i am ultimately doing. i have never done this thread thing from a servlet. regards maulin.
Author and all-around good cowpoke
Joined: Mar 22, 2000
You don't "store a Thread object in the session" you create a class implementing Runnable that encapsulates the job to be done. Create an instance of the Runnable object, attach and start a Thread to that object and store the object in session. The Thread will run independently of the servlet request/response cycle that started it. When the Thread exits the run method, the object will still be there in the session when the user makes another request. I think you are making things too complicated, don't think about the Thread, think about the object containing the run() method that the Thread executes. Bill