mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-08-29 03:06:45 +08:00
Allow JMX Registration Policy to be set
See gh-32573
This commit is contained in:
parent
a5d1b672d3
commit
f2950f2d09
@ -34,7 +34,6 @@ import org.springframework.jmx.export.annotation.AnnotationJmxAttributeSource;
|
||||
import org.springframework.jmx.export.annotation.AnnotationMBeanExporter;
|
||||
import org.springframework.jmx.export.naming.ObjectNamingStrategy;
|
||||
import org.springframework.jmx.support.MBeanServerFactoryBean;
|
||||
import org.springframework.jmx.support.RegistrationPolicy;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
@ -67,7 +66,7 @@ public class JmxAutoConfiguration {
|
||||
@ConditionalOnMissingBean(value = MBeanExporter.class, search = SearchStrategy.CURRENT)
|
||||
public AnnotationMBeanExporter mbeanExporter(ObjectNamingStrategy namingStrategy, BeanFactory beanFactory) {
|
||||
AnnotationMBeanExporter exporter = new AnnotationMBeanExporter();
|
||||
exporter.setRegistrationPolicy(RegistrationPolicy.FAIL_ON_EXISTING);
|
||||
exporter.setRegistrationPolicy(this.properties.getRegistrationPolicy());
|
||||
exporter.setNamingStrategy(namingStrategy);
|
||||
String serverBean = this.properties.getServer();
|
||||
if (StringUtils.hasLength(serverBean)) {
|
||||
|
@ -17,6 +17,7 @@
|
||||
package org.springframework.boot.autoconfigure.jmx;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.jmx.support.RegistrationPolicy;
|
||||
|
||||
/**
|
||||
* Configuration properties for JMX.
|
||||
@ -47,6 +48,11 @@ public class JmxProperties {
|
||||
*/
|
||||
private String defaultDomain;
|
||||
|
||||
/**
|
||||
* JMX Registration policy.
|
||||
*/
|
||||
private RegistrationPolicy registrationPolicy = RegistrationPolicy.FAIL_ON_EXISTING;
|
||||
|
||||
public boolean getEnabled() {
|
||||
return this.enabled;
|
||||
}
|
||||
@ -79,4 +85,12 @@ public class JmxProperties {
|
||||
this.defaultDomain = defaultDomain;
|
||||
}
|
||||
|
||||
public RegistrationPolicy getRegistrationPolicy() {
|
||||
return this.registrationPolicy;
|
||||
}
|
||||
|
||||
public void setRegistrationPolicy(RegistrationPolicy registrationPolicy) {
|
||||
this.registrationPolicy = registrationPolicy;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -33,6 +33,7 @@ import org.springframework.jmx.export.annotation.ManagedOperation;
|
||||
import org.springframework.jmx.export.annotation.ManagedResource;
|
||||
import org.springframework.jmx.export.naming.MetadataNamingStrategy;
|
||||
import org.springframework.jmx.export.naming.ObjectNamingStrategy;
|
||||
import org.springframework.jmx.support.RegistrationPolicy;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
@ -72,6 +73,8 @@ class JmxAutoConfigurationTests {
|
||||
assertThat(context).hasSingleBean(ParentAwareNamingStrategy.class);
|
||||
MBeanExporter exporter = context.getBean(MBeanExporter.class);
|
||||
assertThat(exporter).hasFieldOrPropertyWithValue("ensureUniqueRuntimeObjectNames", false);
|
||||
assertThat(exporter).hasFieldOrPropertyWithValue("registrationPolicy", RegistrationPolicy.FAIL_ON_EXISTING);
|
||||
|
||||
MetadataNamingStrategy naming = (MetadataNamingStrategy) ReflectionTestUtils.getField(exporter,
|
||||
"namingStrategy");
|
||||
assertThat(naming).hasFieldOrPropertyWithValue("ensureUniqueRuntimeObjectNames", false);
|
||||
@ -80,15 +83,21 @@ class JmxAutoConfigurationTests {
|
||||
|
||||
@Test
|
||||
void testDefaultDomainConfiguredOnMBeanExport() {
|
||||
this.contextRunner.withPropertyValues("spring.jmx.enabled=true", "spring.jmx.default-domain=my-test-domain",
|
||||
"spring.jmx.unique-names=true").run((context) -> {
|
||||
this.contextRunner
|
||||
.withPropertyValues("spring.jmx.enabled=true", "spring.jmx.default-domain=my-test-domain",
|
||||
"spring.jmx.unique-names=true", "spring.jmx.registration-policy=IGNORE_EXISTING")
|
||||
.run((context) -> {
|
||||
assertThat(context).hasSingleBean(MBeanExporter.class);
|
||||
MBeanExporter exporter = context.getBean(MBeanExporter.class);
|
||||
assertThat(exporter).hasFieldOrPropertyWithValue("ensureUniqueRuntimeObjectNames", true);
|
||||
assertThat(exporter).hasFieldOrPropertyWithValue("registrationPolicy",
|
||||
RegistrationPolicy.IGNORE_EXISTING);
|
||||
|
||||
MetadataNamingStrategy naming = (MetadataNamingStrategy) ReflectionTestUtils.getField(exporter,
|
||||
"namingStrategy");
|
||||
assertThat(naming).hasFieldOrPropertyWithValue("defaultDomain", "my-test-domain");
|
||||
assertThat(naming).hasFieldOrPropertyWithValue("ensureUniqueRuntimeObjectNames", true);
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user