Author: nicolas
Date: Wed Jan 28 13:50:53 2009
New Revision: 738486

URL: http://svn.apache.org/viewvc?rev=738486&view=rev
Log:
Use java.util.concurrent for PeriodicLogger (was java.util.Timer)

Added:
    
commons/sandbox/monitoring/branches/modules/reporting/src/test/java/org/apache/commons/monitoring/reporting/AbstractPeriodicLoggerTest.java
Removed:
    commons/sandbox/monitoring/branches/modules/src/assembly/
Modified:
    
commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/repositories/ObservableRepository.java
    commons/sandbox/monitoring/branches/modules/reporting/pom.xml
    
commons/sandbox/monitoring/branches/modules/reporting/src/main/java/org/apache/commons/monitoring/reporting/AbstractPeriodicLogger.java

Modified: 
commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/repositories/ObservableRepository.java
URL: 
http://svn.apache.org/viewvc/commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/repositories/ObservableRepository.java?rev=738486&r1=738485&r2=738486&view=diff
==============================================================================
--- 
commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/repositories/ObservableRepository.java
 (original)
+++ 
commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/repositories/ObservableRepository.java
 Wed Jan 28 13:50:53 2009
@@ -21,6 +21,7 @@
 import java.util.concurrent.CopyOnWriteArrayList;
 
 import org.apache.commons.monitoring.Monitor;
+import org.apache.commons.monitoring.Repository;
 
 /**
  * Implements <tt>Observale</tt> pattern on the repository
@@ -31,6 +32,7 @@
  */
 public abstract class ObservableRepository
     extends AbstractRepository
+    implements Repository.Observable
 {
 
     protected List<Listener> listeners;

Modified: commons/sandbox/monitoring/branches/modules/reporting/pom.xml
URL: 
http://svn.apache.org/viewvc/commons/sandbox/monitoring/branches/modules/reporting/pom.xml?rev=738486&r1=738485&r2=738486&view=diff
==============================================================================
--- commons/sandbox/monitoring/branches/modules/reporting/pom.xml (original)
+++ commons/sandbox/monitoring/branches/modules/reporting/pom.xml Wed Jan 28 
13:50:53 2009
@@ -12,5 +12,11 @@
                <artifactId>commons-monitoring</artifactId>
                <version>${project.version}</version>
        </dependency>
+       <dependency>
+               <groupId>junit</groupId>
+               <artifactId>junit</artifactId>
+               <version>4.5</version>
+               <scope>test</scope>
+       </dependency>
   </dependencies>
 </project>
\ No newline at end of file

Modified: 
commons/sandbox/monitoring/branches/modules/reporting/src/main/java/org/apache/commons/monitoring/reporting/AbstractPeriodicLogger.java
URL: 
http://svn.apache.org/viewvc/commons/sandbox/monitoring/branches/modules/reporting/src/main/java/org/apache/commons/monitoring/reporting/AbstractPeriodicLogger.java?rev=738486&r1=738485&r2=738486&view=diff
==============================================================================
--- 
commons/sandbox/monitoring/branches/modules/reporting/src/main/java/org/apache/commons/monitoring/reporting/AbstractPeriodicLogger.java
 (original)
+++ 
commons/sandbox/monitoring/branches/modules/reporting/src/main/java/org/apache/commons/monitoring/reporting/AbstractPeriodicLogger.java
 Wed Jan 28 13:50:53 2009
@@ -17,8 +17,9 @@
 
 package org.apache.commons.monitoring.reporting;
 
-import java.util.Timer;
-import java.util.TimerTask;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
 
 import org.apache.commons.monitoring.Repository;
 import org.apache.commons.monitoring.repositories.ObserverRepository;
@@ -29,10 +30,9 @@
  * @author <a href="mailto:nico...@apache.org";>Nicolas De Loof</a>
  */
 public abstract class AbstractPeriodicLogger
-    extends TimerTask
 {
     /** The timer that handles the period */
-    private Timer timer;
+    private ScheduledExecutorService scheduler;
 
     /** The observed repository */
     private Repository.Observable repository;
@@ -50,14 +50,21 @@
     public AbstractPeriodicLogger( int period, Repository.Observable 
repository )
     {
         this.repository = repository;
-        this.timer = new Timer();
         this.period = period;
+        this.scheduler = Executors.newSingleThreadScheduledExecutor();
     }
 
     public void init()
     {
         observeRepositoryForPeriod();
-        timer.scheduleAtFixedRate( this, period, period );
+        scheduler.scheduleAtFixedRate( new Runnable()
+        {
+            public void run()
+            {
+                Repository observed = observeRepositoryForPeriod();
+                log( observed );
+            }
+        }, period, period, TimeUnit.MILLISECONDS );
     }
 
     private Repository observeRepositoryForPeriod()
@@ -73,26 +80,14 @@
 
     public void stop()
     {
-        timer.cancel();
-    }
-
-    /**
-     * {...@inheritdoc}
-     *
-     * @see java.util.TimerTask#run()
-     */
-    @Override
-    public void run()
-    {
+        scheduler.shutdown();
         try
         {
-            Repository observedPeriod = observeRepositoryForPeriod();
-            log( observedPeriod );
+            scheduler.awaitTermination( 100, TimeUnit.MILLISECONDS );
         }
-        catch ( Throwable t )
+        catch ( InterruptedException e )
         {
-            // catch any exception, as throwing it will stop the timer
-            handleError( t );
+            // Can be ignored, we are stopping anyway;
         }
     }
 
@@ -102,25 +97,4 @@
      * @param observeRepositoryForPeriod
      */
     protected abstract void log( Repository repositoryForPeriod );
-
-    /**
-     * Warn when logging the repository failed.
-     * <p>
-     * This method is expected to be override by user to avoid System.err 
outputs and use the application logging
-     * strategy.
-     *
-     * @param t error during logging
-     */
-    protected void handleError( Throwable t )
-    {
-        System.err.println( "Failure to log observed repository : " + 
t.getMessage() );
-    }
-
-    /**
-     * @return the SecondaryRepository active for the current period.
-     */
-    protected Repository getRepositoryForActivePeriod()
-    {
-        return this.secondary;
-    }
 }

Added: 
commons/sandbox/monitoring/branches/modules/reporting/src/test/java/org/apache/commons/monitoring/reporting/AbstractPeriodicLoggerTest.java
URL: 
http://svn.apache.org/viewvc/commons/sandbox/monitoring/branches/modules/reporting/src/test/java/org/apache/commons/monitoring/reporting/AbstractPeriodicLoggerTest.java?rev=738486&view=auto
==============================================================================
--- 
commons/sandbox/monitoring/branches/modules/reporting/src/test/java/org/apache/commons/monitoring/reporting/AbstractPeriodicLoggerTest.java
 (added)
+++ 
commons/sandbox/monitoring/branches/modules/reporting/src/test/java/org/apache/commons/monitoring/reporting/AbstractPeriodicLoggerTest.java
 Wed Jan 28 13:50:53 2009
@@ -0,0 +1,48 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.monitoring.reporting;
+
+import static org.junit.Assert.assertTrue;
+
+import org.apache.commons.monitoring.Repository;
+import org.apache.commons.monitoring.repositories.DefaultRepository;
+import org.junit.Test;
+
+/**
+ * @author <a href="mailto:nico...@apache.org";>Nicolas De Loof</a>
+ */
+public class AbstractPeriodicLoggerTest
+{
+    private int count = 0;
+
+    @Test
+    public void schedule()
+        throws Exception
+    {
+        AbstractPeriodicLogger logger = new AbstractPeriodicLogger( 10, new 
DefaultRepository() ) {
+            protected void log( Repository repositoryForPeriod )
+            {
+                count++;
+            }
+        };
+        logger.init();
+        Thread.sleep( 110 );
+        logger.stop();
+        assertTrue( count >= 10 );
+    }
+}


Reply via email to