assertEquals(ManagementFactory.THREAD_MXBEAN_NAME, mo.getName());
validateComponentType(mo);
ThreadMXBean mbean = ManagementFactory.getThreadMXBean();
long threadID = Thread.currentThread().getId();
ThreadInfo threadInfo = mbean.getThreadInfo(threadID, 3);
// Test ThreadInfo MetaValue wrap/unwrap
MetaValueFactory metaValueFactory = MetaValueFactory.getInstance();
CompositeDataMetaValueBuilder builder = new CompositeDataMetaValueBuilder();
builder.setMetaValueFactory(metaValueFactory);
metaValueFactory.setBuilder(CompositeData.class, builder);
metaValueFactory.setBuilder(CompositeDataSupport.class, builder);
MetaValue threadInfoMV = metaValueFactory.create(threadInfo);
log.debug("ThreadInfo.MV: "+threadInfoMV);
assertTrue(threadInfoMV instanceof CompositeValue);
CompositeValue tiCV = CompositeValue.class.cast(threadInfoMV);
ThreadInfo threadInfo2 = ManagementFactoryUtils.unwrapThreadInfo(tiCV);
assertEquals(threadInfo.getLockOwnerName(), threadInfo2.getLockOwnerName());
assertEquals(threadInfo.getThreadName(), threadInfo2.getThreadName());
assertEquals(threadInfo.isInNative(), threadInfo2.isInNative());
assertEquals(threadInfo.getBlockedCount(), threadInfo2.getBlockedCount());
assertEquals(threadInfo.getBlockedTime(), threadInfo2.getBlockedTime());
assertEquals(threadInfo.getLockOwnerId(), threadInfo2.getLockOwnerId());
assertEquals(threadInfo.getThreadId(), threadInfo2.getThreadId());
assertEquals(threadInfo.getThreadState(), threadInfo2.getThreadState());
assertEquals(threadInfo.getWaitedCount(), threadInfo2.getWaitedCount());
assertEquals(threadInfo.getWaitedTime(), threadInfo2.getWaitedTime());
StackTraceElement[] st = threadInfo.getStackTrace();
StackTraceElement[] st2 = threadInfo2.getStackTrace();
for(int n = 0; n < st.length; n ++)
{
assertEquals(st[n], st2[n]);
}
// Properties
ManagedProperty allThreadIds = mo.getProperty("allThreadIds");
assertNotNull(allThreadIds);
ManagedProperty currentThreadCpuTime = mo.getProperty("currentThreadCpuTime");
assertNotNull(currentThreadCpuTime);
long x = (Long) metaValueFactory.unwrap(currentThreadCpuTime.getValue());
assertTrue(x > 1000);
ManagedProperty currentThreadUserTime = mo.getProperty("currentThreadUserTime");
assertNotNull(currentThreadUserTime);
ManagedProperty daemonThreadCount = mo.getProperty("daemonThreadCount");
assertNotNull(daemonThreadCount);
x = (Integer) metaValueFactory.unwrap(daemonThreadCount.getValue());
assertEquals(mbean.getDaemonThreadCount(), x);
ManagedProperty peakThreadCount = mo.getProperty("peakThreadCount");
assertNotNull(peakThreadCount);
x = (Integer) metaValueFactory.unwrap(peakThreadCount.getValue());
assertEquals(mbean.getPeakThreadCount(), x);
ManagedProperty threadCount = mo.getProperty("threadCount");
assertNotNull(threadCount);
x = (Integer) metaValueFactory.unwrap(threadCount.getValue());
assertEquals(mbean.getThreadCount(), x);
ManagedProperty totalStartedThreadCount = mo.getProperty("totalStartedThreadCount");
assertNotNull(totalStartedThreadCount);
x = (Long) metaValueFactory.unwrap(totalStartedThreadCount.getValue());
assertEquals(mbean.getTotalStartedThreadCount(), x);
ManagedProperty currentThreadCpuTimeSupported = mo.getProperty("currentThreadCpuTimeSupported");
assertNotNull(currentThreadCpuTimeSupported);
boolean flag = (Boolean) metaValueFactory.unwrap(currentThreadCpuTimeSupported.getValue());
assertEquals(mbean.isCurrentThreadCpuTimeSupported(), flag);
ManagedProperty threadContentionMonitoringEnabled = mo.getProperty("threadContentionMonitoringEnabled");
assertNotNull(threadContentionMonitoringEnabled);
flag = (Boolean) metaValueFactory.unwrap(threadContentionMonitoringEnabled.getValue());
assertEquals(mbean.isThreadContentionMonitoringEnabled(), flag);
ManagedProperty threadContentionMonitoringSupported = mo.getProperty("threadContentionMonitoringSupported");
assertNotNull(threadContentionMonitoringSupported);
flag = (Boolean) metaValueFactory.unwrap(threadContentionMonitoringSupported.getValue());
assertEquals(mbean.isThreadContentionMonitoringSupported(), flag);
ManagedProperty threadCpuTimeEnabled = mo.getProperty("threadCpuTimeEnabled");
assertNotNull(threadCpuTimeEnabled);
flag = (Boolean) metaValueFactory.unwrap(threadCpuTimeEnabled.getValue());
assertEquals(mbean.isThreadCpuTimeEnabled(), flag);
ManagedProperty threadCpuTimeSupported = mo.getProperty("threadCpuTimeSupported");
assertNotNull(threadCpuTimeSupported);
flag = (Boolean) metaValueFactory.unwrap(threadCpuTimeSupported.getValue());
assertEquals(mbean.isThreadCpuTimeSupported(), flag);
// Ops
Set<ManagedOperation> ops = mo.getOperations();
log.debug(ops);
String javaSpecVersion = System.getProperty("java.specification.version");
if (javaSpecVersion.equals("1.5") || javaSpecVersion.equals("5.0"))
assertEquals(mo + " has wrong number of ManagedOperations.", 8, ops.size());
else if (javaSpecVersion.equals("1.6") || javaSpecVersion.equals("6.0"))
assertEquals(mo + " has wrong number of ManagedOperations.", 11, ops.size());
ManagedOperation getThreadInfo = ManagedOperationMatcher.findOperation(ops,
"getThreadInfo", SimpleMetaType.LONG_PRIMITIVE, SimpleMetaType.INTEGER_PRIMITIVE);
assertNotNull("getThreadInfo", getThreadInfo);
log.debug(getThreadInfo);
String[] getThreadInfoSig = getThreadInfo.getReflectionSignature();
String[] getThreadInfoSigExpected = {"long", "int"};
assertEquals(Arrays.asList(getThreadInfoSigExpected), Arrays.asList(getThreadInfoSig));
ManagedOperation resetPeakThreadCount = ManagedOperationMatcher.findOperation(ops,
"resetPeakThreadCount");
assertNotNull("resetPeakThreadCount", resetPeakThreadCount);
assertEquals(0, resetPeakThreadCount.getReflectionSignature().length);
MBeanServer server = ManagementFactory.getPlatformMBeanServer();
ObjectName tname = new ObjectName(ManagementFactory.THREAD_MXBEAN_NAME);
MBeanInfo tinfo = server.getMBeanInfo(tname);
MBeanOperationInfo[] tops = tinfo.getOperations();
if (javaSpecVersion.equals("1.5") || javaSpecVersion.equals("5.0"))
assertEquals(8, tops.length);
else if (javaSpecVersion.equals("1.6") || javaSpecVersion.equals("6.0"))
assertEquals(11, tops.length);
for(MBeanOperationInfo op : tops)
{
MBeanParameterInfo[] params = op.getSignature();
String sig = "";
if(params != null)
{
for(MBeanParameterInfo param : params)
{
if(sig.length() > 0)
sig += ",";
sig += param.getType();
}
}
log.debug(op.getName()+"("+sig+")");
}
Object[] params = {threadID};
String[] signature = {"long"};
Object result = server.invoke(tname, "getThreadInfo", params, signature);
threadInfo = mbean.getThreadInfo(threadID);
log.debug("getThreadInfo()-OpenType: "+result);
assertTrue(result instanceof CompositeDataSupport);
MetaValue resultMV = metaValueFactory.create(result);
assertTrue(resultMV instanceof CompositeValue);
CompositeValue resultCV = (CompositeValue) resultMV;
log.debug("getThreadInfo()-MetaType: "+resultCV);
ThreadInfo resultTI = ManagementFactoryUtils.unwrapThreadInfo(resultCV);
threadInfo2 = resultTI;
assertEquals(threadInfo.getLockOwnerName(), threadInfo2.getLockOwnerName());
assertEquals(threadInfo.getThreadName(), threadInfo2.getThreadName());
assertEquals(threadInfo.isInNative(), threadInfo2.isInNative());
assertEquals(threadInfo.getBlockedCount(), threadInfo2.getBlockedCount());