Delicious MBeans
Who loves MBeans? I do! I do!
Well, when used correctly at least, MBeans and JMX can lead to more manageable Java applications. Used incrrectly, and you can end up with JBoss-specific dynamic MBeans, monitoring applications that screen scrape JBoss JMX consoles, and applications that expose the wrong things to unwitting administrators.
Now for the bright side of life. In application servers like JBoss, you get JMX "for free". As of JDK 5, you can have an MBean server and remote JMX on any JVM by throwing a few extra -D's into the startup like so:
-Dcom.sun.management.jmxremote \
-Dcom.sun.management.jmxremote.port=5001 \
-Dcom.sun.management.jmxremote.ssl=false \
-Dcom.sun.management.jmxremote.authenticate=false
Now attach JConsole to the following URL:
service:jmx:rmi:///jndi/rmi://localhost:5001/jmxrmi
If you are lucky enough to be Spring-enabled, you have another option for enabling remote JMX connectivity.
<!-- JMX: app server provided or platform MBean server -->
<context:mbean-server />
<!-- JMX: expose a remote JMX connector -->
<bean id="mbeanServerConnector" depends-on="rmiRegistry" class="org.springframework.jmx.support.ConnectorServerFactoryBean">
<property name="serviceUrl" value="service:jmx:rmi:///jndi/rmi://localhost:5001/jmxrmi" />
<property name="threaded" value="true" />
<property name="daemon" value="true" />
</bean>
<!-- RMI registry -->
<bean id="rmiRegistry" class="org.springframework.remoting.rmi.RmiRegistryFactoryBean">
<property name="port" value="1099" />
</bean>
Check out the Spring docs on JMX for more goodies, like exposing POJOs as MBeans, and using alternative remote JMX connectors (no, you don't need to use RMI).
August 20th, 2009 - 10:02
Saved my day!!