• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

when is the servlet destroy( ) method called ?

 
lavnish lalchandani
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ) ?
     
    Ashwin Pai
    Ranch Hand
    Posts: 90
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
     
    Ulf Dittmer
    Rancher
    Posts: 42967
    73
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    lavnish lalchandani
    Ranch Hand
    Posts: 79
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    thank you ashwin and ulf
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic