Hi,

thank you for your reply.
First I'd like to say that I do implement ServletContextListener at all 
beacause destroy() fires after the tomcat warnings.

That being said, on this website: 
http://stackoverflow.com/questions/2255884/jython-and-implementing-httpservlet-contextinitialized
 
is written that  "You can optionally also let your servlet both extend 
HttpServlet and implement ServletContextListener"
It is also said that "it is not always considered a good practice."

Why is that so ?
Would that be an explanation why in my case I can access some variables but 
others not ?

Thank you for your help.

-----Ursprüngliche Nachricht-----
Von: Pid <p...@pidster.com>
Gesendet: 26.05.2010 20:45:53
An: Tomcat Users List <users@tomcat.apache.org>
Betreff: Re: Stopping a Timer in contextDestroyed() to avoid memory leaks 
results in a NullPointerException

>On 26/05/2010 19:43, devn...@web.de wrote:
>> Hi,
>> 
>> Thanks for reading this.
>> 
>> I am developing a webapp and do not understand the following:
>> In my app, I create a Timer when the servlet starts.
>> When tomcat stops, I try to stop the timer in order to avoid memory leaks.
>> 
>> My question is: why do I get a NullPointerException while trying to access 
>> mytimer in contextDestroyed().
>> 
>> The simplified code:
>> 
>> public class myclass extends HttpServlet implements ServletContextListener {
>> 
>>        java.util.Timer mytimer;
>>        int testint;
>> 
>>        public void init() throws ServletException {
>>                mytimer = new Timer("__test__");
>>               TimerTask taskPerformer = new TimerTask() { public void run() 
>> { whatever_function(); } };
>>                mytimer.scheduleAtFixedRate(taskPerformer, 10, 10, 864000);
>>               testint = 12;
>>        }
>
>Why mix & match the Servlet and ServletContextListener methods?
>
>
>p
>
>>        public void contextDestroyed(ServletContextEvent event) {
>>               System.out.println("Inside contextDestroyed, 
>> testint="+testint);
>>               try {
>>                      mytimer.cancel();
>>               }
>>               catch (Exception e) {
>>                      System.out.println("An error occurred inside 
>> contextDestroyed(): "+e);
>>               }
>> }
>> 
>>        public void contextInitialized(ServletContextEvent event) {
>>               // Nothing here
>>        }
>> 
>>        public void destroy() {
>>              // Nothing here
>>        }
>> }
>> 
>> The Output:
>> May 26, 2010 8:06:30 PM org.apache.catalina.core.StandardService stop
>> INFO: Stopping service Catalina
>> Inside contextDestroyed(), testing=12
>> An error occurred inside contextDestroyed(): java.lang.NullPointerException
>> May 26, 2010 8:06:30 PM org.apache.catalina.loader.WebappClassLoader 
>> clearReferencesThreads
>> SEVERE: A web application appears to have started a thread named [__test__] 
>> but has failed to stop it. This is very likely to create a memory leak.
>> 
>> What disturbs me, is that my other variable "testint" is accessible within 
>> contextDestroyed, but not mytimer.
>> 
>> If I save my timer in init() like this:
>> 
>>       ServletContext sc = getServletContext();
>>      sc.setAttribute("mytimer", mytimer)
>> 
>> and if I modify contextDestroyed() so that it accesses my timer through 
>> ServletContext
>> 
>>      ServletContext context = event.getServletContext();
>>      Timer mytimer2 = (Timer)context.getAttribute("mytimer");
>> 
>> then I have no problem stopping it, making the tomcat warning disappear.
>> I'd like to understand why it is so.
>> 
>> Thank you very much in advance
___________________________________________________________
GRATIS: Movie-Flat mit über 300 Top-Videos. Für WEB.DE Nutzer
dauerhaft kostenlos! Jetzt freischalten unter http://movieflat.web.de

Attachment: signature.asc
Description: OpenPGP digital signature

Reply via email to