| |
"Building Manageability"
Vol. 9, Issue 11, p. 44
Listing 1: Connecting using MXBean Proxy
MBeanServerConnection mBeanServerConnection = null;
try{
mBeanServerConnection =
JMXConnectorFactory.connect(
new JMXServiceURL(jmxServiceURL),null)
.getMBeanServerConnection();
MemoryMXBean memoryMXBean =
ManagementFactory.newPlatformMXBeanProxy(
mBeanServerConnection,
ManagementFactory.MEMORY_MXBEAN_NAME,
MemoryMXBean.class);
} catch (Exception e) {
System.err.println("Failed to Connect with "
+ "JMXServiceURL '" + jmxServiceURL +
"': " + e);
}
Listing 2: Connecting directly to the MBeanServer
// get an instance of the logging MXBean
ObjectInstance loggingInstance =
mBeanServerConnection.getObjectInstance(
new ObjectName("java.util.logging:type=Logging"));
// Use the query mechanism to get all the MXBeans that are
//part of the java.lang domain.
Set mBeanSet =
mBeanServerConnection.queryMBeans(
new ObjectName("java.lang:*"), null);
Listing 3: Detecting deadlocked threads
// check if threads are deadlocked
long[] deadlockedThreadIds =threadMxBean.
findMonitorDeadlockedThreads();
if (deadlockedThreadIds != null){
for( long element:deadlockedThreadIds ){
//for each threaded get the ThreadInfo
ThreadInfo tinfo =threadMxBean.getThreadInfo(element);
// print information about the blocked thread
System.out.println(" Blocked Thread Id is " +
tinfo.getThreadId();
System.out.println("Thread ID of blocking thread is " +
tinfo.getLockOwnerId();
}
}
Listing 4: Using the logging MXBean
LoggingMXBean logger =
ManagementFactory.newPlatformMXBeanProxy(
mBeanServerConnection,
LogManager.LOGGING_MXBEAN_NAME,
LoggingMXBean.class);
try{
if (! logger.getLoggerLevel(loggerName).
equals(Level.INFO.getName()))
{
logger.setLoggerLevel(loggerName,
Level.INFO.getName());
}
} catch( IllegalArgumentException e){
System.err.println("Logger does not exist " +
loggerName);
}
Listing 5: Creating the ObjectPool class
public class ObjectPool implements ObjectPoolMBean {
public ObjectPool () {
}
public Integer availableObjects() {
return (pool.getTotalCapacity -
pool.usedCapacity());
}
public Integer availableObjects() {
return pool.getTotalCapacity();
}
public String poolName() {
return pool.name();
}
}
Listing 6: Creating the LowMemory Listener
class LowMemoryListener implements NotificationListener {
public void handleNotification(
Notification notification,
Object handback) {
String notifyType = notification.getType();
if (notifyType.equals
(MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED)) {
// potential low memory condition
// save the pending requests for processing later
saveRequests( requestQueue.pending());
// pause receiving new requests
requestQueue.pauseNewRequests();
}
}
}
Listing 7: Using a polling-based mechanism to empty saved request queue
while (true) {
if (!pool.isUsageThresholdExceeded()) {
if( !saveRequestQueue.isEmpty()) {
// start restoring requests one by one
restoreRequests(saveRequestQueue.getRequest());
}else {
//no more saved requests left
// start receiving new requests
requestQueue.startNewRequests();
}
}
try {
Thread.sleep(SLEEP_INTERVAL);
} catch (InterruptedException e) {
}
}
|
|