You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by bi...@apache.org on 2012/11/16 19:05:35 UTC

svn commit: r1410517 - in /camel/trunk/components/camel-jmx/src: main/java/org/apache/camel/component/jmx/ test/java/org/apache/camel/component/jmx/ test/java/org/apache/camel/component/jmx/beans/

Author: bibryam
Date: Fri Nov 16 18:05:34 2012
New Revision: 1410517

URL: http://svn.apache.org/viewvc?rev=1410517&view=rev
Log:
Refactor and fix jmx consumer, failing when the monitored attribute type is different than int

Added:
    camel/trunk/components/camel-jmx/src/test/java/org/apache/camel/component/jmx/JMXMonitorTypeLongCounterTest.java
Modified:
    camel/trunk/components/camel-jmx/src/main/java/org/apache/camel/component/jmx/JMXMonitorConsumer.java
    camel/trunk/components/camel-jmx/src/test/java/org/apache/camel/component/jmx/beans/SimpleBean.java
    camel/trunk/components/camel-jmx/src/test/java/org/apache/camel/component/jmx/beans/SimpleBeanMBean.java

Modified: camel/trunk/components/camel-jmx/src/main/java/org/apache/camel/component/jmx/JMXMonitorConsumer.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jmx/src/main/java/org/apache/camel/component/jmx/JMXMonitorConsumer.java?rev=1410517&r1=1410516&r2=1410517&view=diff
==============================================================================
--- camel/trunk/components/camel-jmx/src/main/java/org/apache/camel/component/jmx/JMXMonitorConsumer.java (original)
+++ camel/trunk/components/camel-jmx/src/main/java/org/apache/camel/component/jmx/JMXMonitorConsumer.java Fri Nov 16 18:05:34 2012
@@ -19,8 +19,12 @@ package org.apache.camel.component.jmx;
 import java.lang.management.ManagementFactory;
 import java.util.UUID;
 
+import javax.management.AttributeNotFoundException;
+import javax.management.InstanceNotFoundException;
+import javax.management.MBeanException;
 import javax.management.NotificationFilter;
 import javax.management.ObjectName;
+import javax.management.ReflectionException;
 import javax.management.monitor.CounterMonitor;
 import javax.management.monitor.GaugeMonitor;
 import javax.management.monitor.Monitor;
@@ -52,9 +56,12 @@ public class JMXMonitorConsumer extends 
         Monitor bean = null;
         if (ep.getMonitorType().equals("counter")) {
             CounterMonitor counter = new CounterMonitor();
-            counter.setInitThreshold(ep.getInitThreshold());
-            counter.setOffset(ep.getOffset());
-            counter.setModulus(ep.getModulus());
+            Number initThreshold = convertNumberToAttributeType(ep.getInitThreshold(), ep.getJMXObjectName(), ep.getObservedAttribute());
+            Number offset = convertNumberToAttributeType(ep.getOffset(), ep.getJMXObjectName(), ep.getObservedAttribute());
+            Number modulus = convertNumberToAttributeType(ep.getModulus(), ep.getJMXObjectName(), ep.getObservedAttribute());
+            counter.setInitThreshold(initThreshold);
+            counter.setOffset(offset);
+            counter.setModulus(modulus);
             counter.setDifferenceMode(ep.isDifferenceMode());
             counter.setNotify(true);
             bean = counter;
@@ -63,22 +70,9 @@ public class JMXMonitorConsumer extends 
             gm.setNotifyHigh(ep.isNotifyHigh());
             gm.setNotifyLow(ep.isNotifyLow());
             gm.setDifferenceMode(ep.isDifferenceMode());
-            Object attr = ManagementFactory.getPlatformMBeanServer().getAttribute(ep.getJMXObjectName(), ep.getObservedAttribute());
-            Double highValue = ep.getThresholdHigh();
-            Double lowValue = ep.getThresholdLow();
-            if (attr instanceof Byte) {
-                gm.setThresholds(highValue.byteValue(), lowValue.byteValue());
-            } else if  (attr instanceof Integer) {
-                gm.setThresholds(highValue.intValue(), lowValue.intValue());
-            } else if (attr instanceof Short) {
-                gm.setThresholds(highValue.shortValue(), lowValue.shortValue());
-            } else if (attr instanceof Long) {
-                gm.setThresholds(highValue.longValue(), lowValue.longValue());
-            } else if (attr instanceof Float) {
-                gm.setThresholds(highValue.floatValue(), lowValue.floatValue());
-            } else {
-                gm.setThresholds(highValue, lowValue);
-            }
+            Number highValue = convertNumberToAttributeType(ep.getThresholdHigh(), ep.getJMXObjectName(), ep.getObservedAttribute());
+            Number lowValue = convertNumberToAttributeType(ep.getThresholdLow(), ep.getJMXObjectName(), ep.getObservedAttribute());
+            gm.setThresholds(highValue, lowValue);
             bean = gm;
         } else if (ep.getMonitorType().equals("string")) {
             StringMonitor sm = new StringMonitor();
@@ -110,4 +104,21 @@ public class JMXMonitorConsumer extends 
         ManagementFactory.getPlatformMBeanServer().unregisterMBean(mMonitorObjectName);
     }
 
