Spring JMX Bean在JConsole中不可见

时间:2014-11-25 17:27:42

标签: java spring spring-jmx

我知道这里已多次询问过,但我无法解决我的问题。我已经设置了一个基本示例,将POJO公开为JMX bean,并希望在JConsole中查看它。我跟随Spring Docs,所以不确定为什么这不起作用。

我的代码是

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();
}

package org.springframework.jmx;

public class JmxTestBean implements IJmxTestBean {

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

    //getters and setters for each
}

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);
    }
}

<?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>

根据我对<context:mbean-server/><context:mbean-export/>类似问题的回答,但这些问题没有解决。

它与我的main方法中的代码有关吗?我曾尝试使用和不使用context.getBean(...) ...

编辑 Spring日志记录显示INFO: Located managed bean 'bean:name=testBean1': registering with JMX server as MBean [bean:name=testBean1]我可以连接到JConsole中的进程,但MBean没有显示。

编辑#2 启用日志记录后,我可以看到MBeanExporter:651 - Located managed bean 'bean:name=testBean1': registering with JMX server as MBean [bean:name=testBean1]

1 个答案:

答案 0 :(得分:2)

解决方案是在服务器bean中添加一行

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