下面列出了javax.management.MBeanInfo#getOperations ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Return the MBeanInfo for the given resource, based on the given
* per-interface data.
*/
final MBeanInfo getMBeanInfo(Object resource, PerInterface<M> perInterface) {
MBeanInfo mbi =
getClassMBeanInfo(resource.getClass(), perInterface);
MBeanNotificationInfo[] notifs = findNotifications(resource);
if (notifs == null || notifs.length == 0)
return mbi;
else {
return new MBeanInfo(mbi.getClassName(),
mbi.getDescription(),
mbi.getAttributes(),
mbi.getConstructors(),
mbi.getOperations(),
notifs,
mbi.getDescriptor());
}
}
/**
* Return the MBeanInfo for the given resource, based on the given
* per-interface data.
*/
final MBeanInfo getMBeanInfo(Object resource, PerInterface<M> perInterface) {
MBeanInfo mbi =
getClassMBeanInfo(resource.getClass(), perInterface);
MBeanNotificationInfo[] notifs = findNotifications(resource);
if (notifs == null || notifs.length == 0)
return mbi;
else {
return new MBeanInfo(mbi.getClassName(),
mbi.getDescription(),
mbi.getAttributes(),
mbi.getConstructors(),
mbi.getOperations(),
notifs,
mbi.getDescriptor());
}
}
/**
* Return the MBeanInfo for the given resource, based on the given
* per-interface data.
*/
final MBeanInfo getMBeanInfo(Object resource, PerInterface<M> perInterface) {
MBeanInfo mbi =
getClassMBeanInfo(resource.getClass(), perInterface);
MBeanNotificationInfo[] notifs = findNotifications(resource);
if (notifs == null || notifs.length == 0)
return mbi;
else {
return new MBeanInfo(mbi.getClassName(),
mbi.getDescription(),
mbi.getAttributes(),
mbi.getConstructors(),
mbi.getOperations(),
notifs,
mbi.getDescriptor());
}
}
private void printMBeanInfo(MBeanInfo mbInfo) {
System.out.println("Description " + mbInfo.getDescription());
for (MBeanConstructorInfo ctor : mbInfo.getConstructors()) {
System.out.println("Constructor " + ctor.getName());
}
for (MBeanAttributeInfo att : mbInfo.getAttributes()) {
System.out.println("Attribute " + att.getName()
+ " [" + att.getType() + "]");
}
for (MBeanOperationInfo oper : mbInfo.getOperations()) {
System.out.println("Operation " + oper.getName());
}
for (MBeanNotificationInfo notif : mbInfo.getNotifications()) {
System.out.println("Notification " + notif.getName());
}
}
private void printMBeanInfo(MBeanInfo mbInfo) {
System.out.println("Description " + mbInfo.getDescription());
for (MBeanConstructorInfo ctor : mbInfo.getConstructors()) {
System.out.println("Constructor " + ctor.getName());
}
for (MBeanAttributeInfo att : mbInfo.getAttributes()) {
System.out.println("Attribute " + att.getName()
+ " [" + att.getType() + "]");
}
for (MBeanOperationInfo oper : mbInfo.getOperations()) {
System.out.println("Operation " + oper.getName());
}
for (MBeanNotificationInfo notif : mbInfo.getNotifications()) {
System.out.println("Notification " + notif.getName());
}
}
private boolean hasDumpAllThreads() {
synchronized (hasDumpAllThreadsLock) {
if (hasDumpAllThreads == null) {
hasDumpAllThreads = Boolean.FALSE;
try {
ObjectName threadObjName = new ObjectName(ManagementFactory.THREAD_MXBEAN_NAME);
MBeanInfo threadInfo = jmxModel.getMBeanServerConnection().getMBeanInfo(threadObjName);
if (threadInfo != null) {
for (MBeanOperationInfo op : threadInfo.getOperations()) {
if ("dumpAllThreads".equals(op.getName())) {
hasDumpAllThreads = Boolean.TRUE;
}
}
}
} catch (Exception ex) {
LOGGER.log(Level.INFO,"hasDumpAllThreads", ex); // NOI18N
}
}
return hasDumpAllThreads.booleanValue();
}
}
public static void main(String[] args) throws Exception {
System.out.println("--->JRCMD MBean Test: invocation on \"operation info\"...");
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
JMXServiceURL url = new JMXServiceURL("rmi", null, 0);
JMXConnectorServer cs = null;
JMXConnector cc = null;
try {
cs = JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);
cs.start();
JMXServiceURL addr = cs.getAddress();
cc = JMXConnectorFactory.connect(addr);
MBeanServerConnection mbsc = cc.getMBeanServerConnection();
ObjectName name = new ObjectName(HOTSPOT_DIAGNOSTIC_MXBEAN_NAME);
MBeanInfo info = mbsc.getMBeanInfo(name);
// the test should check that the MBean doesn't have any
// Attribute, notification or constructor. Current version only
// check operations
System.out.println("Class Name:" + info.getClassName());
System.out.println("Description:" + info.getDescription());
MBeanOperationInfo[] opInfo = info.getOperations();
System.out.println("Operations:");
for (int i = 0; i < opInfo.length; i++) {
printOperation(opInfo[i]);
System.out.println("\[email protected]@@@@@\n");
}
} finally {
try {
cc.close();
cs.stop();
} catch (Exception e) {
}
}
System.out.println("Test passed");
}
@Override
public MBeanInfo getMBeanInfo() {
MBeanInfo mbi = super.getMBeanInfo();
Class<?> resourceClass = getResource().getClass();
if (StandardMBeanIntrospector.isDefinitelyImmutableInfo(resourceClass))
return mbi;
return new MBeanInfo(mbi.getClassName(), mbi.getDescription(),
mbi.getAttributes(), mbi.getConstructors(),
mbi.getOperations(),
MBeanIntrospector.findNotifications(getResource()),
mbi.getDescriptor());
}
private static void check(MBeanServer mbs, ObjectName on) throws Exception {
MBeanInfo mbi = mbs.getMBeanInfo(on);
// check the MBean itself
check(mbi);
// check attributes
MBeanAttributeInfo[] attrs = mbi.getAttributes();
for (MBeanAttributeInfo attr : attrs) {
check(attr);
if (attr.getName().equals("ReadOnly"))
check("@Full", attr.getDescriptor(), expectedFullDescriptor);
}
// check operations
MBeanOperationInfo[] ops = mbi.getOperations();
for (MBeanOperationInfo op : ops) {
check(op);
check(op.getSignature());
}
MBeanConstructorInfo[] constrs = mbi.getConstructors();
for (MBeanConstructorInfo constr : constrs) {
check(constr);
check(constr.getSignature());
}
}
@Override
public MBeanInfo getMBeanInfo() {
MBeanInfo mbi = super.getMBeanInfo();
Class<?> resourceClass = getResource().getClass();
if (StandardMBeanIntrospector.isDefinitelyImmutableInfo(resourceClass))
return mbi;
return new MBeanInfo(mbi.getClassName(), mbi.getDescription(),
mbi.getAttributes(), mbi.getConstructors(),
mbi.getOperations(),
MBeanIntrospector.findNotifications(getResource()),
mbi.getDescriptor());
}
@Test
public void itShouldCollectInformationAbountJMXOperationsToMBeanInfo() {
BeanStubWithOperations bean = new BeanStubWithOperations();
DynamicMBean mbean = DynamicMBeanFactory.create()
.createDynamicMBean(singletonList(bean), defaultSettings(), false);
MBeanInfo mBeanInfo = mbean.getMBeanInfo();
MBeanOperationInfo[] operations = mBeanInfo.getOperations();
Map<String, MBeanOperationInfo> nameToOperation = new HashMap<>();
for (MBeanOperationInfo operation : operations) {
nameToOperation.put(operation.getName(), operation);
}
assertThat(nameToOperation, hasKey("increment"));
assertThat(nameToOperation, hasKey("addInfo"));
assertThat(nameToOperation, hasKey("multiplyAndAdd"));
MBeanOperationInfo incrementOperation = nameToOperation.get("increment");
MBeanOperationInfo addInfoOperation = nameToOperation.get("addInfo");
MBeanOperationInfo multiplyAndAddOperation = nameToOperation.get("multiplyAndAdd");
assertThat(incrementOperation, hasReturnType("void"));
assertThat(addInfoOperation, hasParameter("information", String.class.getName()));
assertThat(addInfoOperation, hasReturnType("void"));
// parameter names are not annotated
assertThat(multiplyAndAddOperation, hasParameter("arg0", "long"));
assertThat(multiplyAndAddOperation, hasParameter("arg1", "long"));
assertThat(multiplyAndAddOperation, hasReturnType("void"));
}
@Override
public MBeanInfo getMBeanInfo() {
MBeanInfo mbi = super.getMBeanInfo();
Class<?> resourceClass = getResource().getClass();
if (StandardMBeanIntrospector.isDefinitelyImmutableInfo(resourceClass))
return mbi;
return new MBeanInfo(mbi.getClassName(), mbi.getDescription(),
mbi.getAttributes(), mbi.getConstructors(),
mbi.getOperations(),
MBeanIntrospector.findNotifications(getResource()),
mbi.getDescriptor());
}
public static void main(String[] args) throws Exception {
System.out.println("--->JRCMD MBean Test: invocation on \"operation info\"...");
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
JMXServiceURL url = new JMXServiceURL("rmi", null, 0);
JMXConnectorServer cs = null;
JMXConnector cc = null;
try {
cs = JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);
cs.start();
JMXServiceURL addr = cs.getAddress();
cc = JMXConnectorFactory.connect(addr);
MBeanServerConnection mbsc = cc.getMBeanServerConnection();
ObjectName name = new ObjectName(HOTSPOT_DIAGNOSTIC_MXBEAN_NAME);
MBeanInfo info = mbsc.getMBeanInfo(name);
// the test should check that the MBean doesn't have any
// Attribute, notification or constructor. Current version only
// check operations
System.out.println("Class Name:" + info.getClassName());
System.out.println("Description:" + info.getDescription());
MBeanOperationInfo[] opInfo = info.getOperations();
System.out.println("Operations:");
for (int i = 0; i < opInfo.length; i++) {
printOperation(opInfo[i]);
System.out.println("\[email protected]@@@@@\n");
}
} finally {
try {
cc.close();
cs.stop();
} catch (Exception e) {
}
}
System.out.println("Test passed");
}
public static void main(String[] args) throws Exception {
System.out.println("--->JRCMD MBean Test: invocation on \"operation info\"...");
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
JMXServiceURL url = new JMXServiceURL("rmi", null, 0);
JMXConnectorServer cs = null;
JMXConnector cc = null;
try {
cs = JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);
cs.start();
JMXServiceURL addr = cs.getAddress();
cc = JMXConnectorFactory.connect(addr);
MBeanServerConnection mbsc = cc.getMBeanServerConnection();
ObjectName name = new ObjectName(HOTSPOT_DIAGNOSTIC_MXBEAN_NAME);
MBeanInfo info = mbsc.getMBeanInfo(name);
// the test should check that the MBean doesn't have any
// Attribute, notification or constructor. Current version only
// check operations
System.out.println("Class Name:" + info.getClassName());
System.out.println("Description:" + info.getDescription());
MBeanOperationInfo[] opInfo = info.getOperations();
System.out.println("Operations:");
for (int i = 0; i < opInfo.length; i++) {
printOperation(opInfo[i]);
System.out.println("\[email protected]@@@@@\n");
}
} finally {
try {
cc.close();
cs.stop();
} catch (Exception e) {
}
}
System.out.println("Test passed");
}
public static void main(String[] args) throws Exception {
System.out.println("--->JRCMD MBean Test: invocation on \"operation info\"...");
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
JMXServiceURL url = new JMXServiceURL("rmi", null, 0);
JMXConnectorServer cs = null;
JMXConnector cc = null;
try {
cs = JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);
cs.start();
JMXServiceURL addr = cs.getAddress();
cc = JMXConnectorFactory.connect(addr);
MBeanServerConnection mbsc = cc.getMBeanServerConnection();
ObjectName name = new ObjectName(HOTSPOT_DIAGNOSTIC_MXBEAN_NAME);
MBeanInfo info = mbsc.getMBeanInfo(name);
// the test should check that the MBean doesn't have any
// Attribute, notification or constructor. Current version only
// check operations
System.out.println("Class Name:" + info.getClassName());
System.out.println("Description:" + info.getDescription());
MBeanOperationInfo[] opInfo = info.getOperations();
System.out.println("Operations:");
for (int i = 0; i < opInfo.length; i++) {
printOperation(opInfo[i]);
System.out.println("\[email protected]@@@@@\n");
}
} finally {
try {
cc.close();
cs.stop();
} catch (Exception e) {
}
}
System.out.println("Test passed");
}
private static void check(MBeanServer mbs, ObjectName on) throws Exception {
MBeanInfo mbi = mbs.getMBeanInfo(on);
// check the MBean itself
check(mbi);
// check attributes
MBeanAttributeInfo[] attrs = mbi.getAttributes();
for (MBeanAttributeInfo attr : attrs) {
check(attr);
if (attr.getName().equals("ReadOnly"))
check("@Full", attr.getDescriptor(), expectedFullDescriptor);
}
// check operations
MBeanOperationInfo[] ops = mbi.getOperations();
for (MBeanOperationInfo op : ops) {
check(op);
check(op.getSignature());
}
MBeanConstructorInfo[] constrs = mbi.getConstructors();
for (MBeanConstructorInfo constr : constrs) {
check(constr);
check(constr.getSignature());
}
}
public void testProcessMBean() throws Exception {
final String testName = "testProcessMBean";
final int pid = ProcessUtils.identifyPid();
final Process process = new Process(pid, true);
final ObjectName objectName = ObjectName.getInstance(
getClass().getSimpleName() + ":testName=" + testName);
final MBeanServer server = ManagementFactory.getPlatformMBeanServer();
final ObjectInstance instance = server.registerMBean(process, objectName);
assertNotNull(instance);
try {
// validate basics of the ProcessMBean
Set<ObjectName> mbeanNames = server.queryNames(objectName, null);
assertFalse("Zero matching mbeans", mbeanNames.isEmpty());
assertEquals(1, mbeanNames.size());
final ObjectName name = mbeanNames.iterator().next();
final MBeanInfo info = server.getMBeanInfo(name);
final MBeanOperationInfo[] operInfo = info.getOperations();
assertEquals(1, operInfo.length);
assertEquals("stop", operInfo[0].getName());
final MBeanAttributeInfo[] attrInfo = info.getAttributes();
assertEquals(2, attrInfo.length);
// The order of these attributes is indeterminate
// assertEquals("Pid", attrInfo[0].getName());
// assertEquals("Process", attrInfo[1].getName());
assertNotNull(server.getAttribute(name, "Pid"));
assertNotNull(server.getAttribute(name, "Process"));
assertEquals(pid, server.getAttribute(name, "Pid"));
assertEquals(true, server.getAttribute(name, "Process"));
// validate query using only Pid attribute
QueryExp constraint = Query.eq(
Query.attr("Pid"),
Query.value(pid));
mbeanNames = server.queryNames(objectName, constraint);
assertFalse("Zero matching mbeans", mbeanNames.isEmpty());
// validate query with wrong Pid finds nothing
constraint = Query.eq(
Query.attr("Pid"),
Query.value(pid+1));
mbeanNames = server.queryNames(objectName, constraint);
assertTrue("Found matching mbeans", mbeanNames.isEmpty());
// validate query using both attributes
constraint = Query.and(
Query.eq(Query.attr("Process"),Query.value(true)),
Query.eq(Query.attr("Pid"),Query.value(pid)));
mbeanNames = server.queryNames(objectName, constraint);
assertFalse("Zero matching mbeans", mbeanNames.isEmpty());
// validate query with wrong attribute finds nothing
constraint = Query.and(
Query.eq(Query.attr("Process"),Query.value(false)),
Query.eq(Query.attr("Pid"),Query.value(pid)));
mbeanNames = server.queryNames(objectName, constraint);
assertTrue("Found matching mbeans", mbeanNames.isEmpty());
} finally {
try {
server.unregisterMBean(objectName);
} catch (Throwable t) {
t.printStackTrace();
}
}
}
private static void test(Object child, String name, boolean mxbean)
throws Exception {
final ObjectName childName =
new ObjectName("test:type=Child,name="+name);
final MBeanServer server =
ManagementFactory.getPlatformMBeanServer();
server.registerMBean(child,childName);
try {
final MBeanInfo info = server.getMBeanInfo(childName);
System.out.println(name+": " + info.getDescriptor());
final int len = info.getOperations().length;
if (len == OPCOUNT) {
System.out.println(name+": OK, only "+OPCOUNT+
" operations here...");
} else {
final String qual = (len>OPCOUNT)?"many":"few";
System.err.println(name+": Too "+qual+" foos! Found "+
len+", expected "+OPCOUNT);
for (MBeanOperationInfo op : info.getOperations()) {
System.err.println("public "+op.getReturnType()+" "+
op.getName()+"();");
}
throw new RuntimeException("Too " + qual +
" foos for "+name);
}
final Descriptor d = info.getDescriptor();
final String mxstr = String.valueOf(d.getFieldValue("mxbean"));
final boolean mxb =
(mxstr==null)?false:Boolean.valueOf(mxstr).booleanValue();
System.out.println(name+": mxbean="+mxb);
if (mxbean && !mxb)
throw new AssertionError("MXBean is not OpenMBean?");
for (MBeanOperationInfo mboi : info.getOperations()) {
// Sanity check
if (mxbean && !mboi.getName().equals("foo")) {
// The spec doesn't guarantee that the MBeanOperationInfo
// of an MXBean will be an OpenMBeanOperationInfo, and in
// some circumstances in our implementation it will not.
// However, in thsi tests, for all methods but foo(),
// it should.
//
if (!(mboi instanceof OpenMBeanOperationInfo))
throw new AssertionError("Operation "+mboi.getName()+
"() is not Open?");
}
final String exp = EXPECTED_TYPES.get(mboi.getName());
// For MXBeans, we need to compare 'exp' with the original
// type - because mboi.getReturnType() returns the OpenType
//
String type = (String)mboi.getDescriptor().
getFieldValue("originalType");
if (type == null) type = mboi.getReturnType();
if (type.equals(exp)) continue;
System.err.println("Bad return type for "+
mboi.getName()+"! Found "+type+
", expected "+exp);
throw new RuntimeException("Bad return type for "+
mboi.getName());
}
} finally {
server.unregisterMBean(childName);
}
}
@Override
public LineRenderer renderer(Iterator<MBeanInfo> stream) {
List<LineRenderer> renderers = new ArrayList<LineRenderer>();
while (stream.hasNext()) {
MBeanInfo info = stream.next();
//
TreeElement root = new TreeElement(info.getClassName());
// Descriptor
TableElement descriptor = new TableElement().
overflow(Overflow.HIDDEN).
rightCellPadding(1);
Descriptor descriptorInfo = info.getDescriptor();
if (descriptorInfo != null) {
for (String fieldName : descriptorInfo.getFieldNames()) {
String fieldValue = String.valueOf(descriptorInfo.getFieldValue(fieldName));
descriptor.row(fieldName, fieldValue);
}
}
// Attributes
TableElement attributes = new TableElement().
overflow(Overflow.HIDDEN).
rightCellPadding(1).
add(new RowElement().style(Decoration.bold.fg(Color.black).bg(Color.white)).add("NAME", "TYPE", "DESCRIPTION"));
for (MBeanAttributeInfo attributeInfo : info.getAttributes()) {
attributes.row(attributeInfo.getName(), attributeInfo.getType(), attributeInfo.getDescription());
}
// Operations
TreeElement operations = new TreeElement("Operations");
for (MBeanOperationInfo operationInfo : info.getOperations()) {
TableElement signature = new TableElement().
overflow(Overflow.HIDDEN).
rightCellPadding(1);
MBeanParameterInfo[] parameterInfos = operationInfo.getSignature();
for (MBeanParameterInfo parameterInfo : parameterInfos) {
signature.row(parameterInfo.getName(), parameterInfo.getType(), parameterInfo.getDescription());
}
TreeElement operation = new TreeElement(operationInfo.getName());
String impact;
switch (operationInfo.getImpact()) {
case MBeanOperationInfo.ACTION:
impact = "ACTION";
break;
case MBeanOperationInfo.INFO:
impact = "INFO";
break;
case MBeanOperationInfo.ACTION_INFO:
impact = "ACTION_INFO";
break;
default:
impact = "UNKNOWN";
}
operation.addChild(new TableElement().
add(
new RowElement().add("Type: ", operationInfo.getReturnType()),
new RowElement().add("Description: ", operationInfo.getDescription()),
new RowElement().add("Impact: ", impact),
new RowElement().add(new LabelElement("Signature: "), signature)
)
);
operations.addChild(operation);
}
//
root.addChild(
new TableElement().leftCellPadding(1).overflow(Overflow.HIDDEN).
row("ClassName", info.getClassName()).
row("Description", info.getDescription()
)
);
root.addChild(new TreeElement("Descriptor").addChild(descriptor));
root.addChild(new TreeElement("Attributes").addChild(attributes));
root.addChild(operations);
//
renderers.add(root.renderer());
}
return LineRenderer.vertical(renderers);
}
/**
* Return all operations for the specified mbean name
* @param name
* @return
* @throws Exception
*/
public MBeanOperationInfo[] getMBeanOperations(String name) throws Exception {
MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
MBeanInfo mBeanInfo = mBeanServer.getMBeanInfo(new ObjectName(name));
return mBeanInfo.getOperations();
}