When bringing down apache-tomcat, I get several of the below messages:
DATE org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
SEVERE: The web application [/blabla appears to have started a thread named [Timer-1] but has failed to stop it. This is very likely to create a memory leak.
Yes each application has one or more main threads, including perhaps one for garbage collection.
The thread methods destroy and stop are deprecated. How do I tell apache-tomcat what to do when it wants to terminat the application??
How to stop threads safely depends a lot on how those threads were started. If you have a java.util.Timer, then you should call the cancel() method on it. If you have an ExecutorService you should call shutdown() or shutdownNow() on it, and make sure any tasks you have run can handle interrupts correctly. If it is a Thread you start yourself then you will need to provide some means to stop it safely (i.e. a method which can be called by a different Thread which sends a signal to end, and code in the work which responds to that signal and causes the work to stop and clean up results).
You don't have to worry about any thread that the Tomcat Server starts, so the GC is not your problem. You only need to make sure any threads you start are stopped.
Joined: Aug 20, 2010
When I was speaking of GC, I meant my own stuff. It is more like flushing active stuff logging off inactive users, ..., just in case.
Anyway, the real problem is knowing when to stop. I see the "cancel" method for TIMERS, not sure of what the comparable thing is for a RUN method.
However, I could set a global variable that all threads could see (static shared java bean) and then do the right thing.
How do you know that tomcat is shutting down so I can set this variable? Does it call some method of a "HttpServlet" (all of my threads are descendents of HttpServlet).
There is no similar thing for Run methods. If you want one you have to build it. One method is to use the interrupted status of the working Thread:
You may need to do a bit more work to make sure the stop command is accounted for if stopped before the run() method actually starts... You can do similar work with a volatile boolean, but you will need to pay attention to interruption any way since there are other means of the Thread being interrupted, and you will need to handle them.
How do you know the server is shutting down? The best thing to do is use a ServletContextListener's servletDesroyed() method. That gets called whenever the servlet context is being shut down (when the application is being unloaded, i.e. for updates to that particular webapp, or when the server completely shuts down).
Joined: Aug 20, 2010
Thanks for the example, will try. The only thing bothering me is stop() - I thought that "stop", "destroy" and "suspend" were deprecated.
The stop() etc... methods in the Thread class are deprecated. I would suggest you not use a Thread (it is rare that you ever have to extend a Thread) but rather use a class that implements Runnable (and pass the Runnable into the Thread's constructor). Then you can create a stop() method. If you would rather call it something else, feel free.
The point is (and this is described in the JavaDocs) that those deprecated methods are deprecated because they cause an unsafe means of halting Thread activity. You provide a safe means of halting Thread activity by placing checks in safe locations in the running code itself. Whatever you name the method which signals the running code to stop doesn't really matter.