"JMX" 7. Connection Mbean:java Code

Source: Internet
Author: User

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


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.