The front is connected to the Mbeanserver using HTML, Jconsole, and so on, and can manipulate the Mbean through these interfaces. But sometimes we need to manipulate the Mbean in our own program without resorting to these clients, which requires us to know how to connect the Mbean in the code.
The client code is as follows:
public class Client {public static void main (string[] args) throws Exception {Jmxserviceurl URL =
New Jmxserviceurl ("Service:jmx:rmi:///jndi/rmi://localhost:9999/server");
Jmxconnector jmxc = jmxconnectorfactory.connect (URL, null);
Mbeanserverconnection mbsc = Jmxc.getmbeanserverconnection ();
ObjectName mbeanname = new ObjectName ("Alpha:name=helloworld");
Print out all domain System.out.println ("Domains:---------------");
String domains[] = Mbsc.getdomains (); for (int i = 0; i < domains.length i++) {System.out.println ("\tdomain[" + i + "] =" + domains[i])
;
The total number of//Mbean System.out.println ("MBean count =" + Mbsc.getmbeancount ()); Action on the Name property (============== the first letter of the property name to capitalize ==============) Mbsc.setattribute (mbeanname, new attribute ("Name", "PAN DA "));/Set Value System.out.println (" Name = "+ Mbsc.getattribute (mbeanname," Name "))//Take value//call directly after proxy agent Hellombean proxy
= Mbeanserverinvocationhandler.
Newproxyinstance (MBSC, Mbeanname, Hellombean.class, false);
Proxy.printhello ();
Proxy.printhello ("Raymend");
Remote invocation of the way Mbsc.invoke (Mbeanname, "Printhello", NULL, NULL);
Mbsc.invoke (Mbeanname, "Printhello", new object[] {"Panda incense"}, new string[] {String.class.getName ()});
Information of the Mbean mbeaninfo info = mbsc.getmbeaninfo (mbeanname);
System.out.println ("Hello Class:" + info.getclassname ());
System.out.println ("Hello attriber:" + info.getattributes ()) [0].getname ()];
System.out.println ("Hello Operation:" + info.getoperations ()) [0].getname ()];
Get all the Mbean objectname System.out.println ("All objectname:---------------"); Set set = Mbsc.querymbeans (null, NULL); for (Iterator it = Set.iterator (); It.hasnext ();)
{ObjectInstance Oi = (objectinstance) it.next ();
System.out.println ("T" + oi.getobjectname ());
}//Logoff//Mbsc.unregistermbean (Mbeanname);
Close Mbeanserver connection jmxc.close (); }
}
Run the Helloagentrmi in the previous article
Client Console information:
Domains:---------------
domain[0] = jmimplementation
domain[1] = helloagent
domain[2] = Alpha
MBean Count = 3
Name = PANDA
Hello Class:no6_jmx.jconsole.Hello hello
attriber:name
Hello Operation:printhello all
objectname:---------------
jmimplementation:type=mbeanserverdelegate
helloagent:name=htmladapter,port=8082
Alpha:name=helloworld
Agent Console Information:
start ... RMI Start
... Hello world, PANDA
Hello, raymend
Hello World, PANDA
Hello, Panda incense