简体   繁体   English

Spring JMX Bean在JConsole中不可见

[英]Spring JMX Bean Not Visible in JConsole

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. 我已经建立了一个基本示例,将POJO公开为JMX bean,并希望在JConsole中查看它。 I'm following the Spring Docs so not sure why this does not work. 我正在关注Spring Docs,所以不确定为什么它不起作用。

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. 根据对类似问题的回答,我尝试了<context:mbean-server/><context:mbean-export/>但这些都无法解决。

Is it related to the code in my main method? 它与我的main方法中的代码有关吗? I've tried with and without the context.getBean(...) ... 我尝试了有和没有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. 编辑 Spring日志显示INFO: Located managed bean 'bean:name=testBean1': registering with JMX server as MBean [bean:name=testBean1]我可以在JConsole中连接到该进程,但MBean不会显示。

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] Edit#2启用日志记录后,我可以看到MBeanExporter:651 - Located managed bean 'bean:name=testBean1': registering with JMX server as MBean [bean:name=testBean1]

The solution is to add a line to the server bean 解决方案是在服务器bean中添加一行

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM