Spring JMX Bean Not Visible in JConsole

2.7k views Asked by At

I know this has been asked here many times, but I couldn't get a solution to my issue. I have setup a basic example to expose a POJO as a JMX bean and wish to view it in JConsole. I'm following the Spring Docs so not sure why this does not work.

My code is

package org.springframework.jmx;

public interface IJmxTestBean {
    public int getAge();
    public void setAge(int age);
    public void setName(String name);
    public String getName();
    public int add(int x, int y);
    public void dontExposeMe();
}

and

package org.springframework.jmx;

public class JmxTestBean implements IJmxTestBean {

    private String name;
    private int age;
    private boolean isSuperman;

    //getters and setters for each
}

and

package org.springframework.jmx;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Program {
    public static void main(String[] args) throws InterruptedException {
        ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
        context.getBean(JmxTestBean.class);
        Thread.sleep(Long.MAX_VALUE);
    }
}

and

<?xml version="1.0" encoding="UTF-8"?>
<beans>

   <bean id="mbeanServer" class="org.springframework.jmx.support.MBeanServerFactoryBean"/>

   <bean id="exporter" class="org.springframework.jmx.export.MBeanExporter" lazy-init="false">
          <property name="beans">
                 <map>
                        <entry key="bean:name=testBean1" value-ref="testBean"/>
                 </map>
          </property>
          <property name="server" ref="mbeanServer"/>
          <property name="autodetect" value="true"/>
   </bean>

   <bean id="testBean" class="org.springframework.jmx.JmxTestBean">
          <property name="name" value="TEST"/>
          <property name="age" value="100"/>
   </bean>

</beans>

as per answer to similar questions i've tried <context:mbean-server/> and <context:mbean-export/> but these did not resolve.

Is it related to the code in my main method? I've tried with and without the context.getBean(...)...

Edit The Spring logging says INFO: Located managed bean 'bean:name=testBean1': registering with JMX server as MBean [bean:name=testBean1] and I can connect to the process in JConsole except the MBean does not show.

Edit#2 After enabling logging I can see MBeanExporter:651 - Located managed bean 'bean:name=testBean1': registering with JMX server as MBean [bean:name=testBean1]

1

There are 1 answers

0
clicky On BEST ANSWER

The solution is to add a line to the server bean

   <bean id="mbeanServer" class="org.springframework.jmx.support.MBeanServerFactoryBean">
          <property name="locateExistingServerIfPossible" value="true" />
   </bean>