Refactored message dispatchers, executors, and the message bus to support target adapters in addition to message endpoints.

This commit is contained in:
Mark Fisher
2007-12-28 21:41:59 +00:00
parent 40a44a7e25
commit 17550340df
27 changed files with 514 additions and 535 deletions

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.integration.endpoint;
package org.springframework.integration.adapter;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;

View File

@@ -0,0 +1,68 @@
/*
* Copyright 2002-2007 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.adapter;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import org.springframework.integration.MessageDeliveryException;
/**
* @author Mark Fisher
*/
public class MethodInvokingTargetTests {
@Test
public void testValidMethod() {
MethodInvokingTarget<TestSink> target = new MethodInvokingTarget<TestSink>();
target.setObject(new TestSink());
target.setMethod("validMethod");
target.afterPropertiesSet();
boolean result = target.send("test");
assertTrue(result);
}
@Test(expected=MessageDeliveryException.class)
public void testInvalidMethodWithNoArgs() {
MethodInvokingTarget<TestSink> target = new MethodInvokingTarget<TestSink>();
target.setObject(new TestSink());
target.setMethod("invalidMethodWithNoArgs");
target.afterPropertiesSet();
target.send("test");
}
@Test
public void testValidMethodWithIgnoredReturnValue() {
MethodInvokingTarget<TestSink> target = new MethodInvokingTarget<TestSink>();
target.setObject(new TestSink());
target.setMethod("validMethodWithIgnoredReturnValue");
target.afterPropertiesSet();
boolean result = target.send("test");
assertTrue(result);
}
@Test(expected=MessageDeliveryException.class)
public void testNoMatchingMethodName() {
MethodInvokingTarget<TestSink> target = new MethodInvokingTarget<TestSink>();
target.setObject(new TestSink());
target.setMethod("noSuchMethod");
target.afterPropertiesSet();
target.send("test");
}
}

View File

