INT-1675: work a bit harder to expose existing handlers if they are @ManagedResources

This commit is contained in:
Dave Syer
2010-12-11 08:48:43 +00:00
parent e23e478e9e
commit b528abcf68
17 changed files with 429 additions and 41 deletions

View File

@@ -0,0 +1,22 @@
<?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">
<bean class="java.lang.String" depends-on="router">
<constructor-arg value="dummy" />
</bean>
<context:mbean-server id="mbs" />
<context:mbean-export server="mbs" default-domain="test.MBeanAutoDetectFirst" />
<bean id="router" class="org.springframework.integration.config.RouterFactoryBean">
<property name="expressionString" value="payload instanceof String ? 'stringChannel' : 'intChannel'" />
</bean>
</beans>

View File

@@ -0,0 +1,22 @@
<?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">
<context:mbean-server id="mbs" />
<context:mbean-export server="mbs" default-domain="test.MBeanAutoDetectFirst" />
<bean id="router" class="org.springframework.integration.config.RouterFactoryBean">
<property name="expressionString" value="payload instanceof String ? 'stringChannel' : 'intChannel'" />
</bean>
<bean class="java.lang.String" depends-on="router">
<constructor-arg value="dummy" />
</bean>
</beans>

View File

@@ -0,0 +1,66 @@
/*
* Copyright 2002-2010 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 static org.junit.Assert.assertEquals;
import java.util.Set;
import javax.management.MBeanServer;
import javax.management.ObjectName;
import org.junit.After;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author Dave Syer
* @since 2.0
*/
public class MBeanAutoDetectTests {
private MBeanServer server;
private ClassPathXmlApplicationContext context;
@After
public void close() {
if (context != null) {
context.close();
}
}
@Test
public void testRouterMBeanExistsWhenDefinedFirst() throws Exception {
context = new ClassPathXmlApplicationContext("MBeanAutoDetectFirstTests-context.xml", getClass());
server = context.getBean(MBeanServer.class);
// System.err.println(server.queryNames(new ObjectName("test.MBeanAutoDetectFirst:*"), null));
Set<ObjectName> names = server.queryNames(
new ObjectName("test.MBeanAutoDetectFirst:type=ExpressionEvaluatingRouter,*"), null);
assertEquals(1, names.size());
}
@Test
@Ignore // Fails because the MBeanExporter is created before the router
public void testRouterMBeanExistsWhenDefinedSecond() throws Exception {
context = new ClassPathXmlApplicationContext("MBeanAutoDetectSecondTests-context.xml", getClass());
server = context.getBean(MBeanServer.class);
// System.err.println(server.queryNames(new ObjectName("test.MBeanAutoDetectFirst:*"), null));
Set<ObjectName> names = server.queryNames(
new ObjectName("test.MBeanAutoDetectFirst:type=ExpressionEvaluatingRouter,*"), null);
assertEquals(1, names.size());
}
}

View File

@@ -0,0 +1,29 @@
<?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:gateway id="gateway" service-interface="org.springframework.integration.jmx.config.RouterMBeanTests$Service"
default-request-channel="testChannel" />
<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

@@ -14,11 +14,15 @@
package org.springframework.integration.jmx.config;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.management.MBeanOperationInfo;
import javax.management.MBeanServer;
import javax.management.ObjectName;
@@ -48,9 +52,10 @@ public class RouterMBeanTests {
@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" });
new Object[] { "RouterMBeanTests-context.xml" },
new Object[] { "RouterMBeanGatewayTests-context.xml" },
new Object[] { "RouterMBeanNoneTests-context.xml" },
new Object[] { "RouterMBeanSwitchTests-context.xml" });
}
@After
@@ -68,11 +73,50 @@ public class RouterMBeanTests {
assertEquals(1, names.size());
}
@Test
public void testInputChannelMBeanExists() throws Exception {
// System.err.println(server.queryNames(new ObjectName("test.RouterMBean:type=MessageChannel,*"), null));
Set<ObjectName> names = server.queryNames(
new ObjectName("test.RouterMBean:type=MessageChannel,name=testChannel,*"), null);
assertEquals(1, names.size());
}
@Test
public void testErrorChannelMBeanExists() throws Exception {
Set<ObjectName> names = server.queryNames(
new ObjectName("test.RouterMBean:type=MessageChannel,name=errorChannel,*"), null);
assertEquals(1, names.size());
}
@Test
public void testRouterMBeanOnlyRegisteredOnce() throws Exception {
// System.err.println(server.queryNames(new ObjectName("*:type=MessageHandler,*"), null));
// The errorLogger and the router
assertEquals(2, server.queryNames(new ObjectName("test.RouterMBean:type=MessageHandler,*"), null).size());
Set<ObjectName> names = server.queryNames(new ObjectName("test.RouterMBean:type=MessageHandler,name=ptRouter,*"), null);
assertEquals(1, names.size());
}
@Test
public void testRouterMBeanHasTrackableComponent() throws Exception {
// System.err.println(server.queryNames(new ObjectName("test.RouterMBean:*"), null));
Set<ObjectName> names = server.queryNames(new ObjectName("test.RouterMBean:type=ExpressionEvaluatingRouter,*"), null);
Map<String,MBeanOperationInfo> infos = new HashMap<String, MBeanOperationInfo>();
for (MBeanOperationInfo info : server.getMBeanInfo(names.iterator().next()).getOperations()) {
infos.put(info.getName(), info);
}
// System.err.println(infos);
assertNotNull(infos.get("setShouldTrack"));
}
@Test
public void testVanillaRouterMBeanRegistration() throws Exception {
// System.err.println(server.queryNames(new ObjectName("test.RouterMBean:*"), null));
Set<ObjectName> names = server.queryNames(new ObjectName("test.RouterMBean:type=ExpressionEvaluatingRouter,*"), null);
// The router is exposed...
assertEquals(1, names.size());
}
public static interface Service {
void send(String input);
}
}

View File

@@ -0,0 +1,24 @@
<?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" />
<context:mbean-export server="mbs" default-domain="test.RouterMBeanVanilla" />
</beans>

View File

@@ -0,0 +1,54 @@
/*
* Copyright 2002-2010 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 static org.junit.Assert.assertEquals;
import java.util.Set;
import javax.management.MBeanServer;
import javax.management.ObjectName;
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;
/**
* @author Dave Syer
* @since 2.0
*/
@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
public class RouterMBeanVanillaTests {
@Autowired
private MBeanServer server;
@Test
public void testHandlerMBeanRegistration() throws Exception {
// System.err.println(server.queryNames(new ObjectName("test.RouterMBeanVanilla:*"), null));
Set<ObjectName> names = server.queryNames(new ObjectName("test.RouterMBeanVanilla:type=ExpressionEvaluatingRouter,*"), null);
// The router is exposed...
assertEquals(1, names.size());
}
public static class Source {
public String get() {
return "foo";
}
}
}