• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to test destroy() in a servlet  RSS feed

 
Prashant Vasudeo
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
I want to know that how to test the destroy() method in servlets.
I have read that the conatainer will call the destroy() method if it is going to remove the servlet instance from memory OR after "Specific server timeout". I want to know how to do the configuration setting for server timeout period on weblogic 7.0.
I tried to print a number in infinite for loop in doPost() method of a servlet so that after "TIMEOUT" it will call the destroy method.
it was not calling the destroy method even after 10 minutes.
I was referring to the servlets tutorial for how to make gracefull exit in servlet . As getting one instance variable and incrementing it after each request and in destroy method check for no. of threads which are still doing processing. This i have to do the code for the same, but before that i want to know how to configure timout.
regards
parshant
 
Pradeep bhatt
Ranch Hand
Posts: 8933
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
RE Deploy the webapp, the destroy method will be called.
 
Prashant Vasudeo
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks...i will test that.
but tell me how to set the timeout setting for a request on weblogic 7.0
regards
prashant
 
Prashant Vasudeo
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
its not working it's goes in the loop but after redeploy...it never stops..
the code is as follows
package servlets;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServlet;
public class MyServlet extends HttpServlet
{
public void init()
{
System.out.println("inside init()");
}
public void doGet(HttpServletRequest req, HttpServletResponse res)
{
System.out.println("inside doGet()");
for (int i =0; ;i++ )
{
System.out.println("i is "+i);
}
}
public void destroy()
{
System.out.println("inside destroy()");
}
}

regards
prashant
 
Pradeep bhatt
Ranch Hand
Posts: 8933
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remove that loop. Why have you put it.
1. Deploy you web app.
2. Redeploy you web app i.e. do a hot deployment. You will see destroy method of the servlet being called.

As far as I know, there is no way to set the timeout of a servlet. I am sure that I am correct.
 
Prashant Vasudeo
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
i was trying to test the timeout period. for that i made a infinite loop.
if i remove the infinite loop , then yes..the destroy method is called after redepoyment.

Here is the wording of servlet tutorial.
"All of a servlet's service methods should be complete when a servlet is removed. The server tries to ensure this completion by calling the destroy method only after all service requests have returned or after a server-specific grace period, whichever comes first."
the page link is
http://java.sun.com/j2ee/tutorial/1_3-fcs/doc/Servlets12.html
regards
prashant
 
Pradeep bhatt
Ranch Hand
Posts: 8933
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know that. Can you please tell me what are you trying to convey?
 
Prashant Vasudeo
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to test that if i send a request to a servlet and the thread is taking more time than that of server grace period, in this case the web container will call the destroy() method for it.
I want to test this thing , that's why i put an infinite loop in doPut() , so that after i gave request to the servlet , it will go in loop and response will not come ..so that after server timeout the container will call its destroy() method.
After reading the tutorial, i want to test the gracefull exit for a servlet instance, I will send no. of requests and i will make changes the program so the the in doPut() , it will sleep more than that of server timeout, and check in the destroy() method, which will be called after server timeout, that all the threads have been processed the request by the servlet instance.
please let me know , do u want anymore desc.
thanks,
prashant
 
Pradeep bhatt
Ranch Hand
Posts: 8933
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is up to the servlet engine when it wants to destroy the servlet instance. The container may have its own timeout period and I am not sure how many servers allow you to configure the timeout period. Weblogic 6.1/7.0 does not allow this configuration and I do not know about the latest version.
Your approach will not lead to the servlet being destroyed. Putting an infinite loop will only result in response not being sent to the client and *not* the destroy method being called.
 
Prashant Vasudeo
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for the same.

regards
prashant
 
Sam Shukla
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think ability to timeout a servlet is really required attimes.
I have seen this implemented via wrapping operations within a stateless session bean transaction.
Anyone have another approach ?
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!