Author: nicolas
Date: Fri Apr  3 13:56:44 2009
New Revision: 761680

URL: http://svn.apache.org/viewvc?rev=761680&view=rev
Log:
updated HistoryOfMyThread
configurable Roles in Stopwatches

Added:
    
commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/metrics/ThresholdListener.java
    
commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/stopwatches/HistoryOfMyThread.java
      - copied, changed from r761321, 
commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/repositories/HistoryOfMyThread.java
    
commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/stopwatches/ThresholdListener.java
Removed:
    
commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/repositories/HistoryOfMyThread.java
Modified:
    
commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/repositories/HOMTRepositoryDecorator.java
    
commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/repositories/RepositoryDecorator.java
    
commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/stopwatches/DefaultStopWatch.java
    
commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/stopwatches/SimpleStopWatch.java
    
commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/stopwatches/StopWatchDecorator.java
    
commons/sandbox/monitoring/branches/modules/core/src/test/java/org/apache/commons/monitoring/repositories/HistoryOfMyThreadTest.java

Added: 
commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/metrics/ThresholdListener.java
URL: 
http://svn.apache.org/viewvc/commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/metrics/ThresholdListener.java?rev=761680&view=auto
==============================================================================
--- 
commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/metrics/ThresholdListener.java
 (added)
+++ 
commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/metrics/ThresholdListener.java
 Fri Apr  3 13:56:44 2009
@@ -0,0 +1,51 @@
+/*
+ * 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.metrics;
+
+import org.apache.commons.monitoring.Metric;
+import org.apache.commons.monitoring.Metric.Observable;
+
+/**
+ * Listener for a Metric that will invoke {...@link 
ThresholdListener#exceed(Observable, double, double)} when a value set
+ * to the metric exceed the threshold. The threshold value may be dynamic.
+ * 
+ * @author <a href="mailto:nico...@apache.org";>Nicolas De Loof</a>
+ */
+public abstract class ThresholdListener
+    implements Metric.Listener
+{
+    public abstract double getThreshold();
+
+    /**
+     * {...@inheritdoc}
+     * 
+     * @see 
org.apache.commons.monitoring.Metric.Listener#onValueChanged(org.apache.commons.monitoring.Metric.Observable,
+     *      double)
+     */
+    public void onValueChanged( Observable metric, double value )
+    {
+        double threshold = getThreshold();
+        if ( value > threshold )
+        {
+            exceed( metric, threshold, value );
+        }
+
+    }
+
+    public abstract void exceed( Observable metric, double threshold, double 
value );
+}

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=761680&r1=761679&r2=761680&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 13:56:44 2009
@@ -23,10 +23,11 @@
 import org.apache.commons.monitoring.Monitor;
 import org.apache.commons.monitoring.Repository;
 import org.apache.commons.monitoring.StopWatch;
+import org.apache.commons.monitoring.stopwatches.HistoryOfMyThread;
 
 
 /**
- * @author ndeloof
+ * @author <a href="mailto:nico...@apache.org";>Nicolas De Loof</a>
  *
  */
 public class HOMTRepositoryDecorator
@@ -56,12 +57,18 @@
     public StopWatch start( Monitor monitor )
     {
         StopWatch stopWatch = super.start( monitor );
+        HistoryOfMyThread myThread = getThreadHistory();
+        return myThread.add( stopWatch );
+    }
+
+    public HistoryOfMyThread getThreadHistory()
+    {
         HistoryOfMyThread myThread = history.get();
         if ( myThread == null )
         {
             myThread = new HistoryOfMyThread( listeners );
             history.set( myThread );
         }
-        return myThread.add( stopWatch );
+        return myThread;
     }
 }

Modified: 
commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/repositories/RepositoryDecorator.java
URL: 
http://svn.apache.org/viewvc/commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/repositories/RepositoryDecorator.java?rev=761680&r1=761679&r2=761680&view=diff
==============================================================================
--- 
commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/repositories/RepositoryDecorator.java
 (original)
+++ 
commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/repositories/RepositoryDecorator.java
 Fri Apr  3 13:56:44 2009
@@ -26,7 +26,7 @@
 import org.apache.commons.monitoring.Monitor.Key;
 
 /**
- * @author ndeloof
+ * @author <a href="mailto:nico...@apache.org";>Nicolas De Loof</a>
  *
  */
 public abstract class RepositoryDecorator

Modified: 
commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/stopwatches/DefaultStopWatch.java
URL: 
http://svn.apache.org/viewvc/commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/stopwatches/DefaultStopWatch.java?rev=761680&r1=761679&r2=761680&view=diff
==============================================================================
--- 
commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/stopwatches/DefaultStopWatch.java
 (original)
+++ 
commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/stopwatches/DefaultStopWatch.java
 Fri Apr  3 13:56:44 2009
@@ -18,44 +18,59 @@
 package org.apache.commons.monitoring.stopwatches;
 
 import org.apache.commons.monitoring.Monitor;
