Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

overriding default RegistrationPolicy on AnnotationMBeanExporter in Spring Boot

https://github.com/spring-projects/spring-boot/blob/master/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jmx/JmxAutoConfiguration.java

shows

RegistrationPolicy.FAIL_ON_EXISTING 

being set.

We are using spring boot created WARs in a standalone tcserver. New deployments occur before old versions are un-deployed so you can have multiple versions deployed.

I am already using

spring.jmx.default-domain=[app name]

to avoid clashes across apps...but

We are seeing errors like

UnableToRegisterMBeanException: Unable to register MBean with  key 'dataSourceMBean'
  nested exception is javax.management.InstanceAlreadyExistsException

for our datasource Mbean across different version of the same app.

I would like to set a

RegistrationPolicy.IGNORE_EXISTING

as per http://docs.spring.io/spring/docs/current/spring-framework-reference/html/jmx.html#jmx-exporting-registration-behavior.

Can I do this easily while maintaining the ObjectNamingStrategy and defaultDomain? Although not at all difficult, I am hoping I don't have to pretty much override all of JmxAutoConfiguration?

shame there is not a

spring.jmx.mbeanExporter.registrationPolicy

spring boot property

Cheers

like image 937
n99 Avatar asked Mar 13 '23 12:03

n99


1 Answers

Although this question was asked some time ago and maybe you have already found the answer, I will give my 2 cents here once I faced this problem and found a solution that worked for me.

Initially it was not completely clear what I needed to do, but when carefully reading the same spring documentation you provided in your question I figured out you can control the registration behavior on SpringBoot by introducing the class annotation

@EnableMBeanExport(registration=RegistrationPolicy.IGNORE_EXISTING)

to allow Spring ignore a second JMX registration if the MBean was already registered, as can be seen at the spring documentation here

like image 191
Alexandre Queiroz Avatar answered Apr 07 '23 06:04

Alexandre Queiroz