INT-3330 EnableIntegrationMBeanExport Annotation

JIRA: https://jira.spring.io/browse/INT-3330

INT-3330: Enable SpEL evaluation

INT-3330: Polishing for `MBeanExporterHelper`

INT-3330: Fix `errorChannel` early access

INT-3330: Polishing according PR comments

Polishing - copyrights, author, docs
This commit is contained in:
Artem Bilan
2014-03-18 20:57:36 +02:00
committed by Gary Russell
parent 1d6e80ecd0
commit 97c270c0e2
23 changed files with 643 additions and 196 deletions

View File

@@ -1,50 +0,0 @@
/*
* Copyright 2002-2011 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.integration.jmx.config;
import org.junit.Test;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.RootBeanDefinition;
/**
* @author Oleg Zhurakousky
*
*/
public class MBeanExporterHelperTests {
@Test
public void testNoNpeWhenClassNameIsMissing(){
DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
RootBeanDefinition bd = new RootBeanDefinition();
factory.registerBeanDefinition("foo", bd);
MBeanExporterHelper helper = new MBeanExporterHelper();
helper.postProcessBeanFactory(factory);
}
@Test
public void testNoNpeWithAbstractBean(){
DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
RootBeanDefinition abstractBd = new RootBeanDefinition();
abstractBd.setAbstract(true);
abstractBd.setBeanClass(String.class);
factory.registerBeanDefinition("abstractFoo", abstractBd);
RootBeanDefinition concreteBd = new RootBeanDefinition(abstractBd);
factory.registerBeanDefinition("concreteFoo", concreteBd);
MBeanExporterHelper helper = new MBeanExporterHelper();
helper.postProcessBeanFactory(factory);
}
}

View File

