INT-1675: force initialization of other MBeanExporter instances before the integration monitor components are created

This commit is contained in:
Dave Syer
2010-12-10 12:20:05 +01:00
parent c293edc7fa
commit e23e478e9e
6 changed files with 117 additions and 46 deletions

View File

@@ -31,7 +31,11 @@ import org.w3c.dom.Element;
*/
public class MBeanExporterParser extends AbstractSingleBeanDefinitionParser {
private static final String ILLEGAL_NAME = "mbeanExporter";
/**
*
*/
private static final String MBEAN_EXPORTER_NAME = "mbeanExporter";
private static final String ILLEGAL_NAME = MBEAN_EXPORTER_NAME;
@Override
protected boolean shouldGenerateIdAsFallback() {

View File

@@ -83,14 +83,13 @@ import org.springframework.util.ReflectionUtils;
* @author Oleg Zhurakousky
*/
@ManagedResource
public class IntegrationMBeanExporter extends MBeanExporter
implements BeanPostProcessor, BeanFactoryAware, BeanClassLoaderAware, SmartLifecycle {
public class IntegrationMBeanExporter extends MBeanExporter implements BeanPostProcessor, BeanFactoryAware,
BeanClassLoaderAware, SmartLifecycle {
private static final Log logger = LogFactory.getLog(IntegrationMBeanExporter.class);
public static final String DEFAULT_DOMAIN = "org.springframework.integration";
private final AnnotationJmxAttributeSource attributeSource = new AnnotationJmxAttributeSource();
private ListableBeanFactory beanFactory;
@@ -125,7 +124,6 @@ public class IntegrationMBeanExporter extends MBeanExporter
private final Map<String, String> objectNameStaticProperties = new HashMap<String, String>();
public IntegrationMBeanExporter() {
super();
// Shouldn't be necessary, but to be on the safe side...
@@ -134,7 +132,6 @@ public class IntegrationMBeanExporter extends MBeanExporter
setAssembler(new MetadataMBeanInfoAssembler(attributeSource));
}
@Override
public void setBeanClassLoader(ClassLoader classLoader) {
this.beanClassLoader = classLoader;
@@ -183,8 +180,7 @@ public class IntegrationMBeanExporter extends MBeanExporter
Object advised = applyHandlerInterceptor(bean, monitor, beanClassLoader);
handlers.add(monitor);
return advised;
}
else if (bean instanceof MessageSource<?>) {
} else if (bean instanceof MessageSource<?>) {
SimpleMessageSourceMetrics monitor = new SimpleMessageSourceMetrics((MessageSource<?>) bean);
Object advised = applySourceInterceptor(bean, monitor, beanClassLoader);
sources.add(monitor);
@@ -197,12 +193,10 @@ public class IntegrationMBeanExporter extends MBeanExporter
Object target = extractTarget(bean);
if (target instanceof QueueChannel) {
monitor = new QueueChannelMetrics((QueueChannel) target, beanName);
}
else {
} else {
monitor = new PollableChannelMetrics(beanName);
}
}
else {
} else {
monitor = new DirectChannelMetrics(beanName);
}
Object advised = applyChannelInterceptor(bean, monitor, beanClassLoader);
@@ -233,8 +227,7 @@ public class IntegrationMBeanExporter extends MBeanExporter
this.lifecycleLock.lock();
try {
return this.running;
}
finally {
} finally {
this.lifecycleLock.unlock();
}
}
@@ -249,8 +242,7 @@ public class IntegrationMBeanExporter extends MBeanExporter
logger.info("started " + this);
}
}
}
finally {
} finally {
this.lifecycleLock.unlock();
}
}
@@ -265,8 +257,7 @@ public class IntegrationMBeanExporter extends MBeanExporter
logger.info("stopped " + this);
}
}
}
finally {
} finally {
this.lifecycleLock.unlock();
}
}
@@ -276,8 +267,7 @@ public class IntegrationMBeanExporter extends MBeanExporter
try {
this.stop();
callback.run();
}
finally {
} finally {
this.lifecycleLock.unlock();
}
}
@@ -295,6 +285,16 @@ public class IntegrationMBeanExporter extends MBeanExporter
registerSources();
}
@Override
public void afterPropertiesSet() {
super.afterPropertiesSet();
/*
* Force early initialization of other MBeanExporters. Workaround for possible bug in Spring (see INT-1675) for
* more details.
*/
beanFactory.getBeansOfType(MBeanExporter.class);
}
@Override
public void destroy() {
super.destroy();
@@ -457,8 +457,7 @@ public class IntegrationMBeanExporter extends MBeanExporter
}
try {
return extractTarget(advised.getTargetSource().getTarget());
}
catch (Exception e) {
} catch (Exception e) {
logger.error("Could not extract target", e);
return null;
}
@@ -470,8 +469,7 @@ public class IntegrationMBeanExporter extends MBeanExporter
if (bean instanceof Advised) {
((Advised) bean).addAdvisor(advisor);
return bean;
}
else {
} else {
ProxyFactory proxyFactory = new ProxyFactory(bean);
proxyFactory.addAdvisor(advisor);
return proxyFactory.getProxy(beanClassLoader);
@@ -531,8 +529,7 @@ public class IntegrationMBeanExporter extends MBeanExporter
Object field = null;
try {
field = extractTarget(getField(endpoint, "handler"));
}
catch (Exception e) {
} catch (Exception e) {
logger.trace("Could not get handler from bean = " + beanName);
}
if (field == monitor.getMessageHandler()) {
@@ -551,8 +548,7 @@ public class IntegrationMBeanExporter extends MBeanExporter
if (targetSource != null) {
try {
target = targetSource.getTarget();
}
catch (Exception e) {
} catch (Exception e) {
logger.debug("Could not get handler from bean = " + name);
}
}

View File

@@ -11,19 +11,16 @@
http://www.springframework.org/schema/integration/jmx
http://www.springframework.org/schema/integration/jmx/spring-integration-jmx.xsd">
<jmx:mbean-export server="mbs" default-domain="test.RouterMBean" />
<context:mbean-export server="mbs" default-domain="test.RouterMBean" />
<context:mbean-server id="mbs" />
<int:channel id="testChannel" />
<int:channel id="intChannel" />
<int:channel id="stringChannel" />
<int:payload-type-router id="ptRouter" input-channel="testChannel">
<int:mapping type="java.lang.String" channel="stringChannel" />
<int:mapping type="java.lang.Integer" channel="intChannel" />
</int:payload-type-router>
<int:router id="ptRouter" input-channel="testChannel" expression="payload instanceof String ? 'stringChannel' : 'intChannel'"/>
<context:mbean-server id="mbs" />
<context:mbean-export server="mbs" default-domain="test.RouterMBean" />
<jmx:mbean-export server="mbs" default-domain="test.RouterMBean" />
</beans>

View File

@@ -15,32 +15,56 @@ package org.springframework.integration.jmx.config;
import static org.junit.Assert.assertEquals;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import javax.management.MBeanServer;
import javax.management.ObjectName;
import org.junit.After;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author Dave Syer
* @since 2.0
*/
@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
@RunWith(Parameterized.class)
public class RouterMBeanTests {
@Autowired
private MBeanServer server;
private ClassPathXmlApplicationContext context;
public RouterMBeanTests(String configLocation) {
context = new ClassPathXmlApplicationContext(configLocation, getClass());
server = context.getBean(MBeanServer.class);
}
@Parameters
public static List<Object[]> getParameters() {
return Arrays.asList(
new Object[] { RouterMBeanTests.class.getSimpleName() + "-context.xml" },
new Object[] { RouterMBeanTests.class.getSimpleName() + "None-context.xml" },
new Object[] { RouterMBeanTests.class.getSimpleName() + "Switch-context.xml" });
}
@After
public void close() {
if (context != null) {
context.close();
}
}
@Test
public void testRouterMBeanExists() throws Exception {
// System.err.println(server.queryNames(new ObjectName("test.RouterMBean:*"), null));
Set<ObjectName> names = server.queryNames(new ObjectName("test.RouterMBean:type=MessageHandler,name=ptRouter,*"), null);
Set<ObjectName> names = server.queryNames(
new ObjectName("test.RouterMBean:type=MessageHandler,name=ptRouter,*"), null);
assertEquals(1, names.size());
}

View File

@@ -0,0 +1,25 @@
<?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"
xmlns:jmx="http://www.springframework.org/schema/integration/jmx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/integration/jmx
http://www.springframework.org/schema/integration/jmx/spring-integration-jmx.xsd">
<int:channel id="testChannel" />
<int:channel id="intChannel" />
<int:channel id="stringChannel" />
<int:router id="ptRouter" input-channel="testChannel" expression="payload instanceof String ? 'stringChannel' : 'intChannel'"/>
<context:mbean-server id="mbs" />
<!-- Same as RouterMBeanTests-context.xml but no Core MBeanExporter -->
<jmx:mbean-export server="mbs" default-domain="test.RouterMBean" />
</beans>

View File

@@ -0,0 +1,25 @@
<?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"
xmlns:jmx="http://www.springframework.org/schema/integration/jmx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/integration/jmx
http://www.springframework.org/schema/integration/jmx/spring-integration-jmx.xsd">
<int:channel id="testChannel" />
<int:channel id="intChannel" />
<int:channel id="stringChannel" />
<int:router id="ptRouter" input-channel="testChannel" expression="payload instanceof String ? 'stringChannel' : 'intChannel'"/>
<!-- Same as RouterMBeanTests-context.xml but switched order of MBeanExporters -->
<jmx:mbean-export server="mbs" default-domain="test.RouterMBean" />
<context:mbean-server id="mbs" />
</beans>