File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Servlet Destroy() Method

 
Mukesh Mittal
Greenhorn
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bosun Bello
Ranch Hand
Posts: 1510
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mukesh Mittal
Greenhorn
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply guys. I think Bosun, your are right, Container is calling destroy method before actually unloading instance.
 
dema rogatkin
Ranch Hand
Posts: 294
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What cases when a container reload a servlet? Does it happen when .awr file got updated? Any other situation?
 
Vishnu Prakash
Ranch Hand
Posts: 1026
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
dema rogatkin
Ranch Hand
Posts: 294
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there any real servlet container which follows the spec and unload servlet randomly to save resources?
 
Philippe Desrosiers
Ranch Hand
Posts: 138
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do you know the destroy method is not being called?
 
Philippe Desrosiers
Ranch Hand
Posts: 138
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We klunked heads....

Are you actually hitting the servlet (to make sure it's loaded) before shutting tomcat down?
 
Philippe Desrosiers
Ranch Hand
Posts: 138
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 138
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 138
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 138
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3830
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic