commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nico...@apache.org
Subject svn commit: r761693 - in /commons/sandbox/monitoring/branches/modules/core/src: main/java/org/apache/commons/monitoring/repositories/HOMTRepositoryDecorator.java test/java/org/apache/commons/monitoring/repositories/HistoryOfMyThreadTest.java
Date Fri, 03 Apr 2009 14:31:25 GMT
Author: nicolas
Date: Fri Apr  3 14:31:25 2009
New Revision: 761693

URL: http://svn.apache.org/viewvc?rev=761693&view=rev
Log:
fix ThreadLocal cleanup thanks to Julien

Modified:
    commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/repositories/HOMTRepositoryDecorator.java
    commons/sandbox/monitoring/branches/modules/core/src/test/java/org/apache/commons/monitoring/repositories/HistoryOfMyThreadTest.java

Modified: commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/repositories/HOMTRepositoryDecorator.java
URL: http://svn.apache.org/viewvc/commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/repositories/HOMTRepositoryDecorator.java?rev=761693&r1=761692&r2=761693&view=diff
==============================================================================
--- commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/repositories/HOMTRepositoryDecorator.java
(original)
+++ commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/repositories/HOMTRepositoryDecorator.java
Fri Apr  3 14:31:25 2009
@@ -32,12 +32,19 @@
  */
 public class HOMTRepositoryDecorator
     extends RepositoryDecorator
-    implements Repository
+    implements Repository, HistoryOfMyThread.Listener
 {
     private ThreadLocal<HistoryOfMyThread> history = new ThreadLocal<HistoryOfMyThread>();
 
     private Collection<HistoryOfMyThread.Listener> listeners = new CopyOnWriteArrayList<HistoryOfMyThread.Listener>();
 
+    public HOMTRepositoryDecorator()
+    {
+        super();
+        // Act myself as a listener to force cleanup of the ThreadLocal
+        addListener( this );
+    }
+
     public void addListener( HistoryOfMyThread.Listener listener )
     {
         listeners.add( listener );
@@ -71,4 +78,9 @@
         }
         return myThread;
     }
+
+    public void onHistoryEnd( HistoryOfMyThread myThread, long elapsedTime )
+    {
+        history.remove();
+    }
 }

Modified: commons/sandbox/monitoring/branches/modules/core/src/test/java/org/apache/commons/monitoring/repositories/HistoryOfMyThreadTest.java
URL: http://svn.apache.org/viewvc/commons/sandbox/monitoring/branches/modules/core/src/test/java/org/apache/commons/monitoring/repositories/HistoryOfMyThreadTest.java?rev=761693&r1=761692&r2=761693&view=diff
==============================================================================
--- commons/sandbox/monitoring/branches/modules/core/src/test/java/org/apache/commons/monitoring/repositories/HistoryOfMyThreadTest.java
(original)
+++ commons/sandbox/monitoring/branches/modules/core/src/test/java/org/apache/commons/monitoring/repositories/HistoryOfMyThreadTest.java
Fri Apr  3 14:31:25 2009
@@ -4,6 +4,7 @@
 
 import junit.framework.TestCase;
 
+import org.apache.commons.monitoring.Repository;
 import org.apache.commons.monitoring.StopWatch;
 import org.apache.commons.monitoring.stopwatches.HistoryOfMyThread;
 
@@ -40,6 +41,29 @@
         assertEquals( s3, history.get( 2 ) );
     }
 
+    public void testThreadLocalCleanUp()
+        throws Exception
+    {
+        HOMTRepositoryDecorator repository = new HOMTRepositoryDecorator();
+        repository.decorate( new DefaultRepository() );
+        repository.addListener( this );
+
+        run( repository );
+        run( repository );
+        run( repository );
+    }
+
+    private void run( Repository repository )
+    {
+        StopWatch s1 = repository.start( repository.getMonitor( "test0" ) );
+        s1.stop();
+
+        assertNotNull( historyOfMyThread );
+        List<StopWatch> history = historyOfMyThread.history();
+        assertEquals( 1, history.size() );
+        assertEquals( s1, history.get( 0 ) );
+    }
+
     /**
      * {@inheritDoc}
      * 



Mime
View raw message