简体   繁体   中英

Calling servlet's destroy method

As per the link http://www.xyzws.com/Servletfaq/when-is-destroy-of-servlets-called/20 , one of the reason of calling destroy method is when the servlet hasn't got a request in a long time .

I was thinking there could be some pages that don't get called for a long time. So, does that mean destroy will be called and they will be no longer used?

Actually, I was asked this question in interview and he told me that destroy method will only be called when server is shut down.

Appreciate any help on this.

AFAIK,

In java servlet, destroy() is not supposed to be called by the programmer. But, if it is invoked, it gets executed. The implicit question is, will the servlet get destroyed? No, it will not. destroy() method is not supposed to and will not destroy a java servlet.

The meaning of destroy() in java servlet is, the content gets executed just before when the container decides to destroy the servlet. But if you invoke the destroy() method yourself, the content just gets executed and then the respective process continues. With respective to this question, the destroy() gets executed and then the servlet initialization gets completed.

destroy() method is invoked first, then Servlet is removed from the container and then eventually garbage collected. destroy() method generally contains code to free any resources like JDBC connection that will not be garbage collected.

Couple of cases :

  1. when the container shuts down or the application shuts down;
  2. when the container decides that there is a shortage of memory;
  3. when this servlet hasn't got a request in a long time.

As per the Doc

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.

From the specification ( The Servlet Interface -> Servlet Life Cycle -> End of Service ):

Once the destroy method is called on a servlet instance, the container may not route other requests to that instance of the servlet. If the container needs to enable the servlet again, it must do so with a new instance of the servlet's class.

The specification is referring to a particular instance of the servlet that is being destroyed.

So, does that mean destroy will be called and they will be no longer used?

It doesn't mean that the servlet whose instance is destroyed is never used. It's only that particular instance that is not used as it would have been garbage collected. When a request comes in for that servlet, the container would create a new instance of that servlet and performs all the life-cycle actions till serving the request, again.

This is one of those classic spec vs implementation things. Servlet containers like Tomcat are allowed by the specification to maintain a pool of Servlet objects, creating and destroying them as load goes up or down. In this kind of setup, destroy would be called when the particular instance was no longer needed. It would even be legal to create a new instance for each request, calling init immediately before the request is processed and destroy straight after.

However in practice virtually every Servlet container maintains exactly one instance of each Servlet. Since Servlets are required to be thread safe this is also perfectly ok. In this scenario destroy is only called when the context is shutdown.

I can't think of many cases where you would want to pool the Servlets, but it is allowed by the spec.

It totally depends on the implementation of the servlet container you are using. If your servlet container is very conservative for saving resources, it may destroy the servlet that is not in service for long time.

In page 22 of servlet spec 2.5, you will find that, "When the servlet container determines that a servlet should be removed from service, it calls the destroy method of the Servlet interface to allow the servlet to release any resources it is using and save any persistent state. For example, the container may do this when it wants to conserve memory resources, or when it is being shut down."

Servlet.destroy() 's javadoc says: "Called by the servlet container to indicate to a servlet that the servlet is being taken out of service". It doesn't specify what situations would cause a servlet to be "taken out of service", it is simply an event to which you can react to if you need to. For example you might want to close some resources you have open, or persist some state to storage.

The most obvious reason for this is that the servlet container is being shut down, but a container could equally well decide that it's time to unload a servlet because it's not been used in a month and is simply consuming resources. That behaviour is entirely up to the implementation, and it is not something you (as a servlet designer) should be concerned about.

Servlet's destroy method is a callback. These are the functions called by the container when a particular event is about to happen (in this case servlet about to be destroyed). Thus the whole purpose is if you want to do something (eg: cleaning up the resource) you can do those under this callback.

From Servlet Specification:-

destroy() method is 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 destroy() method, it will not call the service method again on "THIS SERVLET".

The servlet container is not required to keep a servlet loaded for any particular period of time. A servlet instance may be kept active in a servlet container for a period of milliseconds, for the lifetime of the servlet container (which could be a number of days, months, or years), or any amount of time in between.

When the servlet container determines that a servlet should be removed from service, it calls the destroy method of the Servlet interface to allow the servlet to release any resources it is using and save any persistent state. For example, the container may do this when it wants to conserve memory resources, or when it is being shut down.

Before the servlet container calls the destroy method, it must allow any threads that are currently running in the service method of the servlet to complete execution, or exceed a server-defined time limit.

Once the destroy method is called on a servlet instance, the container may not route other requests to that instance of the servlet.

If the container needs to enable the servlet again, it must do so with a new instance of the servlet's class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM