简体   繁体   中英

Cannot integrate JMX with Spring application

I have got a SPRING application. When I run

mvn jetty:run

everything is ok.

I would like to use JMX in my project.

I created another project, I tried tutorial for beginners and I was able to see some changes with 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

How to integrate JMX with 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

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

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 implements to my class:

public class MyMainClassBean implements IMyMainClassBean {...}

Last thing, I edited my xml file:

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

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

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

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.


As an aside, my SimpleJMX library is 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 program which demonstrates what you need to do to get it working. Here's the Spring XML file .

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

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

Use @DirtiesContext as a class level annotation for every testcase class.

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.

Example -

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

    }
    @AfterSuite
    public void teardown() {

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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