Java 无法将 JMX 与 Spring 应用程序集成

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22789428/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 17:53:03  来源:igfitidea点击:

Cannot integrate JMX with Spring application

javaspringjmx

提问by ruhungry

I have got a SPRINGapplication. When I run

我有一个SPRING申请。当我跑

mvn jetty:run

everything is ok.

一切都好。

I would like to use JMXin my project.

我想在我的项目中使用JMX

I created another project, I tried tutorial for beginners and I was able to see some changes with jconsole.

我创建了另一个项目,我尝试了初学者教程,我能够看到jconsole 的一些变化。

Now, I want to use JMX in my real project and I would like to use SPRING libraries which manages JMX - following this post

现在,我想在我的真实项目中使用 JMX,并且我想使用管理 JMX 的 SPRING 库 - 按照这篇文章

How to integrate JMX with Spring?

如何将 JMX 与 Spring 集成?

I have got a class:

我有一堂课:

public class MyMainClass {
  private int var1;
  private int var2;
  private TimeUnit var3;

  // public getters and setters
  public static MyXXXClass<String, Object> getInstance();
}

and in config.xml

并在config.xml

<!-- other beans -->
<bean id="myid" class="com.my.package.MyMainClass">
    <property name="var1" value.../>
    <property name="var2" value... />
    <property name="var3" value.../>
</bean>
<!-- other beans -->

I changed few things to make it works with JMX.

我改变了一些东西以使其与 JMX 一起工作。

I added an interface:

我添加了一个接口:

import java.util.concurrent.TimeUnit;

public interface IMyMainClassBean {

    public int getVar1();

    public void setVar1(int var1);

    public int getVar2();

    public void setVar2(int var2);

    public TimeUnit getVar3();

    public void setVar3(TimeUnit var3);
}

I added implementsto my class:

我在班级中添加了工具

public class MyMainClassBean implements IMyMainClassBean {...}

Last thing, I edited my xml file:

最后一件事,我编辑了我的 xml 文件:

<!-- other beans -->
<bean id="myid" class="com.my.package.MyMainClassBean">
    <property name="var1" value.../>
    <property name="var2" value... />
    <property name="var3" value.../>
</bean>

<!-- this bean must not be lazily initialized if the exporting is to happen -->
<bean id="exporter" class="org.springframework.jmx.export.MBeanExporter"
    lazy-init="false">
    <property name="beans">
        <map>
            <entry key="bean:name=testBean1" value-ref="myid" />
        </map>
    </property>
</bean>
<!-- other beans -->

Now, when I start my server, it gives me a lot of exceptions (log is really long, so I copied just a part which I think is the most important).

现在,当我启动我的服务器时,它给了我很多异常(日志真的很长,所以我只复制了我认为最重要的部分)。

Caused by: org.springframework.jmx.export.UnableToRegisterMBeanException: Unable to register MBean [com.my.package
.MyMainClassBean@3d4395fb] with key 'bean:name=testBean1'; nested exception is javax.management.InstanceAlreadyExistsExcep
tion: bean:name=testBean1
        at org.springframework.jmx.export.MBeanExporter.registerBeanNameOrInstance(MBeanExporter.java:602)
        at org.springframework.jmx.export.MBeanExporter.registerBeans(MBeanExporter.java:527)
        at org.springframework.jmx.export.MBeanExporter.afterPropertiesSet(MBeanExporter.java:413)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableB
eanFactory.java:1571)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBean
Factory.java:1509)
        ... 163 more
Caused by: javax.management.InstanceAlreadyExistsException: bean:name=testBean1
        at com.sun.jmx.mbeanserver.Repository.addMBean(Repository.java:453)
        at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.internal_addObject(DefaultMBeanServerInterceptor.java:1484)
        at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.registerDynamicMBean(DefaultMBeanServerInterceptor.java:963)
        at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.registerObject(DefaultMBeanServerInterceptor.java:917)
        at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.registerMBean(DefaultMBeanServerInterceptor.java:312)
        at com.sun.jmx.mbeanserver.JmxMBeanServer.registerMBean(JmxMBeanServer.java:483)
        at org.springframework.jmx.support.MBeanRegistrationSupport.doRegister(MBeanRegistrationSupport.java:195)
        at org.springframework.jmx.export.MBeanExporter.registerBeanInstance(MBeanExporter.java:655)
        at org.springframework.jmx.export.MBeanExporter.registerBeanNameOrInstance(MBeanExporter.java:592)
        ... 167 more
