tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Terence M. Bandoian" <tere...@tmbsw.com>
Subject Terminating Timer Thread Gracefully
Date Tue, 12 Jul 2011 18:20:43 GMT
  Thanks to all who replied.

Functionally, the servlet context listener is implemented as follows:

public class AServletContextListener implements ServletContextListener
{
     private Timer timer;
     private ATimerTask timerTask;

     public AServletContextListener()
     {
     }

     public void contextInitialized( ServletContextEvent sce )
     {
         if ( timer == null )
         {
             Calendar firstRunTime;

             timer = new Timer( true );
             timerTask = new ATimerTask();

             firstRunTime = new GregorianCalendar();
             firstRunTime.set( Calendar.HOUR_OF_DAY, 12 );
             firstRunTime.set( Calendar.MINUTE, 0 );
             firstRunTime.set( Calendar.SECOND, 0 );
             firstRunTime.set( Calendar.MILLISECOND, 0 );

             timer.scheduleAtFixedRate(
                 timerTask, firstRunTime.getTime(), 1000 * 60 * 60 * 24 );
         }
     }

     public void contextDestroyed( ServletContextEvent sce )
     {
         if ( timer != null )
         {
             timer.cancel();
             timer.purge();

             timer = null;
             timerTask = null;

             try
             {
                 Thread.sleep( 1000 );
             }
             catch ( InterruptedException ie )
             {
             }
         }
     }
}

If I remove the call to Thread.sleep, the error message is written to 
the logs.

Timer tasks are scheduled no more often than daily and none are 
executing when the context is destroyed.  The one second delay appears 
to work reliably.  I haven't yet tested with other values.

The real question may be, as Len suggested, what does "terminates 
gracefully" mean in relation to the execution thread of a canceled Timer?

Thanks again.

-Terence Bandoian

On 1:59 PM, Pid wrote:
> On 12/07/2011 02:06, Terence M. Bandoian wrote:
>>   Hi-
>>
>> I've been testing a web application on:
>>
>> Tomcat 6.0.32 (32-bit)
>> Sun/Oracle JRE 1.6.0_25 (32-bit)
>> Windows Server 2008 R2
>>
>> The web application includes a ServletContextListener which creates a
>> Timer in the contextInitialized method to perform periodic maintenance.
>> To no avail, I spent quite a bit of time trying to eliminate the "The
>> web application appears to have started a thread..." error message from
>> the logs including logging the cancellation of the timer in the
>> contextDestroyed method of the ServletContextListener.  Finally, in
>> contextDestroyed, I inserted a call to Thread.sleep after canceling the
>> timer and the error message disappeared.
>>
>> Is there some other way to eliminate this message that I've missed?
> Hard to know, can you post the code you used to start/stop the timer?
>
>
> p
>
>
>> -Terence Bandoian
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
>> For additional commands, e-mail: users-help@tomcat.apache.org
>>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Mime
View raw message