+    private Number convertNumberToAttributeType(Number toConvert, ObjectName jmxObjectName, String observedAttribute)
+        throws InstanceNotFoundException, ReflectionException, AttributeNotFoundException, MBeanException {
+        Object attr = ManagementFactory.getPlatformMBeanServer().getAttribute(jmxObjectName, observedAttribute);
+        if (attr instanceof Byte) {
+            return toConvert != null ? toConvert.byteValue() : null;
+        } else if (attr instanceof Integer) {
+            return toConvert != null ? toConvert.intValue() : null;
+        } else if (attr instanceof Short) {
+            return toConvert != null ? toConvert.shortValue() : null;
+        } else if (attr instanceof Long) {
+            return toConvert != null ? toConvert.longValue() : null;
+        } else if (attr instanceof Float) {
+            return toConvert != null ? toConvert.floatValue() : null;
+        } else {
+            return toConvert;
+        }
+    }
 }

Added: camel/trunk/components/camel-jmx/src/test/java/org/apache/camel/component/jmx/JMXMonitorTypeLongCounterTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jmx/src/test/java/org/apache/camel/component/jmx/JMXMonitorTypeLongCounterTest.java?rev=1410517&view=auto
==============================================================================
--- camel/trunk/components/camel-jmx/src/test/java/org/apache/camel/component/jmx/JMXMonitorTypeLongCounterTest.java (added)
+++ camel/trunk/components/camel-jmx/src/test/java/org/apache/camel/component/jmx/JMXMonitorTypeLongCounterTest.java Fri Nov 16 18:05:34 2012
@@ -0,0 +1,52 @@
+/**
+ * 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.camel.component.jmx;
+
+import java.io.File;
+
+import org.apache.camel.component.jmx.beans.ISimpleMXBean;
+import org.junit.Test;
+
+
+public class JMXMonitorTypeLongCounterTest extends SimpleBeanFixture {
+    
+    @Test
+    public void counter() throws Exception {
+
+        ISimpleMXBean simpleBean = getSimpleMXBean();
+        
+        // we should get an event after the monitor number reaches 3
+        simpleBean.setLongNumber(1L);
+        // this should trigger a notification
+        simpleBean.setLongNumber(3L);
+        
+        // we should get 1 change from the counter bean
+        getMockFixture().waitForMessages();
+        getMockFixture().assertMessageReceived(new File("src/test/resources/monitor-consumer/monitorNotificationLong.xml"));
+    }
+
+    @Override
+    protected JMXUriBuilder buildFromURI() {
+        return super.buildFromURI().withMonitorType("counter")
+                                   .withGranularityPeriod(500)
+                                   .withObservedAttribute("LongNumber")
+                                   .withInitThreshold(2)
+                                   .withOffset(2)
+                                   .withModulus(100)
+                                   .withDifferenceMode(false);
+    }
+}

Modified: camel/trunk/components/camel-jmx/src/test/java/org/apache/camel/component/jmx/beans/SimpleBean.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jmx/src/test/java/org/apache/camel/component/jmx/beans/SimpleBean.java?rev=1410517&r1=1410516&r2=1410517&view=diff
==============================================================================
--- camel/trunk/components/camel-jmx/src/test/java/org/apache/camel/component/jmx/beans/SimpleBean.java (original)
+++ camel/trunk/components/camel-jmx/src/test/java/org/apache/camel/component/jmx/beans/SimpleBean.java Fri Nov 16 18:05:34 2012
@@ -42,6 +42,7 @@ public class SimpleBean extends Notifica
 
     private String mStringValue;
     private int mMonitorNumber;
+    private long mLongNumber;
 
     public String getStringValue() {
         return mStringValue;
@@ -63,6 +64,13 @@ public class SimpleBean extends Notifica
         mMonitorNumber = aNumber;
     }
 
+    public Long getLongNumber() {
+        return mLongNumber;
+    }
+    public void setLongNumber(Long aNumber) {
+        mLongNumber = aNumber;
+    }
+
 
     public int getSequence() {
         return mSequence;

Modified: camel/trunk/components/camel-jmx/src/test/java/org/apache/camel/component/jmx/beans/SimpleBeanMBean.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jmx/src/test/java/org/apache/camel/component/jmx/beans/SimpleBeanMBean.java?rev=1410517&r1=1410516&r2=1410517&view=diff
==============================================================================
--- camel/trunk/components/camel-jmx/src/test/java/org/apache/camel/component/jmx/beans/SimpleBeanMBean.java (original)
+++ camel/trunk/components/camel-jmx/src/test/java/org/apache/camel/component/jmx/beans/SimpleBeanMBean.java Fri Nov 16 18:05:34 2012
@@ -38,4 +38,7 @@ public interface SimpleBeanMBean extends
 
     Integer getMonitorNumber();
     void setMonitorNumber(Integer aMonitorNumber);
+
+    Long getLongNumber();
+    void setLongNumber(Long aMonitorNumber);
 }