tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Terence M. Bandoian" <tere...@tmbsw.com>
Subject Re: Terminating Timer Thread Gracefully
Date Mon, 25 Jul 2011 00:28:44 GMT
  For anyone who might be interested, this is what I ended up with for a 
scheduler:

public class AServletContextListener implements ServletContextListener
{

     private ScheduledExecutorService executor;


     /**
      * Constructs AServletContextListener object.
      */

     public AServletContextListener()
     {
     }


     /**
      * Invoked when context is initialized.
      *
      * @param    sce        triggering ServletContextEvent
      */

     public void contextInitialized( ServletContextEvent sce )
     {
         if ( executor == null )
         {
             executor = Executors.newSingleThreadScheduledExecutor();

             Calendar 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 );

             executor.scheduleAtFixedRate(
                 new ARunnable(),
                 firstRunTime.getTimeInMillis() - 
System.currentTimeMillis(),
                 1000 * 60 * 60 * 24,
                 TimeUnit.MILLISECONDS );
         }
     }


     /**
      * Invoked when context is destroyed.
      *
      * @param    sce        triggering ServletContextEvent
      */

     public void contextDestroyed( ServletContextEvent sce )
     {
         if ( executor != null )
         {
             boolean isTerminated = false;

             executor.shutdown();

             do
             {
                 try
                 {
                     isTerminated = executor.awaitTermination(
                         1, TimeUnit.SECONDS );
                 }
                 catch ( InterruptedException ignore )
                 {
                 }
             }
             while ( !isTerminated );

             executor = null;

             Thread.yield();
         }
     }

}

Thanks for all the suggestions and pointers.

-Terence


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


Mime
View raw message