下面列出了javax.management.MBeanServerConnection#getMBeanInfo ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private static void fetchKafkaMetrics(String host, String jmxPort, String metric)
throws Exception {
Map<String, String[]> env = new HashMap<>();
JMXServiceURL address = new JMXServiceURL(
"service:jmx:rmi://" + host + "/jndi/rmi://" + host + ":" + jmxPort + "/jmxrmi");
JMXConnector connector = JMXConnectorFactory.connect(address, env);
MBeanServerConnection mbs = connector.getMBeanServerConnection();
ObjectName name = ObjectName.getInstance(metric);
MBeanInfo beanInfo = mbs.getMBeanInfo(name);
for (MBeanAttributeInfo attributeInfo : beanInfo.getAttributes()) {
Object obj = mbs.getAttribute(name, attributeInfo.getName());
System.out.println(" attributeName = " + attributeInfo.getName() + " " + obj.toString());
}
}
private static void printAttrs(
MBeanServerConnection mbsc1, Class<? extends Exception> expectX)
throws Exception {
Set<ObjectName> names = mbsc1.queryNames(null, null);
for (ObjectName name : names) {
System.out.println(name + ":");
MBeanInfo mbi = mbsc1.getMBeanInfo(name);
MBeanAttributeInfo[] mbais = mbi.getAttributes();
for (MBeanAttributeInfo mbai : mbais) {
String attr = mbai.getName();
Object value;
try {
value = mbsc1.getAttribute(name, attr);
} catch (Exception e) {
if (expectX != null && expectX.isInstance(e))
value = "<" + e + ">";
else
throw e;
}
String s = " " + attr + " = " + value;
if (s.length() > 80)
s = s.substring(0, 77) + "...";
System.out.println(s);
}
}
}
private static void printAttrs(
MBeanServerConnection mbsc1, Class<? extends Exception> expectX)
throws Exception {
Set<ObjectName> names = mbsc1.queryNames(null, null);
for (ObjectName name : names) {
System.out.println(name + ":");
MBeanInfo mbi = mbsc1.getMBeanInfo(name);
MBeanAttributeInfo[] mbais = mbi.getAttributes();
for (MBeanAttributeInfo mbai : mbais) {
String attr = mbai.getName();
Object value;
try {
value = mbsc1.getAttribute(name, attr);
} catch (Exception e) {
if (expectX != null && expectX.isInstance(e))
value = "<" + e + ">";
else
throw e;
}
String s = " " + attr + " = " + value;
if (s.length() > 80)
s = s.substring(0, 77) + "...";
System.out.println(s);
}
}
}
private final int doGarbageCollectorMXBeanTest(MBeanServerConnection mbsc) {
int errorCount = 0 ;
System.out.println("---- GarbageCollectorMXBean") ;
try {
ObjectName filterName =
new ObjectName(ManagementFactory.GARBAGE_COLLECTOR_MXBEAN_DOMAIN_TYPE
+ ",*");
Set<ObjectName> onSet = mbsc.queryNames(filterName, null);
for (Iterator<ObjectName> iter = onSet.iterator(); iter.hasNext(); ) {
ObjectName garbageName = iter.next() ;
System.out.println("-------- " + garbageName) ;
MBeanInfo mbInfo = mbsc.getMBeanInfo(garbageName);
errorCount += checkNonEmpty(mbInfo);
System.out.println("getMBeanInfo\t\t" + mbInfo);
GarbageCollectorMXBean garbage = null ;
garbage =
JMX.newMXBeanProxy(mbsc,
garbageName,
GarbageCollectorMXBean.class) ;
System.out.println("getCollectionCount\t\t"
+ garbage.getCollectionCount());
System.out.println("getCollectionTime\t\t"
+ garbage.getCollectionTime());
}
System.out.println("---- OK\n") ;
} catch (Exception e) {
Utils.printThrowable(e, true) ;
errorCount++ ;
System.out.println("---- ERROR\n") ;
}
return errorCount ;
}
private final int doMemoryManagerMXBeanTest(MBeanServerConnection mbsc) {
int errorCount = 0 ;
System.out.println("---- MemoryManagerMXBean") ;
try {
ObjectName filterName =
new ObjectName(ManagementFactory.MEMORY_MANAGER_MXBEAN_DOMAIN_TYPE
+ ",*");
Set<ObjectName> onSet = mbsc.queryNames(filterName, null);
for (Iterator<ObjectName> iter = onSet.iterator(); iter.hasNext(); ) {
ObjectName memoryManagerName = iter.next() ;
System.out.println("-------- " + memoryManagerName) ;
MBeanInfo mbInfo = mbsc.getMBeanInfo(memoryManagerName);
System.out.println("getMBeanInfo\t\t" + mbInfo);
errorCount += checkNonEmpty(mbInfo);
MemoryManagerMXBean memoryManager = null;
memoryManager =
JMX.newMXBeanProxy(mbsc,
memoryManagerName,
MemoryManagerMXBean.class) ;
System.out.println("getMemoryPoolNames\t\t"
+ Arrays.deepToString(memoryManager.getMemoryPoolNames()));
System.out.println("getName\t\t"
+ memoryManager.getName());
System.out.println("isValid\t\t"
+ memoryManager.isValid());
}
System.out.println("---- OK\n") ;
} catch (Exception e) {
Utils.printThrowable(e, true) ;
errorCount++ ;
System.out.println("---- ERROR\n") ;
}
return errorCount ;
}
private final int doGarbageCollectorMXBeanTest(MBeanServerConnection mbsc) {
int errorCount = 0 ;
System.out.println("---- GarbageCollectorMXBean") ;
try {
ObjectName filterName =
new ObjectName(ManagementFactory.GARBAGE_COLLECTOR_MXBEAN_DOMAIN_TYPE
+ ",*");
Set<ObjectName> onSet = mbsc.queryNames(filterName, null);
for (Iterator<ObjectName> iter = onSet.iterator(); iter.hasNext(); ) {
ObjectName garbageName = iter.next() ;
System.out.println("-------- " + garbageName) ;
MBeanInfo mbInfo = mbsc.getMBeanInfo(garbageName);
errorCount += checkNonEmpty(mbInfo);
System.out.println("getMBeanInfo\t\t" + mbInfo);
GarbageCollectorMXBean garbage = null ;
garbage =
JMX.newMXBeanProxy(mbsc,
garbageName,
GarbageCollectorMXBean.class) ;
System.out.println("getCollectionCount\t\t"
+ garbage.getCollectionCount());
System.out.println("getCollectionTime\t\t"
+ garbage.getCollectionTime());
}
System.out.println("---- OK\n") ;
} catch (Exception e) {
Utils.printThrowable(e, true) ;
errorCount++ ;
System.out.println("---- ERROR\n") ;
}
return errorCount ;
}
private final int doGarbageCollectorMXBeanTest(MBeanServerConnection mbsc) {
int errorCount = 0 ;
System.out.println("---- GarbageCollectorMXBean") ;
try {
ObjectName filterName =
new ObjectName(ManagementFactory.GARBAGE_COLLECTOR_MXBEAN_DOMAIN_TYPE
+ ",*");
Set<ObjectName> onSet = mbsc.queryNames(filterName, null);
for (Iterator<ObjectName> iter = onSet.iterator(); iter.hasNext(); ) {
ObjectName garbageName = iter.next() ;
System.out.println("-------- " + garbageName) ;
MBeanInfo mbInfo = mbsc.getMBeanInfo(garbageName);
errorCount += checkNonEmpty(mbInfo);
System.out.println("getMBeanInfo\t\t" + mbInfo);
GarbageCollectorMXBean garbage = null ;
garbage =
JMX.newMXBeanProxy(mbsc,
garbageName,
GarbageCollectorMXBean.class) ;
System.out.println("getCollectionCount\t\t"
+ garbage.getCollectionCount());
System.out.println("getCollectionTime\t\t"
+ garbage.getCollectionTime());
}
System.out.println("---- OK\n") ;
} catch (Exception e) {
Utils.printThrowable(e, true) ;
errorCount++ ;
System.out.println("---- ERROR\n") ;
}
return errorCount ;
}
private final int doClassLoadingMXBeanTest(MBeanServerConnection mbsc) {
int errorCount = 0 ;
System.out.println("---- ClassLoadingMXBean") ;
try {
ObjectName classLoadingName =
new ObjectName(ManagementFactory.CLASS_LOADING_MXBEAN_NAME) ;
MBeanInfo mbInfo = mbsc.getMBeanInfo(classLoadingName);
errorCount += checkNonEmpty(mbInfo);
System.out.println("getMBeanInfo\t\t"
+ mbInfo);
ClassLoadingMXBean classLoading = null;
classLoading = JMX.newMXBeanProxy(mbsc,
classLoadingName,
ClassLoadingMXBean.class) ;
System.out.println("getLoadedClassCount\t\t"
+ classLoading.getLoadedClassCount());
System.out.println("getTotalLoadedClassCount\t\t"
+ classLoading.getTotalLoadedClassCount());
System.out.println("getUnloadedClassCount\t\t"
+ classLoading.getUnloadedClassCount());
System.out.println("isVerbose\t\t"
+ classLoading.isVerbose());
System.out.println("---- OK\n") ;
} catch (Exception e) {
Utils.printThrowable(e, true) ;
errorCount++ ;
System.out.println("---- ERROR\n") ;
}
return errorCount ;
}
@Test
public void testInvokeOperationExpressionsStandalone() throws Exception {
MBeanServerConnection connection = setupAndGetConnection(new MBeanInfoAdditionalInitialization(ProcessType.STANDALONE_SERVER, new TestExtension(true)));
ObjectName name = createObjectName(EXPR_DOMAIN + ":subsystem=test");
ModelControllerResourceDefinition.VoidOperationNoParams.INSTANCE.invoked = false;
Assert.assertNull(connection.invoke(name, ModelControllerResourceDefinition.VoidOperationNoParams.OPERATION_JMX_NAME, null, null));
Assert.assertTrue(ModelControllerResourceDefinition.VoidOperationNoParams.INSTANCE.invoked);
String result = assertCast(String.class, connection.invoke(
name,
ModelControllerResourceDefinition.IntOperationWithParams.OPERATION_JMX_NAME,
new Object[]{"${should.not.exist!!!!!:100}", new String[]{"${should.not.exist!!!!!:A}"}, Collections.singletonMap("test", "${should.not.exist!!!!!:3}"), "${should.not.exist!!!!!:5}", "${should.not.exist!!!!!:5}"},
new String[]{Long.class.getName(), String[].class.getName(), Map.class.getName(), Integer.class.getName(), Integer.class.getName()}));
Assert.assertEquals("A105", result);
Assert.assertTrue(ModelControllerResourceDefinition.IntOperationWithParams.INSTANCE_EXPRESSIONS.invoked);
MBeanInfo info = connection.getMBeanInfo(name);
CompositeType complexType = assertCast(CompositeType.class, findAttribute(info.getAttributes(), "complex").getOpenType());
CompositeData complexData = createComplexData(connection, complexType, "${should.not.exist!!!!!:5}", "${should.not.exist!!!!!:10}");
Assert.assertEquals(complexData, assertCast(CompositeData.class, connection.invoke(
name,
ModelControllerResourceDefinition.ComplexOperation.OPERATION_NAME,
new Object[]{complexData},
new String[]{CompositeData.class.getName()})));
}
private final int doClassLoadingMXBeanTest(MBeanServerConnection mbsc) {
int errorCount = 0 ;
System.out.println("---- ClassLoadingMXBean") ;
try {
ObjectName classLoadingName =
new ObjectName(ManagementFactory.CLASS_LOADING_MXBEAN_NAME) ;
MBeanInfo mbInfo = mbsc.getMBeanInfo(classLoadingName);
errorCount += checkNonEmpty(mbInfo);
System.out.println("getMBeanInfo\t\t"
+ mbInfo);
ClassLoadingMXBean classLoading = null;
classLoading = JMX.newMXBeanProxy(mbsc,
classLoadingName,
ClassLoadingMXBean.class) ;
System.out.println("getLoadedClassCount\t\t"
+ classLoading.getLoadedClassCount());
System.out.println("getTotalLoadedClassCount\t\t"
+ classLoading.getTotalLoadedClassCount());
System.out.println("getUnloadedClassCount\t\t"
+ classLoading.getUnloadedClassCount());
System.out.println("isVerbose\t\t"
+ classLoading.isVerbose());
System.out.println("---- OK\n") ;
} catch (Exception e) {
Utils.printThrowable(e, true) ;
errorCount++ ;
System.out.println("---- ERROR\n") ;
}
return errorCount ;
}
private final int doMemoryManagerMXBeanTest(MBeanServerConnection mbsc) {
int errorCount = 0 ;
System.out.println("---- MemoryManagerMXBean") ;
try {
ObjectName filterName =
new ObjectName(ManagementFactory.MEMORY_MANAGER_MXBEAN_DOMAIN_TYPE
+ ",*");
Set<ObjectName> onSet = mbsc.queryNames(filterName, null);
for (Iterator<ObjectName> iter = onSet.iterator(); iter.hasNext(); ) {
ObjectName memoryManagerName = iter.next() ;
System.out.println("-------- " + memoryManagerName) ;
MBeanInfo mbInfo = mbsc.getMBeanInfo(memoryManagerName);
System.out.println("getMBeanInfo\t\t" + mbInfo);
errorCount += checkNonEmpty(mbInfo);
MemoryManagerMXBean memoryManager = null;
memoryManager =
JMX.newMXBeanProxy(mbsc,
memoryManagerName,
MemoryManagerMXBean.class) ;
System.out.println("getMemoryPoolNames\t\t"
+ Arrays.deepToString(memoryManager.getMemoryPoolNames()));
System.out.println("getName\t\t"
+ memoryManager.getName());
System.out.println("isValid\t\t"
+ memoryManager.isValid());
}
System.out.println("---- OK\n") ;
} catch (Exception e) {
Utils.printThrowable(e, true) ;
errorCount++ ;
System.out.println("---- ERROR\n") ;
}
return errorCount ;
}
public static ObjectName getRequiredObjectName(ObjectName origName, ObjectName confName, String operationName, MBeanServerConnection in_conn){
ObjectName oName = null;
try{
if(origName != null){
MBeanInfo bnInfo = in_conn.getMBeanInfo(origName);
MBeanOperationInfo[] operInfo = bnInfo.getOperations();
String[] operNames = new String[operInfo.length];
for(int i=0; i<operInfo.length; i++){
operNames[i] = operInfo[i].getName();
}
Set operList = new HashSet(Arrays.asList(operNames));
if(operList.contains(operationName)){
oName = origName;
return oName;
}
}
MBeanInfo beanInfo = in_conn.getMBeanInfo(confName);
MBeanOperationInfo[] operationInfo = beanInfo.getOperations();
String[] operationNames = new String[operationInfo.length];
for(int i=0; i<operationInfo.length; i++){
operationNames[i] = operationInfo[i].getName();
}
Set newOperList = new HashSet(Arrays.asList(operationNames));
if(newOperList.contains(operationName)){
oName = confName;
}
}catch(Exception ex){
//System.out.println("Error in getRequiredObjectName " + ex.getLocalizedMessage());
return oName;
}
return oName;
}
private final int doMemoryMXBeanTest(MBeanServerConnection mbsc) {
int errorCount = 0 ;
System.out.println("---- MemoryMXBean") ;
try {
ObjectName memoryName =
new ObjectName(ManagementFactory.MEMORY_MXBEAN_NAME) ;
MBeanInfo mbInfo = mbsc.getMBeanInfo(memoryName);
errorCount += checkNonEmpty(mbInfo);
System.out.println("getMBeanInfo\t\t"
+ mbInfo);
MemoryMXBean memory = null ;
memory =
JMX.newMXBeanProxy(mbsc,
memoryName,
MemoryMXBean.class,
true) ;
System.out.println("getMemoryHeapUsage\t\t"
+ memory.getHeapMemoryUsage());
System.out.println("getNonHeapMemoryHeapUsage\t\t"
+ memory.getNonHeapMemoryUsage());
System.out.println("getObjectPendingFinalizationCount\t\t"
+ memory.getObjectPendingFinalizationCount());
System.out.println("isVerbose\t\t"
+ memory.isVerbose());
System.out.println("---- OK\n") ;
} catch (Exception e) {
Utils.printThrowable(e, true) ;
errorCount++ ;
System.out.println("---- ERROR\n") ;
}
return errorCount ;
}
private final int doMemoryPoolMXBeanTest(MBeanServerConnection mbsc) {
int errorCount = 0 ;
System.out.println("---- MemoryPoolMXBean") ;
try {
ObjectName filterName =
new ObjectName(ManagementFactory.MEMORY_POOL_MXBEAN_DOMAIN_TYPE
+ ",*");
Set<ObjectName> onSet = mbsc.queryNames(filterName, null);
for (Iterator<ObjectName> iter = onSet.iterator(); iter.hasNext(); ) {
ObjectName memoryPoolName = iter.next() ;
System.out.println("-------- " + memoryPoolName) ;
MBeanInfo mbInfo = mbsc.getMBeanInfo(memoryPoolName);
errorCount += checkNonEmpty(mbInfo);
System.out.println("getMBeanInfo\t\t" + mbInfo);
MemoryPoolMXBean memoryPool = null;
memoryPool =
JMX.newMXBeanProxy(mbsc,
memoryPoolName,
MemoryPoolMXBean.class,
true) ;
System.out.println("getCollectionUsage\t\t"
+ memoryPool.getCollectionUsage());
System.out.println("getMemoryManagerNames\t\t"
+ Arrays.deepToString(memoryPool.getMemoryManagerNames()));
System.out.println("getName\t\t"
+ memoryPool.getName());
System.out.println("getPeakUsage\t\t"
+ memoryPool.getPeakUsage());
System.out.println("getType\t\t"
+ memoryPool.getType());
System.out.println("getUsage\t\t"
+ memoryPool.getUsage());
System.out.println("isValid\t\t"
+ memoryPool.isValid());
boolean supported = memoryPool.isUsageThresholdSupported() ;
System.out.println("isUsageThresholdSupported\t\t"
+ supported);
if ( supported ) {
System.out.println("getUsageThreshold\t\t"
+ memoryPool.getUsageThreshold());
System.out.println("isUsageThresholdExceeded\t\t"
+ memoryPool.isUsageThresholdExceeded());
System.out.println("getUsageThresholdCount\t\t"
+ memoryPool.getUsageThresholdCount());
}
supported = memoryPool.isCollectionUsageThresholdSupported() ;
System.out.println("isCollectionUsageThresholdSupported\t\t"
+ supported);
if ( supported ) {
System.out.println("getCollectionUsageThreshold\t\t"
+ memoryPool.getCollectionUsageThreshold());
System.out.println("getCollectionUsageThresholdCount\t\t"
+ memoryPool.getCollectionUsageThresholdCount());
System.out.println("isCollectionUsageThresholdExceeded\t\t"
+ memoryPool.isCollectionUsageThresholdExceeded());
}
memoryPool.resetPeakUsage();
}
System.out.println("---- OK\n") ;
} catch (Exception e) {
Utils.printThrowable(e, true) ;
errorCount++ ;
System.out.println("---- ERROR\n") ;
}
return errorCount ;
}
public static void main(String[] args) throws Exception {
MBeanServer mbs = MBeanServerFactory.newMBeanServer();
ObjectName name = new ObjectName("a:b=c");
Resource resource1 = new Resource();
Resource resource2 = new Resource();
Resource resource3 = new Resource();
Method operationMethod = Resource.class.getMethod("operation");
Method getCountMethod = Resource.class.getMethod("getCount");
Method setCountMethod = Resource.class.getMethod("setCount", int.class);
Descriptor operationDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "targetObject"
}, new Object[] {
"operation", "operation", resource1
});
Descriptor getCountDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "targetObject"
}, new Object[] {
"operation", "getCount", resource2
});
Descriptor setCountDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "targetObject"
}, new Object[] {
"operation", "setCount", resource2
});
Descriptor countDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "getMethod", "setMethod"
}, new Object[] {
"attribute", "Count", "getCount", "setCount"
});
ModelMBeanOperationInfo operationInfo =
new ModelMBeanOperationInfo("operation description",
operationMethod, operationDescriptor);
ModelMBeanOperationInfo getCountInfo =
new ModelMBeanOperationInfo("getCount description",
getCountMethod, getCountDescriptor);
ModelMBeanOperationInfo setCountInfo =
new ModelMBeanOperationInfo("setCount description",
setCountMethod, setCountDescriptor);
ModelMBeanAttributeInfo countInfo =
new ModelMBeanAttributeInfo("Count", "Count description",
getCountMethod, setCountMethod,
countDescriptor);
ModelMBeanInfo mmbi =
new ModelMBeanInfoSupport(Resource.class.getName(),
"ModelMBean to test targetObject",
new ModelMBeanAttributeInfo[] {countInfo},
null, // no constructors
new ModelMBeanOperationInfo[] {
operationInfo, getCountInfo, setCountInfo
},
null); // no notifications
ModelMBean mmb = new RequiredModelMBean(mmbi);
mmb.setManagedResource(resource3, "ObjectReference");
mbs.registerMBean(mmb, name);
mbs.invoke(name, "operation", null, null);
mbs.setAttribute(name, new Attribute("Count", 53));
if (resource1.operationCount != 1)
throw new Exception("operationCount: " + resource1.operationCount);
if (resource2.count != 53)
throw new Exception("count: " + resource2.count);
int got = (Integer) mbs.getAttribute(name, "Count");
if (got != 53)
throw new Exception("got count: " + got);
JMXServiceURL url = new JMXServiceURL("rmi", null, 0);
JMXConnectorServer cs =
JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);
cs.start();
JMXServiceURL addr = cs.getAddress();
JMXConnector cc = JMXConnectorFactory.connect(addr);
MBeanServerConnection mbsc = cc.getMBeanServerConnection();
ModelMBeanInfo rmmbi = (ModelMBeanInfo) mbsc.getMBeanInfo(name);
// Above gets NotSerializableException if resource included in
// serialized form
cc.close();
cs.stop();
System.out.println("TEST PASSED");
}
@Test
public void testAddMethodSingleFixedChild() throws Exception {
final ObjectName testObjectName = createObjectName(LEGACY_DOMAIN + ":subsystem=test");
final ObjectName childObjectName = createObjectName(LEGACY_DOMAIN + ":subsystem=test,single=only");
MBeanServerConnection connection = setupAndGetConnection(new MBeanInfoAdditionalInitialization(ProcessType.STANDALONE_SERVER, new SubystemWithSingleFixedChildExtension()));
Set<ObjectName> names = connection.queryNames(createObjectName(LEGACY_DOMAIN + ":subsystem=test,*"), null);
Assert.assertEquals(1, names.size());
Assert.assertTrue(names.contains(testObjectName));
MBeanInfo subsystemInfo = connection.getMBeanInfo(testObjectName);
Assert.assertEquals(0, subsystemInfo.getAttributes().length);
Assert.assertEquals(1, subsystemInfo.getOperations().length);
OpenMBeanOperationInfo op = findOperation(subsystemInfo.getOperations(), "addSingleOnly");
Assert.assertEquals("add", op.getDescription());
Assert.assertEquals(1, op.getSignature().length);
Assert.assertEquals(Integer.class.getName(), op.getSignature()[0].getType());
connection.invoke(testObjectName, "addSingleOnly", new Object[]{123}, new String[]{String.class.getName()});
names = connection.queryNames(createObjectName(LEGACY_DOMAIN + ":subsystem=test,*"), null);
Assert.assertEquals(2, names.size());
Assert.assertTrue(names.contains(testObjectName));
Assert.assertTrue(names.contains(childObjectName));
subsystemInfo = connection.getMBeanInfo(testObjectName);
Assert.assertEquals(0, subsystemInfo.getAttributes().length);
Assert.assertEquals(1, subsystemInfo.getOperations().length);
op = findOperation(subsystemInfo.getOperations(), "addSingleOnly");
Assert.assertEquals("add", op.getDescription());
Assert.assertEquals(1, op.getSignature().length);
Assert.assertEquals(Integer.class.getName(), op.getSignature()[0].getType());
MBeanInfo childInfo = connection.getMBeanInfo(childObjectName);
Assert.assertEquals(1, childInfo.getAttributes().length);
Assert.assertEquals(Integer.class.getName(), childInfo.getAttributes()[0].getType());
Assert.assertEquals(1, childInfo.getOperations().length);
op = findOperation(childInfo.getOperations(), REMOVE);
Assert.assertEquals("remove", op.getDescription());
Assert.assertEquals(0, op.getSignature().length);
Assert.assertEquals(123, connection.getAttribute(childObjectName, "attr"));
try {
connection.invoke(testObjectName, "addSingleOnly", new Object[]{123}, new String[]{String.class.getName()});
Assert.fail("Should not have been able to register a duplicate resource");
} catch (Exception expected) {
//expected
}
connection.invoke(childObjectName, REMOVE, new Object[]{}, new String[]{});
names = connection.queryNames(createObjectName(LEGACY_DOMAIN + ":subsystem=test,*"), null);
Assert.assertEquals(1, names.size());
Assert.assertTrue(names.contains(testObjectName));
}
private final int doThreadMXBeanTest(MBeanServerConnection mbsc) {
int errorCount = 0 ;
System.out.println("---- ThreadMXBean") ;
try {
ObjectName threadName =
new ObjectName(ManagementFactory.THREAD_MXBEAN_NAME) ;
MBeanInfo mbInfo = mbsc.getMBeanInfo(threadName);
errorCount += checkNonEmpty(mbInfo);
System.out.println("getMBeanInfo\t\t" + mbInfo);
ThreadMXBean thread = null ;
thread =
JMX.newMXBeanProxy(mbsc,
threadName,
ThreadMXBean.class) ;
System.out.println("findMonitorDeadlockedThreads\t\t"
+ thread.findMonitorDeadlockedThreads());
long[] threadIDs = thread.getAllThreadIds() ;
System.out.println("getAllThreadIds\t\t"
+ threadIDs);
for ( long threadID : threadIDs ) {
System.out.println("getThreadInfo long\t\t"
+ thread.getThreadInfo(threadID));
System.out.println("getThreadInfo long, int\t\t"
+ thread.getThreadInfo(threadID, 2));
}
System.out.println("getThreadInfo long[]\t\t"
+ thread.getThreadInfo(threadIDs));
System.out.println("getThreadInfo long[], int\t\t"
+ thread.getThreadInfo(threadIDs, 2));
System.out.println("getDaemonThreadCount\t\t"
+ thread.getDaemonThreadCount());
System.out.println("getPeakThreadCount\t\t"
+ thread.getPeakThreadCount());
System.out.println("getThreadCount\t\t"
+ thread.getThreadCount());
System.out.println("getTotalStartedThreadCount\t\t"
+ thread.getTotalStartedThreadCount());
boolean supported = thread.isThreadContentionMonitoringSupported() ;
System.out.println("isThreadContentionMonitoringSupported\t\t"
+ supported);
if ( supported ) {
System.out.println("isThreadContentionMonitoringEnabled\t\t"
+ thread.isThreadContentionMonitoringEnabled());
}
supported = thread.isThreadCpuTimeSupported() ;
System.out.println("isThreadCpuTimeSupported\t\t"
+ supported);
if ( supported ) {
System.out.println("isThreadCpuTimeEnabled\t\t"
+ thread.isThreadCpuTimeEnabled());
for (long id : threadIDs) {
System.out.println("getThreadCpuTime(" + id + ")\t\t"
+ thread.getThreadCpuTime(id));
System.out.println("getThreadUserTime(" + id + ")\t\t"
+ thread.getThreadUserTime(id));
}
}
supported = thread.isCurrentThreadCpuTimeSupported() ;
System.out.println("isCurrentThreadCpuTimeSupported\t\t"
+ supported);
if ( supported ) {
System.out.println("getCurrentThreadCpuTime\t\t"
+ thread.getCurrentThreadCpuTime());
System.out.println("getCurrentThreadUserTime\t\t"
+ thread.getCurrentThreadUserTime());
}
thread.resetPeakThreadCount() ;
System.out.println("---- OK\n") ;
} catch (Exception e) {
Utils.printThrowable(e, true) ;
errorCount++ ;
System.out.println("---- ERROR\n") ;
}
return errorCount ;
}
public static void main(String[] args) throws Exception {
MBeanServer mbs = MBeanServerFactory.newMBeanServer();
ObjectName name = new ObjectName("a:b=c");
Resource resource1 = new Resource();
Resource resource2 = new Resource();
Resource resource3 = new Resource();
Method operationMethod = Resource.class.getMethod("operation");
Method getCountMethod = Resource.class.getMethod("getCount");
Method setCountMethod = Resource.class.getMethod("setCount", int.class);
Descriptor operationDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "targetObject"
}, new Object[] {
"operation", "operation", resource1
});
Descriptor getCountDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "targetObject"
}, new Object[] {
"operation", "getCount", resource2
});
Descriptor setCountDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "targetObject"
}, new Object[] {
"operation", "setCount", resource2
});
Descriptor countDescriptor =
new DescriptorSupport(new String[] {
"descriptorType", "name", "getMethod", "setMethod"
}, new Object[] {
"attribute", "Count", "getCount", "setCount"
});
ModelMBeanOperationInfo operationInfo =
new ModelMBeanOperationInfo("operation description",
operationMethod, operationDescriptor);
ModelMBeanOperationInfo getCountInfo =
new ModelMBeanOperationInfo("getCount description",
getCountMethod, getCountDescriptor);
ModelMBeanOperationInfo setCountInfo =
new ModelMBeanOperationInfo("setCount description",
setCountMethod, setCountDescriptor);
ModelMBeanAttributeInfo countInfo =
new ModelMBeanAttributeInfo("Count", "Count description",
getCountMethod, setCountMethod,
countDescriptor);
ModelMBeanInfo mmbi =
new ModelMBeanInfoSupport(Resource.class.getName(),
"ModelMBean to test targetObject",
new ModelMBeanAttributeInfo[] {countInfo},
null, // no constructors
new ModelMBeanOperationInfo[] {
operationInfo, getCountInfo, setCountInfo
},
null); // no notifications
ModelMBean mmb = new RequiredModelMBean(mmbi);
mmb.setManagedResource(resource3, "ObjectReference");
mbs.registerMBean(mmb, name);
mbs.invoke(name, "operation", null, null);
mbs.setAttribute(name, new Attribute("Count", 53));
if (resource1.operationCount != 1)
throw new Exception("operationCount: " + resource1.operationCount);
if (resource2.count != 53)
throw new Exception("count: " + resource2.count);
int got = (Integer) mbs.getAttribute(name, "Count");
if (got != 53)
throw new Exception("got count: " + got);
JMXServiceURL url = new JMXServiceURL("rmi", null, 0);
JMXConnectorServer cs =
JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);
cs.start();
JMXServiceURL addr = cs.getAddress();
JMXConnector cc = JMXConnectorFactory.connect(addr);
MBeanServerConnection mbsc = cc.getMBeanServerConnection();
ModelMBeanInfo rmmbi = (ModelMBeanInfo) mbsc.getMBeanInfo(name);
// Above gets NotSerializableException if resource included in
// serialized form
cc.close();
cs.stop();
System.out.println("TEST PASSED");
}
private final int doBasicMXBeanTest(MBeanServerConnection mbsc) {
int errorCount = 0 ;
System.out.println("---- doBasicMXBeanTest") ;
try {
ObjectName objName =
new ObjectName("sqe:type=BasicMXBean") ;
mbsc.createMBean(BASIC_MXBEAN_CLASS_NAME, objName);
MBeanInfo mbInfo = mbsc.getMBeanInfo(objName);
printMBeanInfo(mbInfo);
System.out.println("---- OK\n") ;
System.out.println("getMBeanInfo\t\t"
+ mbInfo);
System.out.println("---- OK\n") ;
System.out.println("Check mxbean field in the MBeanInfo");
String mxbeanField =
(String)mbInfo.getDescriptor().getFieldValue(JMX.MXBEAN_FIELD);
if ( mxbeanField == null || ! mxbeanField.equals("true")) {
System.out.println("---- ERROR : Improper mxbean field value "
+ mxbeanField);
errorCount++;
}
System.out.println("---- OK\n") ;
System.out.println("Set attribute ObjectNameAtt");
Attribute att = new Attribute("ObjectNameAtt", objName);
mbsc.setAttribute(objName, att);
ObjectName value =
(ObjectName)mbsc.getAttribute(objName, "ObjectNameAtt");
if ( ! value.equals(objName) ) {
errorCount++;
System.out.println("---- ERROR : setAttribute failed, got "
+ value
+ " while expecting "
+ objName);
}
System.out.println("---- OK\n") ;
System.out.println("Call operation doNothing");
mbsc.invoke(objName, "doNothing", null, null);
System.out.println("---- OK\n") ;
System.out.println("Call operation getWeather");
Object weather = mbsc.invoke(objName,
"getWeather",
new Object[]{Boolean.TRUE},
new String[]{"boolean"});
System.out.println("Weather is " + weather);
System.out.println("---- OK\n") ;
} catch (Exception e) {
Utils.printThrowable(e, true) ;
errorCount++ ;
System.out.println("---- ERROR\n") ;
}
return errorCount ;
}
private final int doBasicMXBeanTest(MBeanServerConnection mbsc) {
int errorCount = 0 ;
System.out.println("---- doBasicMXBeanTest") ;
try {
ObjectName objName =
new ObjectName("sqe:type=BasicMXBean") ;
mbsc.createMBean(BASIC_MXBEAN_CLASS_NAME, objName);
MBeanInfo mbInfo = mbsc.getMBeanInfo(objName);
printMBeanInfo(mbInfo);
System.out.println("---- OK\n") ;
System.out.println("getMBeanInfo\t\t"
+ mbInfo);
System.out.println("---- OK\n") ;
System.out.println("Check mxbean field in the MBeanInfo");
String mxbeanField =
(String)mbInfo.getDescriptor().getFieldValue(JMX.MXBEAN_FIELD);
if ( mxbeanField == null || ! mxbeanField.equals("true")) {
System.out.println("---- ERROR : Improper mxbean field value "
+ mxbeanField);
errorCount++;
}
System.out.println("---- OK\n") ;
System.out.println("Set attribute ObjectNameAtt");
Attribute att = new Attribute("ObjectNameAtt", objName);
mbsc.setAttribute(objName, att);
ObjectName value =
(ObjectName)mbsc.getAttribute(objName, "ObjectNameAtt");
if ( ! value.equals(objName) ) {
errorCount++;
System.out.println("---- ERROR : setAttribute failed, got "
+ value
+ " while expecting "
+ objName);
}
System.out.println("---- OK\n") ;
System.out.println("Call operation doNothing");
mbsc.invoke(objName, "doNothing", null, null);
System.out.println("---- OK\n") ;
System.out.println("Call operation getWeather");
Object weather = mbsc.invoke(objName,
"getWeather",
new Object[]{Boolean.TRUE},
new String[]{"boolean"});
System.out.println("Weather is " + weather);
System.out.println("---- OK\n") ;
} catch (Exception e) {
Utils.printThrowable(e, true) ;
errorCount++ ;
System.out.println("---- ERROR\n") ;
}
return errorCount ;
}