INT-1789: Added 'managed-components' attribute to mbean-export

This commit is contained in:
Dave Syer
2011-05-03 10:22:20 +01:00
parent ea469ca887
commit 41a8307266
5 changed files with 72 additions and 2 deletions

View File

@@ -53,6 +53,7 @@ public class MBeanExporterParser extends AbstractSingleBeanDefinitionParser {
builder.getRawBeanDefinition().setSource(parserContext.extractSource(element));
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "default-domain");
IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "object-name-static-properties");
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "managed-components", "componentNamePatterns");
builder.addPropertyValue("server", mbeanServer);
}

View File

@@ -63,6 +63,7 @@ import org.springframework.jmx.export.assembler.MetadataMBeanInfoAssembler;
import org.springframework.jmx.export.naming.MetadataNamingStrategy;
import org.springframework.jmx.support.MetricType;
import org.springframework.util.Assert;
import org.springframework.util.PatternMatchUtils;
import org.springframework.util.ReflectionUtils;
/**
@@ -142,6 +143,8 @@ public class IntegrationMBeanExporter extends MBeanExporter implements BeanPostP
private final MetadataNamingStrategy namingStrategy = new MetadataNamingStrategy(attributeSource);
private String[] componentNamePatterns = { "*" };
public IntegrationMBeanExporter() {
super();
// Shouldn't be necessary, but to be on the safe side...
@@ -176,6 +179,11 @@ public class IntegrationMBeanExporter extends MBeanExporter implements BeanPostP
this.namingStrategy.setDefaultDomain(domain);
}
public void setComponentNamePatterns(String[] componentNamePatterns) {
Assert.notEmpty(componentNamePatterns, "componentNamePatterns must not be empty");
this.componentNamePatterns = componentNamePatterns;
}
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
super.setBeanFactory(beanFactory);
Assert.isTrue(beanFactory instanceof ListableBeanFactory, "A ListableBeanFactory is required.");
@@ -494,6 +502,9 @@ public class IntegrationMBeanExporter extends MBeanExporter implements BeanPostP
private void registerChannels() {
for (DirectChannelMetrics monitor : channels) {
String name = monitor.getName();
if (!PatternMatchUtils.simpleMatch(this.componentNamePatterns, name)) {
continue;
}
// Only register once...
if (!channelsByName.containsKey(name)) {
String beanKey = getChannelBeanKey(name);
@@ -515,6 +526,9 @@ public class IntegrationMBeanExporter extends MBeanExporter implements BeanPostP
for (SimpleMessageHandlerMetrics source : handlers) {
MessageHandlerMetrics monitor = enhanceHandlerMonitor(source);
String name = monitor.getName();
if (!PatternMatchUtils.simpleMatch(this.componentNamePatterns, name)) {
continue;
}
// Only register once...
if (!handlersByName.containsKey(name)) {
String beanKey = getHandlerBeanKey(monitor);
@@ -535,6 +549,9 @@ public class IntegrationMBeanExporter extends MBeanExporter implements BeanPostP
for (SimpleMessageSourceMetrics source : sources) {
MessageSourceMetrics monitor = enhanceSourceMonitor(source);
String name = monitor.getName();
if (!PatternMatchUtils.simpleMatch(this.componentNamePatterns, name)) {
continue;
}
// Only register once...
if (!sourcesByName.containsKey(name)) {
String beanKey = getSourceBeanKey(monitor);
@@ -558,12 +575,19 @@ public class IntegrationMBeanExporter extends MBeanExporter implements BeanPostP
AbstractEndpoint endpoint = beanFactory.getBean(name, AbstractEndpoint.class);
String beanKey;
name = endpoint.getComponentName();
String source;
if (name.startsWith("_org.springframework.integration")) {
beanKey = getEndpointBeanKey(endpoint, getInternalComponentName(name), "internal");
name = getInternalComponentName(name);
source = "internal";
}
else {
beanKey = getEndpointBeanKey(endpoint, endpoint.getComponentName(), "endpoint");
name = endpoint.getComponentName();
source = "endpoint";
}
if (!PatternMatchUtils.simpleMatch(this.componentNamePatterns, name)) {
continue;
}
beanKey = getEndpointBeanKey(endpoint, name, source);
ObjectName objectName = registerBeanInstance(new ManagedEndpoint(endpoint), beanKey);
logger.info("Registered endpoint without MessageSource: " + objectName);
}

View File

@@ -124,6 +124,13 @@
</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="managed-components" use="optional">
<xsd:annotation>
<xsd:documentation>
Comma separated list of simple patterns for component names to register (defaults to '*').
</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>

View File

@@ -129,6 +129,17 @@ public class MBeanExporterIntegrationTests {
assertEquals("start", startName);
}
@Test
public void testComponentNames() throws Exception {
context = new GenericXmlApplicationContext(getClass(), "excluded-components.xml");
messageChannelsMonitor = context.getBean(IntegrationMBeanExporter.class);
assertNotNull(messageChannelsMonitor);
MBeanServer server = context.getBean(MBeanServer.class);
Set<ObjectName> names = server.queryNames(ObjectName.getInstance("org.springframework.integration:type=*,*"), null);
// Only one registered (out of >2 available)
assertEquals(1, names.size());
}
public static class DateFactoryBean implements FactoryBean<Date> {
private Date date;

View File

@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context" xmlns:int="http://www.springframework.org/schema/integration"
xsi:schemaLocation="http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<bean id="integrationExecutions" class="org.springframework.integration.monitor.IntegrationMBeanExporter">
<property name="server" ref="mbeanServer" />
<property name="componentNamePatterns" value="input" />
</bean>
<bean id="mbeanServer" class="org.springframework.jmx.support.MBeanServerFactoryBean">
<property name="locateExistingServerIfPossible" value="false" />
</bean>
<int:channel id="input">
<int:queue />
</int:channel>
<int:inbound-channel-adapter id="explicit" ref="service" method="execute" channel="input">
<int:poller fixed-rate="200" />
</int:inbound-channel-adapter>
<bean id="service" class="org.springframework.integration.monitor.MBeanExporterIntegrationTests$SimpleService" />
</beans>