@@ -43,7 +43,7 @@ public class PollingSourceAdapterTests {
PollingSourceAdapter<String> adapter = new PollingSourceAdapter<String>(source);
adapter.setChannel(channel);
adapter.setPeriod(100);
adapter.receiveAndDispatch();
adapter.dispatch();
Message<?> message = channel.receive();
assertNotNull("message should not be null", message);
assertEquals("testing.1", message.getPayload());
@@ -57,14 +57,14 @@ public class PollingSourceAdapterTests {
adapter.setChannel(channel);
adapter.setPeriod(500);
adapter.setSendTimeout(10);
adapter.receiveAndDispatch();
adapter.receiveAndDispatch();
adapter.dispatch();
adapter.dispatch();
Message<?> message1 = channel.receive();
assertNotNull("message should not be null", message1);
assertEquals("testing.1", message1.getPayload());
Message<?> message2 = channel.receive(0);
assertNull("second message should be null", message2);
adapter.receiveAndDispatch();
adapter.dispatch();
Message<?> message3 = channel.receive(0);
assertNotNull("third message should not be null", message3);
assertEquals("testing.3", message3.getPayload());
@@ -78,7 +78,7 @@ public class PollingSourceAdapterTests {
adapter.setChannel(channel);
adapter.setPeriod(1000);
adapter.setMaxMessagesPerTask(5);
adapter.receiveAndDispatch();
adapter.dispatch();
Message<?> message1 = channel.receive(0);
assertNotNull("message should not be null", message1);
assertEquals("testing.1", message1.getPayload());
@@ -100,7 +100,7 @@ public class PollingSourceAdapterTests {
adapter.setChannel(channel);
adapter.setPeriod(1000);
adapter.setMaxMessagesPerTask(2);
adapter.receiveAndDispatch();
adapter.dispatch();
}

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.integration.endpoint;
package org.springframework.integration.adapter;
/**
* @author Mark Fisher

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.integration.endpoint;
package org.springframework.integration.adapter;
/**
* @author Mark Fisher

View File

@@ -0,0 +1,47 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="bus" class="org.springframework.integration.bus.MessageBus"/>
<bean id="inputChannel" class="org.springframework.integration.channel.PointToPointChannel"/>
<bean id="outputChannel" class="org.springframework.integration.channel.PointToPointChannel"/>
<bean id="sourceAdapter" class="org.springframework.integration.adapter.PollingSourceAdapter">
<constructor-arg>
<bean class="org.springframework.integration.adapter.MethodInvokingSource">
<property name="object">
<bean class="org.springframework.integration.adapter.TestSource"/>
</property>
<property name="method" value="foo"/>
</bean>
</constructor-arg>
<property name="channel" ref="inputChannel"/>
</bean>
<bean id="targetAdapter" class="org.springframework.integration.adapter.DefaultTargetAdapter">
<constructor-arg>
<bean class="org.springframework.integration.adapter.MethodInvokingTarget">
<property name="object" ref="sink"/>
<property name="method" value="store"/>
</bean>
</constructor-arg>
<property name="channel" ref="outputChannel"/>
</bean>
<bean id="sink" class="org.springframework.integration.adapter.TestSink"/>
<bean id="endpoint" class="org.springframework.integration.endpoint.GenericMessageEndpoint">
<property name="inputChannelName" value="inputChannel"/>
<property name="defaultOutputChannelName" value="outputChannel"/>
<property name="consumerPolicy">
<bean class="org.springframework.integration.bus.ConsumerPolicy">
<property name="receiveTimeout" value="100"/>
</bean>
</property>
</bean>
</beans>

View File

@@ -59,9 +59,9 @@ public class UnicastMessageDispatcherTests {
channel.send(new StringMessage(1, "test"));
MessageRetriever retriever = new ChannelPollingMessageRetriever(channel, policy);
UnicastMessageDispatcher dispatcher = new UnicastMessageDispatcher(retriever, policy);
dispatcher.addEndpointExecutor(new EndpointExecutor(endpoint1, 1, 1));
dispatcher.addEndpointExecutor(new EndpointExecutor(endpoint2, 1, 1));
dispatcher.receiveAndDispatch();
dispatcher.addExecutor(new MessageReceivingExecutor(endpoint1, 1, 1));
dispatcher.addExecutor(new MessageReceivingExecutor(endpoint2, 1, 1));
dispatcher.dispatch();
latch.await(500, TimeUnit.MILLISECONDS);
assertTrue("exactly one endpoint should have received message",
endpoint1Received.get() ^ endpoint2Received.get());

View File

@@ -1,100 +0,0 @@
/*
* Copyright 2002-2007 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.config;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.integration.bus.MessageBus;
import org.springframework.integration.channel.MessageChannel;
import org.springframework.integration.channel.PointToPointChannel;
import org.springframework.integration.endpoint.MessageEndpoint;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.GenericMessage;
/**
* @author Mark Fisher
*/
public class ComponentConfigurerTests {
@Test
public void testServiceActivator() {
GenericApplicationContext context = new GenericApplicationContext();
ComponentConfigurer cr = new ComponentConfigurer(context, null);
context.registerBeanDefinition("tester", new RootBeanDefinition(Tester.class));
context.registerBeanDefinition("out", new RootBeanDefinition(PointToPointChannel.class));
context.registerBeanDefinition("bus", new RootBeanDefinition(MessageBus.class));
String name = cr.serviceActivator(null, "out", "tester", "test");
context.refresh();
MessageEndpoint endpoint = (MessageEndpoint) context.getBean(name);
endpoint.messageReceived(new GenericMessage<String>(1, "world"));
MessageChannel channel = (MessageChannel) context.getBean("out");
assertEquals("hello world", channel.receive().getPayload());
}
@Test
public void testInboundChannelAdapter() {
GenericApplicationContext context = new GenericApplicationContext();
ComponentConfigurer cr = new ComponentConfigurer(context, null);
context.registerBeanDefinition("tester", new RootBeanDefinition(Tester.class));
String adapter = cr.inboundChannelAdapter("tester", "foo");
MessageChannel channel = (MessageChannel) context.getBean(adapter);
Message<String> message = channel.receive();
assertEquals("bar", message.getPayload());
}
@Test
public void testOutboundChannelAdapter() {
GenericApplicationContext context = new GenericApplicationContext();
ComponentConfigurer cr = new ComponentConfigurer(context, null);
context.registerBeanDefinition("tester", new RootBeanDefinition(Tester.class));
String adapter = cr.outboundChannelAdapter("tester", "store");
Tester tester = (Tester) context.getBean("tester");
assertNull(tester.getStoredValue());
MessageChannel channel = (MessageChannel) context.getBean(adapter);
boolean result = channel.send(new GenericMessage<String>(1, "foo"));
assertTrue(result);
assertEquals("foo", tester.getStoredValue());
}
public static class Tester {
private String storedValue;
public String test(String s) {
return "hello " + s;
}
public String foo() {
return "bar";
}
public void store(String s) {
this.storedValue = s;
}
public String getStoredValue() {
return this.storedValue;
}
}
}

View File

@@ -1,73 +0,0 @@
/*
* Copyright 2002-2007 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.endpoint;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.springframework.integration.MessageHandlingException;
import org.springframework.integration.endpoint.InboundMethodInvokingChannelAdapter;
import org.springframework.integration.message.Message;
/**
* @author Mark Fisher
*/
public class InboundChannelAdapterTests {
@Test
public void testValidMethod() {
InboundMethodInvokingChannelAdapter<TestSource> adapter =
new InboundMethodInvokingChannelAdapter<TestSource>();
adapter.setObject(new TestSource());
adapter.setMethod("validMethod");
adapter.afterPropertiesSet();
Message message = adapter.receive();
assertEquals("valid", message.getPayload());
}
@Test(expected=MessageHandlingException.class)
public void testInvalidMethodWithArg() {
InboundMethodInvokingChannelAdapter<TestSource> adapter =
new InboundMethodInvokingChannelAdapter<TestSource>();
adapter.setObject(new TestSource());
adapter.setMethod("invalidMethodWithArg");
adapter.afterPropertiesSet();
adapter.receive();
}
@Test(expected=MessageHandlingException.class)
public void testInvalidMethodWithNoReturnValue() {
InboundMethodInvokingChannelAdapter<TestSource> adapter =
new InboundMethodInvokingChannelAdapter<TestSource>();
adapter.setObject(new TestSource());
adapter.setMethod("invalidMethodWithNoReturnValue");
adapter.afterPropertiesSet();
adapter.receive();
}
@Test(expected=MessageHandlingException.class)
public void testNoMatchingMethodName() {
InboundMethodInvokingChannelAdapter<TestSource> adapter =
new InboundMethodInvokingChannelAdapter<TestSource>();
adapter.setObject(new TestSource());
adapter.setMethod("noSuchMethod");
adapter.afterPropertiesSet();
adapter.receive();
}
}

View File

@@ -1,74 +0,0 @@
/*
* Copyright 2002-2007 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.endpoint;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import org.springframework.integration.MessageHandlingException;
import org.springframework.integration.endpoint.OutboundMethodInvokingChannelAdapter;
import org.springframework.integration.message.StringMessage;
/**
* @author Mark Fisher
*/
public class OutboundChannelAdapterTests {
@Test
public void testValidMethod() {
OutboundMethodInvokingChannelAdapter<TestSink> adapter =
new OutboundMethodInvokingChannelAdapter<TestSink>();
adapter.setObject(new TestSink());
adapter.setMethod("validMethod");
adapter.afterPropertiesSet();
boolean result = adapter.send(new StringMessage(1, "test"));
assertTrue(result);
}
@Test(expected=MessageHandlingException.class)
public void testInvalidMethodWithNoArgs() {
OutboundMethodInvokingChannelAdapter<TestSink> adapter =
new OutboundMethodInvokingChannelAdapter<TestSink>();
adapter.setObject(new TestSink());
adapter.setMethod("invalidMethodWithNoArgs");
adapter.afterPropertiesSet();
adapter.send(new StringMessage(1, "test"));
}
@Test
public void testValidMethodWithIgnoredReturnValue() {
OutboundMethodInvokingChannelAdapter<TestSink> adapter =
new OutboundMethodInvokingChannelAdapter<TestSink>();
adapter.setObject(new TestSink());
adapter.setMethod("validMethodWithIgnoredReturnValue");
adapter.afterPropertiesSet();
boolean result = adapter.send(new StringMessage(1, "test"));
assertTrue(result);
}
@Test(expected=MessageHandlingException.class)
public void testNoMatchingMethodName() {
OutboundMethodInvokingChannelAdapter<TestSink> adapter =
new OutboundMethodInvokingChannelAdapter<TestSink>();
adapter.setObject(new TestSink());
adapter.setMethod("noSuchMethod");
adapter.afterPropertiesSet();
adapter.send(new StringMessage(1, "test"));
}
}

View File

@@ -1,45 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="bus" class="org.springframework.integration.bus.MessageBus"/>
<bean id="inboundAdapter" class="org.springframework.integration.endpoint.InboundMethodInvokingChannelAdapter">
<property name="object">
<bean class="org.springframework.integration.endpoint.TestSource"/>
</property>
<property name="method" value="foo"/>
</bean>
<bean id="outboundAdapter" class="org.springframework.integration.endpoint.OutboundMethodInvokingChannelAdapter">
<property name="object" ref="sink"/>
<property name="method" value="store"/>
</bean>
<bean id="sink" class="org.springframework.integration.endpoint.TestSink"/>
<bean id="endpoint" class="org.springframework.integration.endpoint.GenericMessageEndpoint">
<property name="inputChannelName" value="inboundAdapter"/>
<property name="defaultOutputChannelName" value="outboundAdapter"/>
<property name="consumerPolicy">
<bean class="org.springframework.integration.bus.ConsumerPolicy">
<property name="receiveTimeout" value="100"/>
</bean>
</property>
</bean>
<!--
<bean class="org.springframework.integration.bus.Subscription">
<property name="channel" value="inboundAdapter"/>
<property name="endpoint" value="endpoint"/>
<property name="policy">
<bean class="org.springframework.integration.bus.ConsumerPolicy">
<property name="receiveTimeout" value="100"/>
</bean>
</property>
</bean>
-->
</beans>