在csdn的一个博客中发现了一个在JMX中用的不是很多,但是却比较实用的方法,于是就想记下来:
Query,这是一个查询接口,在JMX调用MBean的时候可以通过方法 QueryExp exp=Query.eq();等等方法去直接匹配所需要的Bean和所需要的属性和方法。如果需要得到一个JMX中的所有的MBean,只需要将queryMBeans()参数全部设为null。
另外在JBoss中使用JMX调用MBean可以使用通用的接口RMI接口去调用,具体方法如下:
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();
}
}
下面是一个举例说明的下例子,可以获得JBoss中的指定的MBean下的具体的属性。
/**
*
*/
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;
/**
* 默认构造函数
*/
public MBeanInvokerOld() {
}
/**
*
* @param mbean
* jboss-MBean的
* @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 '类名': " + 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("开始获得属性的名称:attrName = " + attrName);
}
System.out.println("JNDIView Operations-->方法:");
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 相关信息
// 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);
}
}