Spring JMX Mbean not appearing or being registered with jconsole

ashley picture ashley · Mar 14, 2013 · Viewed 7.3k times · Source

Just trying out a simple Spring JMX application within eclipse on a local tomcat server but can't seem to register the mbeans so they then become available to view within jconsole, within eclipse context:component-scan does appear to pick up beans i have created however these are not registered. When programmatically registering mbeans it works.

Here is my config xml file.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">

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

<context:component-scan base-package="com.jmx.beans" />
<context:mbean-export server="mbeanServer" />

</beans>

The simple bean i'm trying to register with annotations

package com.jmx.beans;

import org.springframework.jmx.export.annotation.ManagedAttribute;
import org.springframework.jmx.export.annotation.ManagedResource;
import org.springframework.stereotype.Component;

@Component
@ManagedResource(objectName="bean:name=Hello")
public class Hello{

String message =  null;

@ManagedAttribute(description="get the message")
public String getMessage(){

    return this.message;
}

@ManagedAttribute(description="set the message")
public void setMessage(String Message){

    this.message = Message;

}
}

I have also set the tomcat server arguements as follows

-Dcom.sun.management.jmxremote 
-Dcom.sun.management.jmxremote.port=9990
-Dcom.sun.management.jmxremote.ssl=false 
-Dcom.sun.management.jmxremote.authenticate=false
-Dcom.sun.management.jmxremote.hostname="localhost"

Any Help on this would be much appreciated, thanks

Answer

Gary Russell picture Gary Russell · Mar 14, 2013

Why did you edit your post to remove the <context:component-scan/>? That is required to find your @Component.

I just tested and all worked fine for me...

@Component
@ManagedResource
public class Foo {

    @ManagedAttribute
    public int getIt() {
        return 42;
    }
}

and

<context:mbean-server/>

<context:component-scan base-package="foo" />

<context:mbean-export/>

I tried it with your style of MBean server and that worked too.