About JMX monitoring JBoss via Mbean

Source: Internet
Author: User
Tags jboss

A blog in Csdn found a way to use not a lot in jmx, but a more practical approach, so you want to write down:

Query, which is a querying interface, can be Queryexp exp=query.eq () by means of a jmx call to the Mbean, and so on to directly match the required beans and the required properties and methods. If you need to get all the Mbean in a jmx, simply set the Querymbeans () parameter to NULL.

In addition, using the JMX call Mbean in JBoss can be invoked using the common interface RMI interface, as follows:

Securityassociation.setprincipal (new Simpleprincipal (principal));
   securityassociation.setcredential (credential);
   //Get rmiadaptor Object
   properties prop = new Properties ();
   prop.put (Javax.naming.Context.INITIAL_CONTEXT_FACTORY,
      " Org.jnp.interfaces.NamingContextFactory ");
   prop.put (Javax.naming.Context.URL_PKG_PREFIXES,
      " Org.jboss.naming:org.jnp.interface ");
   prop.put (Javax.naming.Context.PROVIDER_URL, URL);
   prop.put (Javax.naming.Context.SECURITY_PRINCIPAL, PRINCIPAL);
   prop.put (Javax.naming.Context.SECURITY_CREDENTIALS, credential);
   initialcontext inictx = new InitialContext (prop);
   object obj = (rmiadaptor) inictx.lookup ("Jmx/rmi/rmiadaptor");
   inictx.close ();
   //

if (obj instanceof rmiadaptor) {
Server = (mbeanserverconnection) obj;
}

} catch (Exception ex) {
Ex.printstacktrace ();
}
}

Below is an example of a specific property under the specified Mbean in JBoss, as illustrated below.

/**
*
*/
Package Org.tuturial.common;

Import java.io.IOException;
Import Java.util.Iterator;
Import java.util.Properties;
Import Java.util.Set;

Import javax.management.InstanceNotFoundException;
Import Javax.management.MBeanAttributeInfo;
Import javax.management.MBeanException;
Import Javax.management.MBeanInfo;
Import Javax.management.MBeanOperationInfo;
Import Javax.management.MBeanParameterInfo;
Import javax.management.MBeanServerConnection;
Import javax.management.ObjectInstance;
Import Javax.management.ObjectName;
Import Javax.management.Query;
Import Javax.management.QueryExp;
Import javax.management.ReflectionException;
Import Javax.naming.InitialContext;

Import Org.jboss.jmx.adaptor.rmi.RMIAdaptor;
Import org.jboss.security.SecurityAssociation;
Import Org.jboss.security.SimplePrincipal;

/**
* @author Administrator
*
*/
public class Mbeaninvokerold {
Private mbeanserverconnection server;

Private ObjectName objname = null;

/**
* Default constructor
*/
Public Mbeaninvokerold () {
}

/**
*
* @param Mbean
* Jboss-mbean's
* @param URL
* @param Principal
* @param credential
*/
Public Mbeaninvokerold (String mbean, string url, string principal,
String credential) {
try {
Securityassociation.setprincipal (New Simpleprincipal (principal));
Securityassociation.setcredential (credential);
Get Rmiadaptor Object
Properties prop = new properties ();
Prop.put (Javax.naming.Context.INITIAL_CONTEXT_FACTORY,
"Org.jnp.interfaces.NamingContextFactory");
Prop.put (Javax.naming.Context.URL_PKG_PREFIXES,
"Org.jboss.naming:org.jnp.interface");
Prop.put (Javax.naming.Context.PROVIDER_URL, URL);
Prop.put (Javax.naming.Context.SECURITY_PRINCIPAL, PRINCIPAL);
Prop.put (Javax.naming.Context.SECURITY_CREDENTIALS, credential);
InitialContext inictx = new InitialContext (prop);
Object obj = (rmiadaptor) inictx.lookup ("Jmx/rmi/rmiadaptor");
Inictx.close ();
//

if (obj instanceof rmiadaptor) {
Server = (mbeanserverconnection) obj;
}
objname = new ObjectName (Mbean);
Mbeaninfo info = server.getmbeaninfo (objname);
System.out.println ("Jndiview class name ':" + info.getclassname ());
mbeanoperationinfo[] Opinfo = Info.getoperations ();
mbeanattributeinfo[] Attrinfo = Info.getattributes ();
for (int i = 0; i < attrinfo.length; i++) {
Mbeanattributeinfo mai = attrinfo[i];
String attrname = Mai.getname ();

System.out.println ("Start getting property name: Attrname =" + attrname);

}
System.out.println ("Jndiview operations--> Method:");
for (int o = 0; o < opinfo.length; o++) {
Mbeanoperationinfo op = opinfo[o];
String returntype = Op.getreturntype ();
String opname = Op.getname ();
System.out.print ("+ returntype =" + returntype
+ ", Opname =" + Opname + "(");
mbeanparameterinfo[] params = op.getsignature ();
for (int p = 0; p < params.length; p++) {
Mbeanparameterinfo paraminfo = params[p];
String pname = Paraminfo.getname ();
String type = Paraminfo.gettype ();
System.out.println ("pname =" + PName + ", type =" + Type ")
+ "....");
if (pname.equals (type))
System.out.print ("... "+ type");
Else
System.out.print ("* * * *" + type + "" + objname);
if (P < params.length-1)
System.out.println ();
}
System.out.println (")");
}

   //Get all MBeans
   objectname oo = new ObjectName ("Org.tuturial.mbean.hello: *");
   //Queryexp exp1 = Query.eq (query.attr ("Age"), Query.value ("*"));
   //queryexp exp1 = (queryexp)
   //query.attr ("Org.tuturial.mbean.hello: Service=helloworld "," age ");
   //Queryexp exp1 = Query.match ("Age", "*");
   set mbset = Server.querymbeans (oo, NULL);//
   for (Iterator it = Mbset.iterator (); It.hasnext ();) {
    objectinstance oi = (objectinstance) it.next ();
    system.out.println ("^^ ^^ ^^" + oi.getobjectname ());
   }
  } catch (Exception ex) {
   ex.printstacktrace ();
  }
 }

/**
* @param args
*/
public static void Main (string[] args) {
Mbeaninvokerold proxy = new Mbeaninvokerold (
"Org.tuturial.mbean.hello:service=helloworld",
"jnp://199.3.8.98:1099", "admin", "admin");
JVM-related information

Proxy.call ("LoadData", null);
HashMap result = Proxy.call ("SaveLocation", New HashMap ());
System.out.println ("---------------------------------");
String s = "";
String ss = "";
int tmpint = 0;
try {
s = Proxy.server.invoke (Proxy.objname, "getage", NULL, NULL)
. toString ();

} catch (Instancenotfoundexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
} catch (Mbeanexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
} catch (Reflectionexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
System.out.println (s);

}

}

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.