下面列出了怎么用java.util.logging.LoggingMXBean的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Returns an MXBean proxy for the logging system of the JVM.
*/
public synchronized LoggingMXBean getLoggingMXBean() {
if (mbsc != null && loggingMXBean == null) {
loggingMXBean = getMXBean(LogManager.LOGGING_MXBEAN_NAME, LoggingMXBean.class);
}
return loggingMXBean;
}
private void getStatsViaProxy() {
RuntimeMXBean runtimeBean = null;
OperatingSystemMXBean osBean = null;
LoggingMXBean logBean = null;
ClassLoadingMXBean classBean = null;
CompilationMXBean compBean = null;
// Get the proxies for the runtime, os, log and class MXBeans
try {
runtimeBean = ManagementFactory.newPlatformMXBeanProxy(this.mbs, ManagementFactory.RUNTIME_MXBEAN_NAME,
RuntimeMXBean.class);
osBean = ManagementFactory.newPlatformMXBeanProxy(this.mbs, ManagementFactory.OPERATING_SYSTEM_MXBEAN_NAME,
OperatingSystemMXBean.class);
logBean = ManagementFactory.newPlatformMXBeanProxy(this.mbs, LogManager.LOGGING_MXBEAN_NAME,
LoggingMXBean.class);
classBean = ManagementFactory.newPlatformMXBeanProxy(this.mbs, ManagementFactory.CLASS_LOADING_MXBEAN_NAME,
ClassLoadingMXBean.class);
// Check the compiler is being used and get the compilation MXBean
Map<String, String> props = runtimeBean.getSystemProperties();
String sys_comp = props.get("java.compiler");
if ((sys_comp != null) && (!(sys_comp.equals("")))) {
compBean = ManagementFactory.newPlatformMXBeanProxy(this.mbs, ManagementFactory.COMPILATION_MXBEAN_NAME,
CompilationMXBean.class);
}
} catch (IOException ioe) {
Message.logOut("A communication problem occurred when accessing the MBeanServerConnection");
ioe.printStackTrace();
Assert.fail("A communication problem occurred when accessing the MBeanServerConnection");
}
try {
Message.logOut("Starting to write data");
// Record the environment data in the log
this.envData.writeData(runtimeBean, osBean, logBean, false);
// Record the number of class loaded over time in a csv and record
// the class data in the log every 10 seconds
int secondsCnt = 0;
int writesCnt = 0;
while (writesCnt < 30) {
// Write out the threadData every 10 seconds
if (secondsCnt == 10) {
System.out.print(".");
this.classData.writeData(classBean, compBean, runtimeBean, true);
secondsCnt = 0;
writesCnt++;
}
this.recordClassStats(runtimeBean, classBean);
Thread.sleep(1000);
secondsCnt++;
}
} catch (InterruptedException ie) {
Message.logOut("The sleeping profiler was interrupted");
ie.printStackTrace();
Assert.fail("The sleeping profiler was interrupted");
} catch (UndeclaredThrowableException ue) {
Throwable cause = ue.getCause();
Class<ConnectException> connectExcept = ConnectException.class;
Class<UnmarshalException> unmarshalExcept = UnmarshalException.class;
if (connectExcept.isInstance(cause) || unmarshalExcept.isInstance(cause)) {
// If the exception was caused by a Connect or Unmarshal
// Exception, assume the monitored JVM has finished.
this.closeCSVFile();
Message.logOut("Exiting as JVM we are connected to has finished");
Assert.fail("Exiting as JVM we are connected to has finished");
} else {
Message.logOut(ue.getMessage());
ue.printStackTrace();
Assert.fail(ue.getMessage());
}
} finally {
this.closeCSVFile();
}
}
private void getStatsViaProxy() {
RuntimeMXBean runtimeBean = null;
OperatingSystemMXBean osBean = null;
LoggingMXBean logBean = null;
MemoryMXBean memoryBean = null;
List<MemoryPoolMXBean> memPoolBeans = new ArrayList<MemoryPoolMXBean>();
List<MemoryManagerMXBean> memMgrBeans = new ArrayList<MemoryManagerMXBean>();
List<GarbageCollectorMXBean> gcBeans = new ArrayList<GarbageCollectorMXBean>();
// Get the proxies for the runtime, os, log, memory MXBeans
try {
runtimeBean = ManagementFactory.newPlatformMXBeanProxy(this.mbs, ManagementFactory.RUNTIME_MXBEAN_NAME,
RuntimeMXBean.class);
osBean = ManagementFactory.newPlatformMXBeanProxy(this.mbs, ManagementFactory.OPERATING_SYSTEM_MXBEAN_NAME,
OperatingSystemMXBean.class);
logBean = ManagementFactory.newPlatformMXBeanProxy(this.mbs, LogManager.LOGGING_MXBEAN_NAME,
LoggingMXBean.class);
memoryBean = ManagementFactory.newPlatformMXBeanProxy(this.mbs, ManagementFactory.MEMORY_MXBEAN_NAME,
MemoryMXBean.class);
Set<?> memPoolNames = this.mbs.queryNames(new ObjectName(ManagementFactory.MEMORY_POOL_MXBEAN_DOMAIN_TYPE + ",*"), null);
// Get a MXBean Proxy for each of the names returned, use the proxy to access the MXBeanS
for (Object memPoolName : memPoolNames){
ObjectName memPool = (ObjectName) memPoolName;
MemoryPoolMXBean memPoolBean = ManagementFactory.newPlatformMXBeanProxy(this.mbs, memPool.toString(),
MemoryPoolMXBean.class);
memPoolBeans.add(memPoolBean);
}
Set<?> memMgrNames = this.mbs.queryNames(new ObjectName(ManagementFactory.MEMORY_MANAGER_MXBEAN_DOMAIN_TYPE + ",*"), null);
// Get a MXBean Proxy for each of the names returned, use the proxy to access the MXBeanS
for (Object memMgrName : memMgrNames){
ObjectName memMgr = (ObjectName) memMgrName;
MemoryManagerMXBean memMgrBean = ManagementFactory.newPlatformMXBeanProxy(this.mbs, memMgr.toString(),
MemoryManagerMXBean.class);
memMgrBeans.add(memMgrBean);
}
Set<?> gcNames = this.mbs.queryNames(new ObjectName(ManagementFactory.GARBAGE_COLLECTOR_MXBEAN_DOMAIN_TYPE + ",*"), null);
// Get a MXBean Proxy for each of the names returned, use the proxy to access the MXBeanS
for (Object gcName : gcNames){
ObjectName gc = (ObjectName) gcName;
GarbageCollectorMXBean gcBean = ManagementFactory.newPlatformMXBeanProxy(this.mbs, gc.toString(),
GarbageCollectorMXBean.class);
gcBeans.add(gcBean);
}
} catch (IOException ioe) {
ioe.printStackTrace();
Assert.fail("A communication problem occurred when accessing the MBeanServerConnection");
} catch (MalformedObjectNameException mone) {
mone.printStackTrace();
Assert.fail("Problem with creating the ObjectName for a MXBean");
}
try {
Message.logOut("Starting to write data");
this.envData.writeData (runtimeBean, osBean, logBean, false);
int secondsCnt = 0;
int writesCnt = 0;
while (writesCnt < 30) {
// Write out the methodData every 10 seconds
if (secondsCnt == 10) {
System.out.print(".");
this.memoryData.writeData(memoryBean, memMgrBeans, memPoolBeans, gcBeans, true);
secondsCnt = 0;
writesCnt++;
}
this.recordMemoryStats(runtimeBean, memoryBean, writesCnt);
Thread.sleep(1000);
secondsCnt++;
}
} catch (InterruptedException ie) {
ie.printStackTrace();
Assert.fail("The sleeping profiler was interrupted");
} catch (UndeclaredThrowableException ue) {
// If the exception was caused by a Connect or Unmarshal Exception, assume the
// monitored JVM has finished.
Throwable cause = ue.getCause();
Class<ConnectException> connectExcept = ConnectException.class;
Class<UnmarshalException> unmarshalExcept = UnmarshalException.class;
if (connectExcept.isInstance(cause) || unmarshalExcept.isInstance(cause)) {
this.closeCSVFile();
Message.logOut("Exiting as JVM we are connected to has finished");
}
else {
Message.logOut("Rethrowing UndeclaredThrowableException");
}
Assert.fail();
} finally {
this.closeCSVFile();
}
}
private void getStatsViaProxy() {
RuntimeMXBean runtimeBean = null;
OperatingSystemMXBean osBean = null;
LoggingMXBean logBean = null;
ThreadMXBean threadBean = null;
int stackDepth = 8;
// Get the runtime, os, log and thread MXBeans
try {
runtimeBean = ManagementFactory.newPlatformMXBeanProxy(this.mbs,
ManagementFactory.RUNTIME_MXBEAN_NAME, RuntimeMXBean.class);
osBean = ManagementFactory.newPlatformMXBeanProxy(this.mbs,
ManagementFactory.OPERATING_SYSTEM_MXBEAN_NAME, OperatingSystemMXBean.class);
logBean = ManagementFactory.newPlatformMXBeanProxy(this.mbs,
LogManager.LOGGING_MXBEAN_NAME, LoggingMXBean.class);
threadBean = ManagementFactory.newPlatformMXBeanProxy(this.mbs,
ManagementFactory.THREAD_MXBEAN_NAME, ThreadMXBean.class);
} catch (IOException ioe) {
Message.logOut("A communication problem occurred when accessing the MBeanServerConnection");
ioe.printStackTrace();
Assert.fail("A communication problem occurred when accessing the MBeanServerConnection");
}
try {
Message.logOut("Starting to write data");
this.envData.writeData(runtimeBean, osBean, logBean, false);
int secondsCnt = 0;
int writesCnt = 0;
while (writesCnt < 30) {
// Write out the threadData 10 seconds
if (secondsCnt == 10) {
System.out.print(".");
this.threadData.writeData(threadBean, stackDepth, true);
secondsCnt = 0;
writesCnt++;
}
this.recordThreadStats(runtimeBean, threadBean);
Thread.sleep(1000);
secondsCnt++;
}
} catch (InterruptedException ie) {
Message.logOut("The sleeping profiler was interrupted");
ie.printStackTrace();
Assert.fail("The sleeping profiler was interrupted");
} catch (UndeclaredThrowableException ue) {
// If the exception was caused by a Connect or Unmarshal Exception
// assume the monitored JVM has finished.
Throwable cause = ue.getCause();
Class<ConnectException> connectExcept = ConnectException.class;
Class<UnmarshalException> unmarshalExcept = UnmarshalException.class;
if (connectExcept.isInstance(cause) || unmarshalExcept.isInstance(cause)) {
this.closeCSVFile();
Message.logOut("Exiting as JVM we are connected to has finished");
Assert.fail("Exiting as JVM we are connected to has finished");
} else {
Message.logOut(ue.getMessage());
ue.printStackTrace();
Assert.fail(ue.getMessage());
}
} finally {
this.closeCSVFile();
}
}
/**
* Returns <tt>LoggingMXBean</tt> for managing loggers.
*
* @return a {@link LoggingMXBean} object for
* the Java virtual machine.
*/
public LoggingMXBean getLoggingMXBean();