Exporting Spring Beans as JMX Mbeans

If you always want to stay on top of your production system and always want to know whats going on in your APP, you would want to configure JMX for your application.

First you need to start your JVM with the following parameters to enable any JMX client to connect to your system.

-Dcom.sun.management.jmxremote.port=8888 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false

This is just done without any security but in production, you would want to add authentication.

Now, add these configuration for your spring APP to automatically expose your Spring Beans as MBeans if they have the right annotations.

	<bean id="exporter" class="org.springframework.jmx.export.MBeanExporter"
		<property name="autodetect" value="true"></property>
		<property name="namingStrategy" ref="namingStrategy"></property>
		<property name="assembler" ref="assembler"></property>		
		<property name="server" ref="mbeanServer"></property>		

	<bean id="assembler"
		<property name="attributeSource" ref="attributeSource" />

	<bean id="namingStrategy"
		<property name="attributeSource" ref="attributeSource" />

	<bean id="mbeanServer" class="java.lang.management.ManagementFactory" factory-method="getPlatformMBeanServer"/>	

The ‘exporter’ will inspect all your defined Spring Beans in your configuration and see if the have Spring JMX annotations. Now you need to define your beans with Annotations.

@ManagedResource(objectName = "yourapp:name=log4jLevelChanger", description = "LOG4j Level Changer")
public class Log4jLevelChanger  {
	private static final Log log = LogFactory.getLog(Log4jLevelChanger.class);
	public void init() throws AttributeNotFoundException, InstanceNotFoundException, MBeanException, ReflectionException {
		ArrayList<MBeanServer> servers = MBeanServerFactory.findMBeanServer(null);
		for(MBeanServer server : servers) {
			String name = (String) server.getAttribute(MBeanServerDelegate.DELEGATE_NAME,
			log.info("found  server: " + name);
	 * (non-Javadoc)
	 * @see
	 * com.vantage.callcenter.web.utils.Log4jLevelChanger#setLogLevel(java.lang
	 * .String, java.lang.String)
	@ManagedOperationParameter(description="Name of the Logger to configure", name="loggerName"),
	@ManagedOperationParameter(description="Level ofthe Logger to set", name="level")
	public void setLogLevel(
			String loggerName, 
			String level) {
		if ("debug".equalsIgnoreCase(level)) {
		} else if ("info".equalsIgnoreCase(level)) {
		} else if ("error".equalsIgnoreCase(level)) {
		} else if ("fatal".equalsIgnoreCase(level)) {
		} else if ("warn".equalsIgnoreCase(level)) {

        //Read the log4j.properties to reconfigure the logging levels.
	public boolean refreshLogging() {
		try {
			return true;
		} catch (FileNotFoundException e) {
			return false;
	@ManagedOperationParameters ({
		@ManagedOperationParameter(description="Name of the Logger to get the Level", name="loggerName")	
	public String getLogLevel(
			String loggerName) {
		log.debug("getLogLevel -> loggerName: " + loggerName);
		Logger logger = Logger.getLogger(loggerName);
		if(logger != null) {
			if(logger.getLevel() != null) {
				return logger.getLevel().toString();
		return Logger.getRootLogger().getLevel().toString();

and define it in your application-context.xml as a regular bean

<bean id="log4jLevelChanger"  class="com.test.Log4jLevelChanger"></bean>

Thats it! Now you can open any Standard JMX client (JConsole / VisualVM / JManage) to inspect the bean and changing log levels!

You can also add Mbean Annotations to any regular Bean that you wish to inspect at runtime. For example, you can add it to the bean that manages your threadpool to see its utilization at runtime, you can add it to any in memory cache to find out memory leaks. If you think closely, the possibilities can be endless!

3 thoughts on “Exporting Spring Beans as JMX Mbeans

  1. Is there a way to avoid the -D JVM parameters and programmatically set up a MBeanServer from within Spring, that can be accessed from JMX clients or jconsole?

    • -D JVM parameter does not create the MBeanServer. It is for allowing the JMX clients (jconsole/visualvm etc) to connect to your JVM. If you don’t want to use any JMX client and just export your mbeans from your app (may be with web interface), you don’t need to use the -D JVM parameter.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s