Home > database >  spring.jmx.enabled property always set to true
spring.jmx.enabled property always set to true

Time:09-18

Now, I have a problem about setting a property named spring.jmx.enabled While upgrading the spring version from 2.2.7 to 2.7.0, we found the following error.

ERROR 2022-09-17 23:27:06 [main] o.s.boot.SpringApplication [tid=] - Application run failed
org.springframework.jmx.export.UnableToRegisterMBeanException: Unable to register MBean [HikariDataSource (some-service-write)] with key 'writeDataSource'; nested exception is javax.management.InstanceAlreadyExistsException: MXBean already registered with name com.zaxxer.hikari:type=PoolConfig (some-service-write)
    at org.springframework.jmx.export.MBeanExporter.registerBeanNameOrInstance(MBeanExporter.java:626)
    at org.springframework.jmx.export.MBeanExporter.lambda$registerBeans$2(MBeanExporter.java:552)
    at java.util.HashMap.forEach(HashMap.java:1289)
    at org.springframework.jmx.export.MBeanExporter.registerBeans(MBeanExporter.java:552)
    at org.springframework.jmx.export.MBeanExporter.afterSingletonsInstantiated(MBeanExporter.java:435)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:972)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583)
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:147)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:734)
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:408)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:308)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1306)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1295)
    at com.company.team.name.some.SomeServiceApplication.main(SomeServiceApplication.java:16)
Caused by: javax.management.InstanceAlreadyExistsException: MXBean already registered with name com.zaxxer.hikari:type=PoolConfig (some-service-write)
    at com.sun.jmx.mbeanserver.MXBeanLookup.addReference(MXBeanLookup.java:151)
    at com.sun.jmx.mbeanserver.MXBeanSupport.register(MXBeanSupport.java:160)
    at com.sun.jmx.mbeanserver.MBeanSupport.preRegister2(MBeanSupport.java:173)
    at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.registerDynamicMBean(DefaultMBeanServerInterceptor.java:930)
    at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.registerObject(DefaultMBeanServerInterceptor.java:900)
    at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.registerMBean(DefaultMBeanServerInterceptor.java:324)
    at com.sun.jmx.mbeanserver.JmxMBeanServer.registerMBean(JmxMBeanServer.java:522)
    at org.springframework.jmx.support.MBeanRegistrationSupport.doRegister(MBeanRegistrationSupport.java:138)
    at org.springframework.jmx.export.MBeanExporter.registerBeanInstance(MBeanExporter.java:672)
    at org.springframework.jmx.export.MBeanExporter.registerBeanNameOrInstance(MBeanExporter.java:616)
    ... 14 common frames omitted

I searched for this error, and most of them answered that can be solved by setting spring.jmx.enabled to false. However, I checked that the error continues to occur even if the property corrects this value.

Debugging, this value was coming to true.

I injected properties into the Vm Option like -Dspring.jmx.enabled=false, but I confirmed that the same result came.

How can I set this value to false?

CodePudding user response:

if you define in application.propertiers:

spring.datasource.hikari.registerMbeans=true

you have to disable the default Mbean with this config

 @Bean
  public MBeanExporter exporter() {
    final MBeanExporter exporter = new AnnotationMBeanExporter();
    exporter.setAutodetect(true);
    exporter.setExcludedBeans("dataSource");
    return exporter;
  }

for more details : https://github.com/brettwooldridge/HikariCP/issues/342

CodePudding user response:

Make sure you do not have @EnableMBeanExport on your application configuration. It can override the external property.

Learn more about this annotation here: https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/context/annotation/EnableMBeanExport.html

CodePudding user response:

You can try changing the default registration policy as below, it will ignore existing bean.

@Configuration
@EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING)
public class MXBeansConfig {
}
  • Related