@@ -0,0 +1,139 @@
/*
* Copyright 2014 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.integration.jmx.configuration;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import java.util.List;
import java.util.Set;
import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import org.hamcrest.Matchers;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.aop.support.AopUtils;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.config.EnableIntegration;
import org.springframework.integration.context.IntegrationContextUtils;
import org.springframework.integration.jmx.config.EnableIntegrationMBeanExport;
import org.springframework.integration.monitor.IntegrationMBeanExporter;
import org.springframework.integration.test.util.TestUtils;
import org.springframework.jmx.support.MBeanServerFactoryBean;
import org.springframework.mock.env.MockEnvironment;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* @author Artem Bilan
* @since 4.0
*/
@ContextConfiguration(initializers = EnableMBeanExportTests.EnvironmentApplicationContextInitializer.class)
@RunWith(SpringJUnit4ClassRunner.class)
@DirtiesContext
public class EnableMBeanExportTests {
@Autowired
private BeanFactory beanFactory;
@Autowired
private IntegrationMBeanExporter exporter;
@Autowired
private MBeanServer mBeanServer;
@SuppressWarnings("unchecked")
@Test
public void testEnableMBeanExport() throws MalformedObjectNameException, ClassNotFoundException {
assertTrue(AopUtils.isAopProxy(this.beanFactory.getBean(IntegrationContextUtils.NULL_CHANNEL_BEAN_NAME)));
assertTrue(AopUtils.isAopProxy(this.beanFactory.getBean(IntegrationContextUtils.ERROR_CHANNEL_BEAN_NAME)));
assertSame(this.mBeanServer, this.exporter.getServer());
String[] componentNamePatterns = TestUtils.getPropertyValue(this.exporter, "componentNamePatterns", String[].class);
for (String componentNamePattern : componentNamePatterns) {
assertThat(componentNamePattern, Matchers.isOneOf("input", "in*"));
assertThat(componentNamePattern, Matchers.not(Matchers.equalTo("*")));
}
Set<ObjectName> names = this.mBeanServer.queryNames(ObjectName.getInstance("FOO:type=MessageChannel,*"), null);
// Only one registered (out of >2 available)
assertEquals(1, names.size());
assertEquals("input", names.iterator().next().getKeyProperty("name"));
names = this.mBeanServer.queryNames(ObjectName.getInstance("FOO:type=MessageHandler,*"), null);
assertEquals(0, names.size());
Class<?> clazz = Class.forName("org.springframework.integration.jmx.config.MBeanExporterHelper");
List<Object> beanPostProcessors = TestUtils.getPropertyValue(beanFactory, "beanPostProcessors", List.class);
Object mBeanExporterHelper = null;
for (Object beanPostProcessor : beanPostProcessors) {
if (clazz.isAssignableFrom(beanPostProcessor.getClass())) {
mBeanExporterHelper = beanPostProcessor;
break;
}
}
assertNotNull(mBeanExporterHelper);
assertTrue(TestUtils.getPropertyValue(mBeanExporterHelper, "siBeanNames", Set.class).contains("input"));
assertTrue(TestUtils.getPropertyValue(mBeanExporterHelper, "siBeanNames", Set.class).contains("output"));
}
@Configuration
@EnableIntegration
@EnableIntegrationMBeanExport(server = "#{mbeanServer}", defaultDomain = "${managed.domain}", managedComponents = {"input", "${managed.component}"})
public static class ContextConfiguration {
@Bean
public MBeanServerFactoryBean mbeanServer() {
return new MBeanServerFactoryBean();
}
@Bean
public QueueChannel input() {
return new QueueChannel();
}
@Bean
public QueueChannel output() {
return new QueueChannel();
}
}
public static class EnvironmentApplicationContextInitializer implements ApplicationContextInitializer<GenericApplicationContext> {
@Override
public void initialize(GenericApplicationContext applicationContext) {
applicationContext.setEnvironment(new MockEnvironment()
.withProperty("managed.component", "input,in*")
.withProperty("managed.domain", "FOO"));
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2014 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
@@ -13,6 +13,7 @@
package org.springframework.integration_.mbeanexporterhelper;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.util.List;
@@ -33,6 +34,7 @@ import org.springframework.jmx.export.MBeanExporter;
/**
* @author Oleg Zhurakousky
* @author Gary Russell
* @author Artem Bilan
*
*/
public class Int2307Tests {
@@ -70,9 +72,17 @@ public class Int2307Tests {
assertEquals(4, count);
Class<?> clazz = Class.forName("org.springframework.integration.jmx.config.MBeanExporterHelper");
Object mBeanExporterHelper = context.getBean(clazz);
assertTrue(((Set<String>)TestUtils.getPropertyValue(mBeanExporterHelper, "siBeanNames")).contains("z"));
assertTrue(((Set<String>)TestUtils.getPropertyValue(mBeanExporterHelper, "siBeanNames")).contains("zz"));
List<Object> beanPostProcessors = TestUtils.getPropertyValue(context, "beanFactory.beanPostProcessors", List.class);
Object mBeanExporterHelper = null;
for (Object beanPostProcessor : beanPostProcessors) {
if (clazz.isAssignableFrom(beanPostProcessor.getClass())) {
mBeanExporterHelper = beanPostProcessor;
break;
}
}
assertNotNull(mBeanExporterHelper);
assertTrue(TestUtils.getPropertyValue(mBeanExporterHelper, "siBeanNames", Set.class).contains("z"));
assertTrue(TestUtils.getPropertyValue(mBeanExporterHelper, "siBeanNames", Set.class).contains("zz"));
// make sure there are no duplicate MBean ObjectNames if 2 contexts loaded from same config
new ClassPathXmlApplicationContext("single-config.xml", this.getClass());
@@ -90,8 +100,16 @@ public class Int2307Tests {
assertTrue(excludedBeanNames.contains("y"));
assertTrue(excludedBeanNames.contains("foo")); // non SI bean
Class<?> clazz = Class.forName("org.springframework.integration.jmx.config.MBeanExporterHelper");
Object mBeanExporterHelper = context.getBean(clazz);
assertTrue(((Set<String>)TestUtils.getPropertyValue(mBeanExporterHelper, "siBeanNames")).contains("z"));
List<Object> beanPostProcessors = TestUtils.getPropertyValue(context, "beanFactory.beanPostProcessors", List.class);
Object mBeanExporterHelper = null;
for (Object beanPostProcessor : beanPostProcessors) {
if (clazz.isAssignableFrom(beanPostProcessor.getClass())) {
mBeanExporterHelper = beanPostProcessor;
break;
}
}
assertNotNull(mBeanExporterHelper);
assertTrue(TestUtils.getPropertyValue(mBeanExporterHelper, "siBeanNames", Set.class).contains("z"));
}
public static class Foo{}