[WARNING] Nested in org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myTask' defined
 in class path resource [anotherconfigfile.xml]: Cannot resolve reference to bean 'anotherimport' while setting bean property 'targetObj
ect'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'anotherimport' defin
ed in class path resource [anotherconfigfile.xml]: Cannot create inner bean 'myTotallyAnotherClass' of type [com.my.package.another.MyTotallyAnotherClass]
 while setting bean property 'myTotallyAnotherClass'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creatin
g bean with name 'myTotallyAnotherClass' defined in class path resource [anotherconfigfile.xml]: Instantiation of bean failed; nested exception is
 org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.my.package.another.MyTotallyAnotherClass]: Co
nstructor threw exception; nested exception is java.lang.ExceptionInInitializerError:
javax.management.InstanceAlreadyExistsException: bean:name=testBean1
        at com.sun.jmx.mbeanserver.Repository.addMBean(Repository.java:453)
        at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.internal_addObject(DefaultMBeanServerInterceptor.java:1484)
        at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.registerDynamicMBean(DefaultMBeanServerInterceptor.java:963)
        at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.registerObject(DefaultMBeanServerInterceptor.java:917)
        at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.registerMBean(DefaultMBeanServerInterceptor.java:312)
        at com.sun.jmx.mbeanserver.JmxMBeanServer.registerMBean(JmxMBeanServer.java:483)
        at org.springframework.jmx.support.MBeanRegistrationSupport.doRegister(MBeanRegistrationSupport.java:195)
        at org.springframework.jmx.export.MBeanExporter.registerBeanInstance(MBeanExporter.java:655)
        at org.springframework.jmx.export.MBeanExporter.registerBeanNameOrInstance(MBeanExporter.java:592)
        at org.springframework.jmx.export.MBeanExporter.registerBeans(MBeanExporter.java:527)
        at org.springframework.jmx.export.MBeanExporter.afterPropertiesSet(MBeanExporter.java:413)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableB
eanFactory.java:1571)

I have no even idea how to debug it.

我什至不知道如何调试它。

Thank you for all your hints.

谢谢你的所有提示。

采纳答案by Gray

Caused by: javax.management.InstanceAlreadyExistsException: bean:name=testBean1

引起:javax.management.InstanceAlreadyExistsException: bean:name=testBean1

This is trying to tell you that you have 2 beans with the same name ObjectNameof bean:name=testBean1being register with the MBeanExporter. However, unless there are other XML files or more entries in your beansmap then there should not be.

这是想告诉你,你有2种豆具有相同名称ObjectNamebean:name=testBean1是寄存器与MBeanExporter。但是,除非您的beans地图中有其他 XML 文件或更多条目,否则不应该有。

I have no even idea how to debug it.

我什至不知道如何调试它。

You could put a breakpoint in the JmxMBeanServer.registerMBean(...)method to see what beans are being registered to see if you can figure out why you are getting a duplicate.

您可以在该JmxMBeanServer.registerMBean(...)方法中放置一个断点以查看正在注册哪些 bean,以查看您是否能弄清楚为什么会得到重复项。



As an aside, my SimpleJMX libraryis an easy way to export your beans via JMX. There is pretty good Spring support as well. Here are the documentation about using with Spring. There is also a Spring test programwhich demonstrates what you need to do to get it working. Here's the Spring XML file.

顺便说一句,我的SimpleJMX 库是一种通过 JMX 导出 bean 的简单方法。也有很好的 Spring 支持。这是有关与 Spring 一起使用的文档。还有一个Spring 测试程序,它演示了您需要做什么才能使其正常工作。这是Spring XML 文件

回答by Arijeet Saha

This is happening as the bean is trying to initialize even after its initialized once.

发生这种情况是因为 bean 即使在初始化一次后仍试图初始化。

I was facing the same issue with RabbitMQ configuration in Spring TestNG testcases.

我在 Spring TestNG 测试用例中遇到了与 RabbitMQ 配置相同的问题。

Use @DirtiesContextas a class level annotation for every testcase class.

使用@DirtiesContext为每个测试用例类的类级别的注释。

This will create applicationcontext and kill it once the testcase class is run and a new applicationcontext will be created for the next testcase class.

这将创建 applicationcontext 并在测试用例类运行后终止它,并且将为下一个测试用例类创建一个新的 applicationcontext。

Example -

例子 -

@Test
@ContextConfiguration(classes = { ApplicationConfig.class })
@DirtiesContext
@WebAppConfiguration
public class ATest extends AbstractTestNGSpringContextTests{
    @BeforeSuite
    public void setup() throws Throwable {

    }
    @AfterSuite
    public void teardown() {

    }
}