• Post Reply Bookmark Topic Watch Topic
  • New Topic

Destroy() method not called  RSS feed

 
Nihar Desai
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

New to this forum and servlets writing but hopefully everyone's experience here will help. I am running Tomcat 7 on a Windows 7 Machine and am learning to write servlets however I am stuck while trying to follow a tutorial.

In destroy() I am writing a simple file using FileOutputStream to store the date the servlet was first called.

However, destroy() method is not called. I verified this by println() as well as moving the file creation to init().

When moved to init() the file is created just fine, and on next load I can read the file fine. I am convinced destroy() is not called.

I stop the service via the manager (the little icon on the bottom "stop service");

Why is destroy not being called?
 
Rajesh Ronald Reagan
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Nihar,

Servlet's destroy() method is to basically to unload the servlet.

Primarily this is used for GC.

And during this time you cannot initialize any rather than closing it.

Good practice of Servlets is to not call the destroy() method explicitly.

May be you could do like the following.




Regards,
Ronald
 
Nihar Desai
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rajesh,

Thanks for the reply.

I actually am not calling the destroy() method. In the destroy() method I simply have code that will write some variables to file. The code is correct as written (no errors in the code), because, for testing, when I move it to init() under the case that the FileNotFound it creates and writes to the file. However I want this to happen when the server is shutdown.

It is my understanding that destroy() is called whenever the server is shutdown as part of the server shutdown, however for the reasons given above the method is never being called by the server itself when I "stop service."
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think the container is obligated to destroy the servlet instance when you "stop service" - just keep the servlet offline.

What happens when you shutdown the server entirely?

Bill
 
Nihar Desai
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bill,

Thanks for the reply. The only options I have are to Start and Stop service via my gui (Tomcat Manager) on windows. I am trying to find out how to "shutdown" the Tomcat 7 on Windows 7 presumably through CMD, but can not find a way maybe if you know how to you can guide me.

However, the tutorial I was following was running on I believe a Tomcat 5.x and when he start and stopped the service via gui the destroy() method was called, maybe they changed this feature in later versions of Tomcat.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you installed Tomcat as a service, you should be able to shut it down and restart via the Task Manager (win 7). If you start the Task Manager you will find a Services tab.

I am new to Win 7 - with XP we had a separate services manager started via the control panel.


In my opinion, developers should stick with installing Tomcat as an application so it will be easier to completely stop and do a clean restart.

Bill

 
Sudip Bhandari
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just tested on my own server setup to see when would destory get called. Closing the browser and  http connection didn't call it. However when I stopped my server (tomcat) in my case, destory callback got called. I guess it's not a good idea to rely on destory() to do some important tasks.

command for killing the server:

  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!