Fix findbugs warnings
[controller.git] / opendaylight / md-sal / sal-clustering-commons / src / main / java / org / opendaylight / controller / cluster / common / actor / MeteringBehavior.java
index 36c35be30a4cc1c4438af53f7c2a558f4ad99ca8..63958912c48e8832a4c4b144eab84822e1b24b7a 100644 (file)
@@ -17,27 +17,29 @@ import org.opendaylight.controller.cluster.reporting.MetricsReporter;
 
 /**
  * Represents behaviour that can be exhibited by actors of type {@link akka.actor.UntypedActor}
- * <p/>
+ *
+ * <p>
  * This behaviour meters actor's default behaviour. It captures 2 metrics:
  * <ul>
  *     <li>message processing rate of actor's receive block</li>
  *     <li>message processing rate by message type</li>
  * </ul>
- *
  * The information is reported to {@link org.opendaylight.controller.cluster.reporting.MetricsReporter}
  */
 public class MeteringBehavior implements Procedure<Object> {
     public static final String DOMAIN = "org.opendaylight.controller.actor.metric";
 
+    private static final String MSG_PROCESSING_RATE = "msg-rate";
+
     private final UntypedActor meteredActor;
 
-    private final MetricRegistry METRICREGISTRY = MetricsReporter.getInstance(DOMAIN).getMetricsRegistry();
-    private final String MSG_PROCESSING_RATE = "msg-rate";
+    private final MetricRegistry metricRegistry = MetricsReporter.getInstance(DOMAIN).getMetricsRegistry();
 
     private String actorQualifiedName;
     private Timer msgProcessingTimer;
 
     /**
+     * Constructs an instance.
      *
      * @param actor whose behaviour needs to be metered
      */
@@ -59,29 +61,32 @@ public class MeteringBehavior implements Procedure<Object> {
     }
 
     private void init(final String actorName) {
-        actorQualifiedName = new StringBuilder(meteredActor.getSelf().path().parent().toStringWithoutAddress()).
-                append("/").append(actorName).toString();
+        actorQualifiedName = meteredActor.getSelf().path().parent().toStringWithoutAddress()
+                + "/" + actorName;
 
         final String msgProcessingTime = MetricRegistry.name(actorQualifiedName, MSG_PROCESSING_RATE);
-        msgProcessingTimer = METRICREGISTRY.timer(msgProcessingTime);
+        msgProcessingTimer = metricRegistry.timer(msgProcessingTime);
     }
 
     /**
      * Uses 2 timers to measure message processing rate. One for overall message processing rate and
      * another to measure rate by message type. The timers are re-used if they were previously created.
-     * <p/>
+     *
+     * <p>
      * {@link com.codahale.metrics.MetricRegistry} maintains a reservoir for different timers where
      * collected timings are kept. It exposes various metrics for each timer based on collected
      * data. Eg: count of messages, 99, 95, 50... percentiles, max, mean etc.
-     * <p/>
+     *
+     * <p>
      * These metrics are exposed as JMX bean.
      *
      * @see <a href="http://dropwizard.github.io/metrics/manual/core/#timers">
      *     http://dropwizard.github.io/metrics/manual/core/#timers</a>
      *
-     * @param message
-     * @throws Exception
+     * @param message the message to process
+     * @throws Exception on message failure
      */
+    @SuppressWarnings("checkstyle:IllegalCatch")
     @Override
     public void apply(final Object message) throws Exception {
         final String messageType = message.getClass().getSimpleName();
@@ -89,7 +94,7 @@ public class MeteringBehavior implements Procedure<Object> {
         final String msgProcessingTimeByMsgType =
                 MetricRegistry.name(actorQualifiedName, MSG_PROCESSING_RATE, messageType);
 
-        final Timer msgProcessingTimerByMsgType = METRICREGISTRY.timer(msgProcessingTimeByMsgType);
+        final Timer msgProcessingTimerByMsgType = metricRegistry.timer(msgProcessingTimeByMsgType);
 
         //start timers
         final Timer.Context context = msgProcessingTimer.time();
@@ -97,13 +102,13 @@ public class MeteringBehavior implements Procedure<Object> {
 
         try {
             meteredActor.onReceive(message);
-        } catch (Throwable t) {
-            Throwables.propagateIfPossible(t, Exception.class);
-            throw Throwables.propagate(t);
+        } catch (Throwable e) {
+            Throwables.propagateIfPossible(e, Exception.class);
+            throw new RuntimeException(e);
+        } finally {
+            //stop timers
+            contextByMsgType.stop();
+            context.stop();
         }
-
-        //stop timers
-        contextByMsgType.stop();
-        context.stop();
     }
 }