diff --git a/.gitignore b/.gitignore index 061c9a3..d893868 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ -.classpath -bin +/bin/ +/target/ +*.iml diff --git a/README.markdown b/README.markdown new file mode 100644 index 0000000..1e7c63e --- /dev/null +++ b/README.markdown @@ -0,0 +1,12 @@ +# jmx-tester + +A small Java application for testing JMX management. + +## Usage + +1. Verify that you have a recent Java Runtime Environment ([JRE]) installed. +2. Download the latest jmx-tester.jar. + + +[JRE]: https://java.com/ +[jmx-tester.jar]: \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..c6029d9 --- /dev/null +++ b/pom.xml @@ -0,0 +1,40 @@ + + + 4.0.0 + + cluther + jmx-tester + 1.0.0 + + + + + maven-assembly-plugin + + + package + + single + + + + + jmx-tester-${project.version} + false + + + + true + cluther.test.jmx.Main + + + + jar-with-dependencies + + + + + + \ No newline at end of file diff --git a/run b/run new file mode 100755 index 0000000..e604ec9 --- /dev/null +++ b/run @@ -0,0 +1,7 @@ +#!/bin/sh +echo "jmx-tester listening on port 9991" +java \ + -Dcom.sun.management.jmxremote.port=9991 \ + -Dcom.sun.management.jmxremote.ssl=false \ + -Dcom.sun.management.jmxremote.authenticate=false \ + -jar target/jmx-tester-1.0.0.jar diff --git a/run.sh b/run.sh deleted file mode 100755 index ad9730d..0000000 --- a/run.sh +++ /dev/null @@ -1,8 +0,0 @@ -#!/bin/sh -echo "JMXTester listening on port 9991" -cd bin -java \ --Dcom.sun.management.jmxremote.port=9991 \ --Dcom.sun.management.jmxremote.ssl=false \ --Dcom.sun.management.jmxremote.authenticate=false \ -JMXTester diff --git a/src/JMXTestObject.java b/src/JMXTestObject.java deleted file mode 100644 index a5f4efe..0000000 --- a/src/JMXTestObject.java +++ /dev/null @@ -1,141 +0,0 @@ -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import javax.management.AttributeChangeNotification; -import javax.management.Notification; -import javax.management.NotificationBroadcasterSupport; -import javax.management.openmbean.CompositeDataSupport; -import javax.management.openmbean.CompositeType; -import javax.management.openmbean.OpenDataException; -import javax.management.openmbean.OpenType; -import javax.management.openmbean.SimpleType; -import javax.management.openmbean.TabularDataSupport; -import javax.management.openmbean.TabularType; - -public class JMXTestObject extends NotificationBroadcasterSupport implements JMXTestObjectMBean { - private int aInt = 2; - private double aDouble = 1.23; - private float aFloat = (float) 4.56; - private Map aMap = null; - private TabularDataSupport aTable = null; - - private long sequenceNumber = 1; - - public JMXTestObject() { - aMap = new HashMap(); - aMap.put("key1", 1); - aMap.put("key2", 2); - - buildATable(); - } - - private void buildATable() { - String[] itemNames = new String[] {"Attribute", "DisplayValue", "Sensor", "Type", "Value"}; - String[] itemDesc = {"Attribute", "DisplayValue", "Sensor", "Type", "Value"}; - OpenType[] rowAttrTypes = {SimpleType.STRING, SimpleType.STRING, SimpleType.STRING, SimpleType.STRING, SimpleType.DOUBLE}; - String[] rowIndex = new String[] {"Attribute", "Sensor"}; - - CompositeType compositeType = null; - TabularType tabularTable = null; - try { - compositeType = new CompositeType( - "Sample Composite Data", - "blah, blah", - itemNames, - itemDesc, - rowAttrTypes); - tabularTable = new TabularType( - "SampleTabularType", - "Sample Tabular Type", - compositeType, - rowIndex); - } catch (OpenDataException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - - aTable = new TabularDataSupport(tabularTable); - - try { - CompositeDataSupport cd = new CompositeDataSupport(compositeType, itemNames, new Object[] { - "AverageResponseTime", "227.77", "HttpInputSensor", "DOUBLE", 227.774}); - aTable.put(cd); - - CompositeDataSupport cd1 = new CompositeDataSupport(compositeType, itemNames, new Object[] { - "NumberOfCalls", "123.123", "HttpInputSensor", "LONG", 123.123}); - aTable.put(cd1); - } catch (OpenDataException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } - - public void update() { - aInt += 1; - aDouble += 0.03; - aFloat += 0.02; - - Notification n = new Notification("update", this, sequenceNumber++, System.currentTimeMillis(), "updated MBean values"); - sendNotification(n); - } - - public int getAInt() { - return aInt; - } - - public synchronized void setAInt(int aInt) { - int oldInt = this.aInt; - this.aInt = aInt; - - Notification n = new AttributeChangeNotification(this, sequenceNumber++, System.currentTimeMillis(), "aInt changed", "aInt", "int", oldInt, this.aInt); - sendNotification(n); - } - - public double getADouble() { - return aDouble; - } - - public float getAFloat() { - return aFloat; - } - - public String getAString() { - return "NONUM"; - } - - public String getANumericString() { - return "23.56"; - } - - public List getListOfStrings() { - ArrayList list = new ArrayList(); - list.add("some text"); - list.add("67.89"); - list.add("more text"); - list.add("98.76"); - return list; - } - - public List callListOfStrings(String param) { - ArrayList list = new ArrayList(); - list.add("some text"); - list.add("67.89"); - list.add("more text"); - list.add("98.76"); - return list; - } - - public Map getAMap() { - return aMap; - } - - public Map callAMap() { - return aMap; - } - - public TabularDataSupport getATable() { - return aTable; - } -} diff --git a/src/JMXTestObjectMBean.java b/src/JMXTestObjectMBean.java deleted file mode 100644 index 73c7fca..0000000 --- a/src/JMXTestObjectMBean.java +++ /dev/null @@ -1,17 +0,0 @@ -import java.util.List; -import java.util.Map; - -import javax.management.openmbean.TabularDataSupport; - -public interface JMXTestObjectMBean { - int getAInt(); - double getADouble(); - float getAFloat(); - String getAString(); - String getANumericString(); - List getListOfStrings(); - List callListOfStrings(String param); - Map getAMap(); - Map callAMap(); - public TabularDataSupport getATable(); -} diff --git a/src/JMXTester.java b/src/JMXTester.java deleted file mode 100644 index 120ac26..0000000 --- a/src/JMXTester.java +++ /dev/null @@ -1,27 +0,0 @@ -import java.lang.management.ManagementFactory; - -import javax.management.MBeanServer; -import javax.management.ObjectName; - - -public class JMXTester { - public static void main(String[] args) throws Exception { - JMXTestObject testObject = new JMXTestObject(); - MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); - ObjectName name = new ObjectName("com.zenoss.jmx:type=JMXTestObjectMBean"); - mbs.registerMBean(testObject, name); - imitateActivity(testObject); - } - - private static void imitateActivity(JMXTestObject testObject) { - while (true) { - try { - testObject.update(); - Thread.sleep(1000); - } catch (InterruptedException e) { - // Nothing - } - } - } - -} diff --git a/src/main/java/cluther/test/jmx/JMXTestObject.java b/src/main/java/cluther/test/jmx/JMXTestObject.java new file mode 100644 index 0000000..e8fe50a --- /dev/null +++ b/src/main/java/cluther/test/jmx/JMXTestObject.java @@ -0,0 +1,143 @@ +package cluther.test.jmx; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import javax.management.AttributeChangeNotification; +import javax.management.Notification; +import javax.management.NotificationBroadcasterSupport; +import javax.management.openmbean.CompositeDataSupport; +import javax.management.openmbean.CompositeType; +import javax.management.openmbean.OpenDataException; +import javax.management.openmbean.OpenType; +import javax.management.openmbean.SimpleType; +import javax.management.openmbean.TabularDataSupport; +import javax.management.openmbean.TabularType; + +public class JMXTestObject extends NotificationBroadcasterSupport implements JMXTestObjectMBean { + private int aInt = 2; + private double aDouble = 1.23; + private float aFloat = (float) 4.56; + private Map aMap = null; + private TabularDataSupport aTable = null; + + private long sequenceNumber = 1; + + public JMXTestObject() { + aMap = new HashMap(); + aMap.put("key1", 1); + aMap.put("key2", 2); + + buildATable(); + } + + private void buildATable() { + String[] itemNames = new String[] {"Attribute", "DisplayValue", "Sensor", "Type", "Value"}; + String[] itemDesc = {"Attribute", "DisplayValue", "Sensor", "Type", "Value"}; + OpenType[] rowAttrTypes = {SimpleType.STRING, SimpleType.STRING, SimpleType.STRING, SimpleType.STRING, SimpleType.DOUBLE}; + String[] rowIndex = new String[] {"Attribute", "Sensor"}; + + CompositeType compositeType = null; + TabularType tabularTable = null; + try { + compositeType = new CompositeType( + "Sample Composite Data", + "blah, blah", + itemNames, + itemDesc, + rowAttrTypes); + tabularTable = new TabularType( + "SampleTabularType", + "Sample Tabular Type", + compositeType, + rowIndex); + } catch (OpenDataException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + aTable = new TabularDataSupport(tabularTable); + + try { + CompositeDataSupport cd = new CompositeDataSupport(compositeType, itemNames, new Object[] { + "AverageResponseTime", "227.77", "HttpInputSensor", "DOUBLE", 227.774}); + aTable.put(cd); + + CompositeDataSupport cd1 = new CompositeDataSupport(compositeType, itemNames, new Object[] { + "NumberOfCalls", "123.123", "HttpInputSensor", "LONG", 123.123}); + aTable.put(cd1); + } catch (OpenDataException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + public void update() { + aInt += 1; + aDouble += 0.03; + aFloat += 0.02; + + Notification n = new Notification("update", this, sequenceNumber++, System.currentTimeMillis(), "updated MBean values"); + sendNotification(n); + } + + public int getAInt() { + return aInt; + } + + public synchronized void setAInt(int aInt) { + int oldInt = this.aInt; + this.aInt = aInt; + + Notification n = new AttributeChangeNotification(this, sequenceNumber++, System.currentTimeMillis(), "aInt changed", "aInt", "int", oldInt, this.aInt); + sendNotification(n); + } + + public double getADouble() { + return aDouble; + } + + public float getAFloat() { + return aFloat; + } + + public String getAString() { + return "NONUM"; + } + + public String getANumericString() { + return "23.56"; + } + + public List getListOfStrings() { + ArrayList list = new ArrayList(); + list.add("some text"); + list.add("67.89"); + list.add("more text"); + list.add("98.76"); + return list; + } + + public List callListOfStrings(String param) { + ArrayList list = new ArrayList(); + list.add("some text"); + list.add("67.89"); + list.add("more text"); + list.add("98.76"); + return list; + } + + public Map getAMap() { + return aMap; + } + + public Map callAMap() { + return aMap; + } + + public TabularDataSupport getATable() { + return aTable; + } +} \ No newline at end of file diff --git a/src/main/java/cluther/test/jmx/JMXTestObjectMBean.java b/src/main/java/cluther/test/jmx/JMXTestObjectMBean.java new file mode 100644 index 0000000..92f9251 --- /dev/null +++ b/src/main/java/cluther/test/jmx/JMXTestObjectMBean.java @@ -0,0 +1,19 @@ +package cluther.test.jmx; + +import java.util.List; +import java.util.Map; + +import javax.management.openmbean.TabularDataSupport; + +public interface JMXTestObjectMBean { + int getAInt(); + double getADouble(); + float getAFloat(); + String getAString(); + String getANumericString(); + List getListOfStrings(); + List callListOfStrings(String param); + Map getAMap(); + Map callAMap(); + public TabularDataSupport getATable(); +} \ No newline at end of file diff --git a/src/main/java/cluther/test/jmx/Main.java b/src/main/java/cluther/test/jmx/Main.java new file mode 100644 index 0000000..a539aeb --- /dev/null +++ b/src/main/java/cluther/test/jmx/Main.java @@ -0,0 +1,29 @@ +package cluther.test.jmx; + +import java.lang.management.ManagementFactory; + +import javax.management.MBeanServer; +import javax.management.ObjectName; + + +public class Main { + public static void main(String[] args) throws Exception { + JMXTestObject testObject = new JMXTestObject(); + MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); + ObjectName name = new ObjectName("jmx-tester:type=JMXTestObjectMBean"); + mbs.registerMBean(testObject, name); + System.out.println("MBean registered: " + name.getCanonicalName()); + imitateActivity(testObject); + } + + private static void imitateActivity(JMXTestObject testObject) { + while (true) { + try { + testObject.update(); + Thread.sleep(1000); + } catch (InterruptedException e) { + // Nothing + } + } + } +} \ No newline at end of file