aspose file tools*
The moose likes Servlets and the fly likes when is the servlet destroy( ) method called ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "when is the servlet destroy( ) method called ?" Watch "when is the servlet destroy( ) method called ?" New topic
Author

when is the servlet destroy( ) method called ?

lavnish lalchandani
Ranch Hand

Joined: Feb 28, 2007
Posts: 79
http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/servlet/Servlet.html

public void destroy( ) : Called by the servlet container to indicate to a servlet that the servlet is being taken out of service. This method is only called once all threads within the servlet's service method have exited or after a timeout period has passed. After the servlet container calls this method, it will not call the service method again on this servlet.

What exactly is meant by the line in bold ?
my analysis ...

If the destroy is called "when all threads within the servlet's service method have exited " then if new request comes will it reload again .. this will be v v inefficient .. and i haven't seen this happening , on deploying a hello world even if you give only one request and wait for next few hours (without giving any req) , the destroy( ) is not called

"after a timeout period has passed." what timeout are they talking about ??? how do i define this time out. I want to make a hello world servlet and configure this time out to a small value lets say 60 sec and see the print statement of destroy() how do i do that ?

I know its called when
  • web application is shut down
  • application server ( hence web app) is shut down


  • Is it also called when there is memory shortage and the server chooses to remove this servlet instance from memory ?

    Is it also called when the servlet class file is changed ( the app server automatically reloads the new servlet ) ?


    lavnish.blogspot.com
    Ashwin Pai
    Ranch Hand

    Joined: May 20, 2008
    Posts: 90
    This method is only called once all threads within the servlet's service method have exited or after a timeout period has passed

    What this means is when ever Servlet container needs to invoke destroy , it checks if there are any running threads. If there are it waits for a specific time interval before it unloads the servlet. I dont think the time interval can be changed.

    The destroy method is called when the appluication is shut down and when the container thinks it requires more memory. Since modifying a servlet and loading the new instance involes destroting the old instance, your are correct the destroy method will be invoked.

    HTH
    Ashwin


    The only way to make your dream come true is to wake up.
    Ulf Dittmer
    Marshal

    Joined: Mar 22, 2005
    Posts: 39547
        
      27
    Is it also called when there is memory shortage and the server chooses to remove this servlet instance from memory ?

    It's highly unlikely that deallocating a servlet will free up significant memory, so I doubt that this happens in practice.

    Is it also called when the servlet class file is changed ( the app server automatically reloads the new servlet ) ?

    Web apps -and the servlets they contain- are much more complicated beasts than they were when servlets were first introduced. Reloading an individual servlet isn't really practical any more these days, so the container would more likely reload the whole web app.

    The destroy method is called when the appluication is shut down and when the container thinks it requires more memory.

    No, this is not correct. The destroy method is called whenever the container decides to take the servlet out of service. The servlet spec says nothing when that might happen, so the developer should not make any assumptions about when (and how often) it might happen.

    The init and destroy methods need to be written in a way that they can be could multiple times during the lifetime of a web app without problems. Now, in most cases, the container will allocate the servlet once during application startup (or when the servlet is first accessed), and deallocate it when the application shuts down. But that is not mandated by the spec, so the developer can't rely on it.


    Ping & DNS - updated with new look and Ping home screen widget
    lavnish lalchandani
    Ranch Hand

    Joined: Feb 28, 2007
    Posts: 79
    thank you ashwin and ulf
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: when is the servlet destroy( ) method called ?
     
    Similar Threads
    destroy method and synchronization
    when destroy method is called
    destroy() method call in sevlet's life cycle
    Re: Closing database connection in destroy metod()?
    ServletException