gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 12:14:20 25 4
gpt4 key购买 nike

我有一个SPRING申请。当我运行时

mvn jetty:run

一切都好。

我想在我的项目中使用JMX

我创建了另一个项目,我尝试了针对初学者的教程,并且我能够使用 jconsole 看到一些更改。

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

How to integrate JMX with Spring?

我有一个类:

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

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

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

我改变了一些东西以使其能够与 JMX 一起使用。

我添加了一个界面:

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

我向我的类(class)添加了工具:

public class MyMainClassBean implements IMyMainClassBean {...}

最后一件事,我编辑了我的 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 -->

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

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)

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

感谢您的所有提示。

最佳答案

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

这是试图告诉您,您有 2 个与 bean:name=testBean1 具有相同名称 ObjectName 的 bean,正在向 MBeanExporter 注册>。但是,除非您的 beans 映射中存在其他 XML 文件或更多条目,否则不应该有。

I have no even idea how to debug it.

您可以在 JmxMBeanServer.registerMBean(...) 方法中放置一个断点来查看正在注册哪些 Bean,从而确定您是否可以找出为什么会出现重复的情况。

<小时/>

顺便说一句,我的 SimpleJMX library是通过 JMX 导出 Bean 的简单方法。 Spring 支持也非常好。以下是有关 using with Spring 的文档。还有一个Spring test program它演示了您需要做什么才能使其正常工作。这是Spring XML file .

关于java - 无法将 JMX 与 Spring 应用程序集成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22789428/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com