JMX

Throw standard java exceptions vs. custom exceptions in custom jmx mbeans. If custom exceptions are thrown and they were not included on classpath when starting jconsole, a classNotFoundException will be thrown and the exception message will not be displayed.

Java 6 Enhancements

HOWTO

Object naming

MBean interface names must end with MBean, and the MBean class for the FooMBean interface must be called Foo. (You can lift this restriction by using a more advanced JMX feature, dynamic MBeans.)

public interface FooMBean

public class Foo

Create and register MBean instance:

Foo foo = new Foo(...);
MBeanServer server = ManagementFactory.getPlatformMBeanServer();
Alternatively for JBoss: MBeanServer server = MBeanServerLocator.locateJBoss();
server.registerMBean((FooMBean)foo, new ObjectName("myapp:type=foo,name=uniqueString"));

Alternatively:

server.registerMBean(foo, new ObjectName("topLevelNodeName:subNodeName=foo,subNode2Name=bar,mbeanName=uniqueNodeNameString"));
In JMX tree:
topLevelNodeName
 |
 +-foo
    |
    +-bar
       |
       +-uniqueNodeNameString

The name value pairs need to uniquely identify the MBean instance within the JVM.


Notification Events



Memory monitoring via JMX:

See java.lang.management.MemoryPoolMXBean and related classes


List memBeans = ManagementFactory.getMemoryPoolMXBeans();
for (Iterator i = memBeans.iterator(); i.hasNext(); ) {
MemoryPoolMXBean mpool = (MemoryPoolMXBean)i.next();
MemoryUsage usage = mpool.getUsage();

String name = mpool.getName();
float init = usage.getInit()/1000;
float used = usage.getUsed()/1000;
float committed = usage.getCommitted()/1000;
float max = usage.getMax()/1000;
float pctUsed = (used / max)*100;
float pctCommitted = (committed / max)*100;
}


Programmatically connecting to JMX:

JMXServiceURL u = new JMXServiceURL(
"service:jmx:rmi:///jndi/rmi:// “ + hostName + ":" + portNum + "/jmxrmi");
JMXConnector c = JMXConnectorFactory.connect(u);

Comments

Popular posts from this blog

Sites, Newsletters, and Blogs

Oracle JDBC ReadTimeout QueryTimeout