1+ /*
2+ * To change this license header, choose License Headers in Project Properties.
3+ * To change this template file, choose Tools | Templates
4+ * and open the template in the editor.
5+ */
6+ package moa .gui ;
7+
8+ /**
9+ * FROM: https://web-gmazza.rhcloud.com/blog/entry/creating-jconsole-alternative
10+ * @author me
11+ */
12+
13+
14+
15+ import javax .management .MBeanServerConnection ;
16+ import javax .management .ObjectName ;
17+ import javax .management .remote .JMXConnector ;
18+ import javax .management .remote .JMXConnectorFactory ;
19+ import javax .management .remote .JMXServiceURL ;
20+ import java .io .IOException ;
21+ import java .lang .management .ManagementFactory ;
22+ import java .lang .management .MemoryMXBean ;
23+ import java .lang .management .MemoryPoolMXBean ;
24+ import java .util .Arrays ;
25+ import java .util .List ;
26+ import java .util .Set ;
27+ import java .util .TreeSet ;
28+ import javax .management .AttributeChangeNotification ;
29+ import javax .management .Notification ;
30+ import javax .management .NotificationListener ;
31+ import javax .management .ObjectInstance ;
32+
33+ public class JMXClient {
34+
35+ /**
36+ * Inner class that will handle the notifications.
37+ */
38+ public static class ClientListener implements NotificationListener {
39+ public void handleNotification (Notification notification ,
40+ Object handback ) {
41+ echo ("\n Received notification:" );
42+ echo ("\t ClassName: " + notification .getClass ().getName ());
43+ echo ("\t Source: " + notification .getSource ());
44+ echo ("\t Type: " + notification .getType ());
45+ echo ("\t Message: " + notification .getMessage ());
46+ if (notification instanceof AttributeChangeNotification ) {
47+ AttributeChangeNotification acn =
48+ (AttributeChangeNotification ) notification ;
49+ echo ("\t AttributeName: " + acn .getAttributeName ());
50+ echo ("\t AttributeType: " + acn .getAttributeType ());
51+ echo ("\t NewValue: " + acn .getNewValue ());
52+ echo ("\t OldValue: " + acn .getOldValue ());
53+ }
54+ }
55+ }
56+
57+ /* For simplicity, we declare "throws Exception".
58+ Real programs will usually want finer-grained exception handling. */
59+ public static void main (String [] args ) throws Exception {
60+
61+
62+ // Create an RMI connector client and
63+ // connect it to the RMI connector server
64+ //
65+ echo ("\n Create an RMI connector client and " +
66+ "connect it to the RMI connector server" );
67+ JMXServiceURL url =
68+ new JMXServiceURL ("service:jmx:rmi:///jndi/rmi://:1617/jmxrmi" );
69+ JMXConnector jmxc = JMXConnectorFactory .connect (url , null );
70+
71+ // Create listener
72+ //
73+ ClientListener listener = new ClientListener ();
74+
75+ // Get an MBeanServerConnection
76+ //
77+ echo ("\n Get an MBeanServerConnection" );
78+ MBeanServerConnection mbsc = jmxc .getMBeanServerConnection ();
79+ waitForEnterPressed ();
80+
81+ // Get domains from MBeanServer
82+ //
83+ echo ("\n Domains:" );
84+ String domains [] = mbsc .getDomains ();
85+ Arrays .sort (domains );
86+ for (String domain : domains ) {
87+ echo ("\t Domain = " + domain );
88+ }
89+ waitForEnterPressed ();
90+
91+ // Get MBeanServer's default domain
92+ //
93+ echo ("\n MBeanServer default domain = " + mbsc .getDefaultDomain ());
94+
95+ // Get MBean count
96+ //
97+ echo ("\n MBean count = " + mbsc .getMBeanCount ());
98+
99+ // Query MBean names
100+ //
101+ echo ("\n Query MBeanServer MBeans:" );
102+ Set <ObjectName > names =
103+ new TreeSet <ObjectName >(mbsc .queryNames (null , null ));
104+ for (ObjectName name : names ) {
105+ echo ("\t ObjectName = " + name );
106+ }
107+ waitForEnterPressed ();
108+
109+ // ----------------------
110+ // Manage the Hello MBean
111+ // ----------------------
112+
113+ echo ("\n >>> Perform operations on Hello MBean <<<" );
114+
115+ // Construct the ObjectName for the Hello MBean
116+ //
117+ ObjectName mbeanName = new ObjectName ("com.example:type=Hello" );
118+
119+ // Create a dedicated proxy for the MBean instead of
120+ // going directly through the MBean server connection
121+ //
122+ //HelloMBean mbeanProxy =
123+ // JMX.newMBeanProxy(mbsc, mbeanName, HelloMBean.class, true);
124+
125+ // Add notification listener on Hello MBean
126+ //
127+ echo ("\n Add notification listener..." );
128+ mbsc .addNotificationListener (mbeanName , listener , null , null );
129+
130+
131+ waitForEnterPressed ();
132+
133+ // Close MBeanServer connection
134+ //
135+ echo ("\n Close the connection to the server" );
136+ jmxc .close ();
137+ echo ("\n Bye! Bye!" );
138+ }
139+
140+ private static void echo (String msg ) {
141+ System .out .println (msg );
142+ }
143+
144+ private static void sleep (int millis ) {
145+ try {
146+ Thread .sleep (millis );
147+ } catch (InterruptedException e ) {
148+ e .printStackTrace ();
149+ }
150+ }
151+
152+ private static void waitForEnterPressed () {
153+ try {
154+ echo ("\n Press <Enter> to continue..." );
155+ System .in .read ();
156+ } catch (IOException e ) {
157+ e .printStackTrace ();
158+ }
159+ }
160+
161+ }
0 commit comments