+import org.apache.commons.monitoring.Role;
 import org.apache.commons.monitoring.Unit;
 
 /**
  * Implementation of StopWatch that maintains a Gauge of concurrent threads 
accessing the monitored resource.
- * 
+ *
  * @author <a href="mailto:nico...@apache.org";>Nicolas De Loof</a>
  */
 public class DefaultStopWatch extends SimpleStopWatch
 {
+    private Role concurrency;
 
     /**
      * Constructor.
      * <p>
      * The monitor can be set to null to use the StopWatch without the 
monitoring infrastructure.
-     * 
+     *
      * @param monitor the monitor associated with the process to be monitored
      */
     public DefaultStopWatch( Monitor monitor )
     {
+        this( monitor, Monitor.CONCURRENCY );
+    }
+
+    public DefaultStopWatch( Monitor monitor, Role concurrency )
+    {
         super( monitor );
+        this.concurrency = concurrency;
+        doStart();
+    }
+
+    public DefaultStopWatch( Monitor monitor, Role concurrency, Role role )
+    {
+        super( monitor, role );
+        this.concurrency = concurrency;
         doStart();
     }
 
     protected void doStart()
     {
-        monitor.getGauge( Monitor.CONCURRENCY ).increment( Unit.UNARY );
+        monitor.getGauge( concurrency ).increment( Unit.UNARY );
     }
 
     protected void doStop()
     {
         super.doStop();
-        monitor.getGauge( Monitor.CONCURRENCY ).decrement( Unit.UNARY );
+        monitor.getGauge( concurrency ).decrement( Unit.UNARY );
     }
 
 
     protected void doCancel()
     {
-        monitor.getGauge( Monitor.CONCURRENCY ).decrement( Unit.UNARY );
+        monitor.getGauge( concurrency ).decrement( Unit.UNARY );
     }
 
 }
\ No newline at end of file

Copied: 
commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/stopwatches/HistoryOfMyThread.java
 (from r761321, 
commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/repositories/HistoryOfMyThread.java)
URL: 
http://svn.apache.org/viewvc/commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/stopwatches/HistoryOfMyThread.java?p2=commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/stopwatches/HistoryOfMyThread.java&p1=commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/repositories/HistoryOfMyThread.java&r1=761321&r2=761680&rev=761680&view=diff
==============================================================================
--- 
commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/repositories/HistoryOfMyThread.java
 (original)
+++ 
commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/stopwatches/HistoryOfMyThread.java
 Fri Apr  3 13:56:44 2009
@@ -15,19 +15,16 @@
  * limitations under the License.
  */
 
-package org.apache.commons.monitoring.repositories;
+package org.apache.commons.monitoring.stopwatches;
 
 import java.util.Collection;
 import java.util.LinkedList;
 import java.util.List;
 
 import org.apache.commons.monitoring.StopWatch;
-import org.apache.commons.monitoring.stopwatches.StopWatchDecorator;
-
 
 /**
- * @author ndeloof
- *
+ * @author <a href="mailto:nico...@apache.org";>Nicolas De Loof</a>
  */
 public class HistoryOfMyThread
 {
@@ -44,7 +41,7 @@
         this.listeners = listeners;
     }
 
-    protected StopWatch add( StopWatch stopWatch )
+    public StopWatch add( StopWatch stopWatch )
     {
         if ( history.size() == 0 )
         {
@@ -52,35 +49,30 @@
             {
                 public StopWatch stop()
                 {
-                    super.stop();
-                    historyEnd();
-                    return getDecorated();
+                    return stop( false );
                 }
 
                 public StopWatch stop( boolean canceled )
                 {
                     super.stop( canceled );
-                    historyEnd();
+                    if ( !canceled )
+                    {
+                        historyEnd( super.getElapsedTime() );
+                    }
                     return getDecorated();
                 }
 
-                public StopWatch cancel()
-                {
-                    super.cancel();
-                    historyEnd();
-                    return getDecorated();
-                }
             };
         }
         history.add( stopWatch );
         return stopWatch;
     }
 
-    private void historyEnd()
+    private void historyEnd( long elapsedTime )
     {
         for ( Listener listener : listeners )
         {
-            listener.onHistoryEnd( this );
+            listener.onHistoryEnd( this, elapsedTime );
         }
     }
 
@@ -91,6 +83,6 @@
 
     public interface Listener
     {
-        void onHistoryEnd( HistoryOfMyThread history );
+        void onHistoryEnd( HistoryOfMyThread history, long elapsedTime );
     }
 }

Modified: 
commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/stopwatches/SimpleStopWatch.java
URL: 
http://svn.apache.org/viewvc/commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/stopwatches/SimpleStopWatch.java?rev=761680&r1=761679&r2=761680&view=diff
==============================================================================
--- 
commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/stopwatches/SimpleStopWatch.java
 (original)
+++ 
commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/stopwatches/SimpleStopWatch.java
 Fri Apr  3 13:56:44 2009
@@ -1,8 +1,26 @@
+/*
+ * 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.stopwatches;
 
 import static org.apache.commons.monitoring.Unit.Time.NANOSECOND;
 
 import org.apache.commons.monitoring.Monitor;
+import org.apache.commons.monitoring.Role;
 import org.apache.commons.monitoring.StopWatch;
 
 /**
@@ -31,6 +49,13 @@
     /** flag for paused probe */
     protected boolean paused;
 
