Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Servlets and the fly likes Servlet Destroy() Method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet Destroy() Method" Watch "Servlet Destroy() Method" New topic
Author

Servlet Destroy() Method

Mukesh Mittal
Greenhorn

Joined: Dec 29, 2005
Posts: 16
I have a question regarding calling servlet Destroy method.

I know, container call destroy method to remove servlet instance.

Just for testing purpose, I called destroy method in the same servlet. Nothing happened? I am calling the same servlet again and system is not calling my init method and system is doing the same previous work.

Can we call destroy method?
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

The destroy method gets called by the container.
It's just a place for you to put your own clean up code; the code you want called before the servlet is destroyed.

Calling it yourself does absolutly nothing.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
Here is how I understand it; The server may decide to remove a previously loaded servlet instance. But, before it does, it calls the servlets's destroy method. This gives the servlet to release resources, close DB connections, etc. Not sure what happens when you explicitly call destroy (probably nothing as you are experiencing) Init() is only called once when the servlet is first loaded.


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
Mukesh Mittal
Greenhorn

Joined: Dec 29, 2005
Posts: 16
Thanks for the reply guys. I think Bosun, your are right, Container is calling destroy method before actually unloading instance.
dema rogatkin
Ranch Hand

Joined: Oct 09, 2002
Posts: 294
What cases when a container reload a servlet? Does it happen when .awr file got updated? Any other situation?


Tough in space?, <a href="http://tjws.sf.net" target="_blank" rel="nofollow">Get J2EE servlet container under 150Kbytes here</a><br />Love your iPod and want it anywhere?<a href="http://mediachest.sf.net" target="_blank" rel="nofollow">Check it here.</a><br /><a href="http://7bee.j2ee.us/book/Generics%20in%20JDK%201.5.html" target="_blank" rel="nofollow">Curious about generic in Java?</a><br /><a href="http://7bee.j2ee.us/bee/index-bee.html" target="_blank" rel="nofollow">Hate ant? Use bee.</a><br /><a href="http://7bee.j2ee.us/addressbook/" target="_blank" rel="nofollow">Need contacts anywhere?</a><br /><a href="http://searchdir.sourceforge.net/" target="_blank" rel="nofollow">How to promote your business with a search engine</a>
Vishnu Prakash
Ranch Hand

Joined: Nov 15, 2004
Posts: 1026
From Spec


SRV.2.3.4 End of Service
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.

After the destroy method completes, the servlet container must release the
servlet instance so that it is eligible for garbage collection.


Servlet Spec 2.4/ Jsp Spec 2.0/ JSTL Spec 1.1 - JSTL Tag Documentation
dema rogatkin
Ranch Hand

Joined: Oct 09, 2002
Posts: 294
Is there any real servlet container which follows the spec and unload servlet randomly to save resources?
Philippe Desrosiers
Ranch Hand

Joined: Mar 29, 2006
Posts: 138
For some reason, the destroy() method in my HttpServlet does not seem to be called when I shutdown the container. I'm running under Tomcat 5.5.15. When I run the shutdown.sh script, Tomcat shuts down like nothing's wrong, but the destroy() method is never called.

So far, the only situation I've found where this might happen is if the init() method threw an UnavailableException or something, but as far as I can tell, the init() method is returning just fine, not errors.

Any ideas?
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

How do you know the destroy method is not being called?
Philippe Desrosiers
Ranch Hand

Joined: Mar 29, 2006
Posts: 138
Originally posted by Ben Souther:
How do you know the destroy method is not being called?


I have a logging event (actually, I use log4J, but here is a simplified version).



And no message is appearing in the log.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

I just happen to be running 5.5.15 so I gave it a shot




The following showed up in tomcat/logs/catalina.out:



How are you testing this and what results are you seeing?
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

We klunked heads....

Are you actually hitting the servlet (to make sure it's loaded) before shutting tomcat down?
Philippe Desrosiers
Ranch Hand

Joined: Mar 29, 2006
Posts: 138
Strictly speaking, the servlet doesn't handle any requests, so it shouldn't really be an HttpServlet, but there you have it. All logging in the init() method outputs just fine. Basically, to test, I:

1) start tomcat
2) look at the log
3) stop tomcat
4) look at the log

At step 4, I would expect to see evidence that the destroy() method was called, but there is nothing.

as an optional step 5), I run ps axf, to make sure that tomcat is completely shut down (and it is).

At step 4, In catalina.out, I have the following:

As you can see, log4j is complaining, which is strange, since the loggin works fine throughout the rest of the servlet, and only falls down during the destroy() method. So perhaps destroy() is being called, but by that time log4j has been unloaded, or something.

Then, of course, there's the "Failed shutdown of Apache Portable Runtime" message...
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

I would first check that the init and destroy method are being called.
Use System.out.println not log4j.
The logs indicate that the problem might be with log4j configuration.

If you can't hit the servlet with a browser, you'll need to use the load-on-startup attribute in the servlet entry of your web.xml page.

If you can see that the init method is working but not the destroy, then you may have something there.

If both are working with System.out.println, then you may need to fish for a problem with your log4j configuration.
Philippe Desrosiers
Ranch Hand

Joined: Mar 29, 2006
Posts: 138
Originally posted by Ben Souther:
I would first check that the init and destroy method are being called.
Use System.out.println not log4j.

I tried this, but I can't figure out where System.out.println is "printing" to?
If you can't hit the servlet with a browser, you'll need to use the load-on-startup attribute in the servlet entry of your web.xml page.
Yep. This is done. Servlet loads on startup just fine (all the logging in the init() method is showing up in the logs without a problem).

If both are working with System.out.println, then you may need to fish for a problem with your log4j configuration.


Any idea where the System.out.println would output to?
Philippe Desrosiers
Ranch Hand

Joined: Mar 29, 2006
Posts: 138
Oh yeah, and to complicate things, I'm also using Spring-beans, with the following in my web.xml:


Which is apparantly some listener that checks the log4J configuration file to see if it's changed, and reloads Log4J for the container (or something). So, by unloading the Spring framework (which maybe happens before the destroy() method is called), I might be unloading log4J as well.
Philippe Desrosiers
Ranch Hand

Joined: Mar 29, 2006
Posts: 138
So, by unloading the Spring framework (which maybe happens before the destroy() method is called), I might be unloading log4J as well.
Confirmed. This was the problem. I loaded Log4J in the regular way, without using the Spring listener, and the problem went away.
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
Originally posted by Philippe Desrosiers:

Any idea where the System.out.println would output to?


It prints 'output' to the DOS prompt (which gets open when you start tomcat).

But my doubt is, for destroying servlet, you are making tomcat shut down (right?), so that will close the DOS prompt (which was opened when you started tomcat), then how you will see the 'output' (SOPs)???

Thanks.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Servlet Destroy() Method
 
Similar Threads
Destroy Method in servlet constructor
calling destroy() from init()
destroy() method
Call destroy() inside init()
destroy() method of servlet.