Support for Jmx (and Integration) autoconfig in parent contexts

When there are parent contexts we already had a strategy for registering
the actuator endpoints, but not the regular JMX or Integration MBeans.
This chnage makes the autoconfigs for JMX aware of the parent context.

Also adds a sample with a parent context.

See gh-847
This commit is contained in:
Dave Syer
2014-06-10 09:13:19 +01:00
parent 5adbf32c18
commit 36130b27e1
17 changed files with 537 additions and 4 deletions

View File

@@ -18,6 +18,8 @@ package org.springframework.boot.autoconfigure.jmx;
import javax.management.MBeanServer;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
@@ -26,7 +28,13 @@ import org.springframework.boot.autoconfigure.condition.SearchStrategy;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableMBeanExport;
import org.springframework.context.annotation.MBeanExportConfiguration;
import org.springframework.core.env.Environment;
import org.springframework.core.type.StandardAnnotationMetadata;
import org.springframework.jmx.export.MBeanExporter;
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;
/**
@@ -42,10 +50,33 @@ import org.springframework.jmx.support.MBeanServerFactoryBean;
@ConditionalOnExpression("${spring.jmx.enabled:true}")
public class JmxAutoConfiguration {
@Configuration
@Autowired
private Environment environment;
@Autowired
private BeanFactory beanFactory;
@Autowired
private ObjectNamingStrategy namingStrategy;
@Bean
@ConditionalOnMissingBean(value = MBeanExporter.class, search = SearchStrategy.CURRENT)
@EnableMBeanExport(defaultDomain = "${spring.jmx.default_domain:}", server = "${spring.jmx.server:mbeanServer}")
public static class MBeanExport {
public AnnotationMBeanExporter mbeanExporter() {
// Re-use the @EnableMBeanExport configuration
MBeanExportConfiguration config = new MBeanExportConfiguration();
config.setEnvironment(this.environment);
config.setBeanFactory(this.beanFactory);
config.setImportMetadata(new StandardAnnotationMetadata(Empty.class));
// But add a custom naming strategy
AnnotationMBeanExporter exporter = config.mbeanExporter();
exporter.setNamingStrategy(this.namingStrategy);
return exporter;
}
@Bean
@ConditionalOnMissingBean(ObjectNamingStrategy.class)
public ParentAwareNamingStrategy objectNamingStrategy() {
return new ParentAwareNamingStrategy(new AnnotationJmxAttributeSource());
}
@Bean
@@ -56,4 +87,9 @@ public class JmxAutoConfiguration {
return factory;
}
@EnableMBeanExport(defaultDomain = "${spring.jmx.default_domain:}", server = "${spring.jmx.server:mbeanServer}")
private static class Empty {
}
}

View File

@@ -0,0 +1,86 @@
/*
* Copyright 2012-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.autoconfigure.jmx;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.jmx.export.metadata.JmxAttributeSource;
import org.springframework.jmx.export.naming.MetadataNamingStrategy;
import org.springframework.util.ObjectUtils;
/**
* @author Dave Syer
* @since 1.1.1
*/
public class ParentAwareNamingStrategy extends MetadataNamingStrategy implements
ApplicationContextAware {
private ApplicationContext applicationContext;
private boolean ensureUniqueRuntimeObjectNames = false;
public ParentAwareNamingStrategy(JmxAttributeSource attributeSource) {
super(attributeSource);
}
/**
* @param ensureUniqueRuntimeObjectNames the ensureUniqueRuntimeObjectNames to set
*/
public void setEnsureUniqueRuntimeObjectNames(boolean ensureUniqueRuntimeObjectNames) {
this.ensureUniqueRuntimeObjectNames = ensureUniqueRuntimeObjectNames;
}
@Override
public ObjectName getObjectName(Object managedBean, String beanKey)
throws MalformedObjectNameException {
StringBuilder builder = new StringBuilder(beanKey);
if (parentContextContainsSameBean(this.applicationContext, beanKey)) {
builder.append(",context="
+ ObjectUtils.getIdentityHexString(this.applicationContext));
}
if (this.ensureUniqueRuntimeObjectNames) {
builder.append(",identity=" + ObjectUtils.getIdentityHexString(managedBean));
}
ObjectName name = super.getObjectName(managedBean, beanKey);
return name;
}
@Override
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
this.applicationContext = applicationContext;
}
private boolean parentContextContainsSameBean(ApplicationContext applicationContext,
String beanKey) {
if (applicationContext.getParent() != null) {
try {
this.applicationContext.getParent().getBean(beanKey);
return true;
}
catch (BeansException ex) {
return parentContextContainsSameBean(applicationContext.getParent(),
beanKey);
}
}
return false;
}
}

View File

@@ -18,6 +18,7 @@ package org.springframework.boot.autoconfigure.integration;
import org.junit.Test;
import org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.integration.support.channel.HeaderChannelRegistry;
@@ -29,10 +30,18 @@ import static org.junit.Assert.assertNotNull;
*/
public class IntegrationAutoConfigurationTests {
private final AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
private AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
@Test
public void integrationIsAvailable() {
this.context.register(IntegrationAutoConfiguration.class);
this.context.refresh();
assertNotNull(this.context.getBean(HeaderChannelRegistry.class));
this.context.close();
}
@Test
public void addJmxAuto() {
this.context.register(JmxAutoConfiguration.class,
IntegrationAutoConfiguration.class);
this.context.refresh();
@@ -40,4 +49,18 @@ public class IntegrationAutoConfigurationTests {
this.context.close();
}
@Test
public void parentContext() {
this.context.register(IntegrationAutoConfiguration.class);
this.context.refresh();
AnnotationConfigApplicationContext parent = this.context;
this.context = new AnnotationConfigApplicationContext();
this.context.setParent(parent);
this.context.register(IntegrationAutoConfiguration.class);
this.context.refresh();
assertNotNull(this.context.getBean(HeaderChannelRegistry.class));
((ConfigurableApplicationContext) this.context.getParent()).close();
this.context.close();
}
}

View File

@@ -21,6 +21,7 @@ import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@@ -51,6 +52,9 @@ public class JmxAutoConfigurationTests {
public void tearDown() {
if (this.context != null) {
this.context.close();
if (this.context.getParent() != null) {
((ConfigurableApplicationContext) this.context.getParent()).close();
}
}
}
@@ -104,6 +108,18 @@ public class JmxAutoConfigurationTests {
ReflectionTestUtils.getField(naming, "defaultDomain"));
}
@Test
public void testParentContext() {
this.context = new AnnotationConfigApplicationContext();
this.context.register(JmxAutoConfiguration.class);
this.context.refresh();
AnnotationConfigApplicationContext parent = this.context;
this.context = new AnnotationConfigApplicationContext();
this.context.setParent(parent);
this.context.register(JmxAutoConfiguration.class);
this.context.refresh();
}
@Configuration
public static class TestConfiguration {