+    private Role role;
+
+    public SimpleStopWatch( Monitor monitor )
+    {
+        this( monitor, Monitor.PERFORMANCES );
+    }
+
     /**
      * Constructor.
      * <p>
@@ -38,20 +63,12 @@
      *
      * @param monitor the monitor associated with the process to be monitored
      */
-    public SimpleStopWatch( Monitor monitor )
+    public SimpleStopWatch( Monitor monitor, Role role )
     {
         super();
+        this.role = role;
         this.monitor = monitor;
         startedAt = nanotime();
-        doStart();
-    }
-
-    /**
-     * To be overriden by subclasses to add some features on stopWath start
-     */
-    protected void doStart()
-    {
-
     }
 
     /**
@@ -140,7 +157,7 @@
 
     protected void doStop()
     {
-        monitor.getCounter( Monitor.PERFORMANCES ).add( getElapsedTime(), 
NANOSECOND );
+        monitor.getCounter( role ).add( getElapsedTime(), NANOSECOND );
     }
 
     /**

Modified: 
commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/stopwatches/StopWatchDecorator.java
URL: 
http://svn.apache.org/viewvc/commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/stopwatches/StopWatchDecorator.java?rev=761680&r1=761679&r2=761680&view=diff
==============================================================================
--- 
commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/stopwatches/StopWatchDecorator.java
 (original)
+++ 
commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/stopwatches/StopWatchDecorator.java
 Fri Apr  3 13:56:44 2009
@@ -21,7 +21,7 @@
 import org.apache.commons.monitoring.StopWatch;
 
 /**
- * @author ndeloof
+ * @author <a href="mailto:nico...@apache.org";>Nicolas De Loof</a>
  *
  */
 public abstract class StopWatchDecorator

Added: 
commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/stopwatches/ThresholdListener.java
URL: 
http://svn.apache.org/viewvc/commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/stopwatches/ThresholdListener.java?rev=761680&view=auto
==============================================================================
--- 
commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/stopwatches/ThresholdListener.java
 (added)
+++ 
commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/stopwatches/ThresholdListener.java
 Fri Apr  3 13:56:44 2009
@@ -0,0 +1,51 @@
+/*
+ * 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.stopwatches;
+
+/**
+ * @author <a href="mailto:nico...@apache.org";>Nicolas De Loof</a>
+ */
+public abstract class ThresholdListener
+    implements HistoryOfMyThread.Listener
+{
+
+    public abstract long getThreshold();
+
+    /**
+     * {...@inheritdoc}
+     * 
+     * @see 
org.apache.commons.monitoring.stopwatches.HistoryOfMyThread.Listener#onHistoryEnd(org.apache.commons.monitoring.stopwatches.HistoryOfMyThread,
+     *      long)
+     */
+    public void onHistoryEnd( HistoryOfMyThread history, long elapsedTime )
+    {
+        long threshold = getThreshold();
+        if ( elapsedTime > threshold )
+        {
+            exceed( elapsedTime, threshold, history );
+        }
+    }
+
+    /**
+     * @param elapsedTime
+     * @param threshold TODO
+     * @param history
+     */
+    public abstract void exceed( long elapsedTime, long threshold, 
HistoryOfMyThread history );
+
+}

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=761680&r1=761679&r2=761680&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 13:56:44 2009
@@ -1,10 +1,11 @@
 package org.apache.commons.monitoring.repositories;
 
-import java.util.Iterator;
+import java.util.List;
 
 import junit.framework.TestCase;
 
 import org.apache.commons.monitoring.StopWatch;
+import org.apache.commons.monitoring.stopwatches.HistoryOfMyThread;
 
 /**
  * @author ndeloof
@@ -31,19 +32,20 @@
         s1.stop();
 
         assertNotNull( historyOfMyThread );
-        Iterator<StopWatch> history = historyOfMyThread.history();
-        assertEquals( s1, history.next() );
-        assertEquals( s2, history.next() );
-        assertEquals( s3, history.next() );
-        assertFalse( history.hasNext() );
+        List<StopWatch> history = historyOfMyThread.history();
+        assertEquals( 3, history.size() );
+        assertEquals( s1, history.get( 0 ) );
+        assertEquals( s2, history.get( 1 ) );
+        assertEquals( s3, history.get( 2 ) );
     }
 
     /**
      * {...@inheritdoc}
      * 
-     * @see 
org.apache.commons.monitoring.repositories.HistoryOfMyThread.Listener#onHistoryEnd(org.apache.commons.monitoring.repositories.HistoryOfMyThread)
+     * @see 
org.apache.commons.monitoring.stopwatches.HistoryOfMyThread.Listener#onHistoryEnd(org.apache.commons.monitoring.stopwatches.HistoryOfMyThread,
+     *      long)
      */
-    public void onHistoryEnd( HistoryOfMyThread history )
+    public void onHistoryEnd( HistoryOfMyThread history, long elapsedTime )
     {
         historyOfMyThread = history;
     }


Reply via email to