Refactored MethodInvokingTarget to MethodInvokingConsumer including changes to OutboundChannelAdapter.
This commit is contained in:
@@ -29,7 +29,7 @@ import org.springframework.integration.ConfigurationException;
|
||||
import org.springframework.integration.channel.DirectChannel;
|
||||
import org.springframework.integration.endpoint.SourcePollingChannelAdapter;
|
||||
import org.springframework.integration.endpoint.OutboundChannelAdapter;
|
||||
import org.springframework.integration.handler.MethodInvokingTarget;
|
||||
import org.springframework.integration.handler.MethodInvokingConsumer;
|
||||
import org.springframework.integration.message.MethodInvokingSource;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.util.xml.DomUtils;
|
||||
@@ -91,7 +91,7 @@ public class ChannelAdapterParser extends AbstractBeanDefinitionParser {
|
||||
}
|
||||
else if (StringUtils.hasText(target)) {
|
||||
if (StringUtils.hasText(methodName)) {
|
||||
BeanDefinitionBuilder invokerBuilder = BeanDefinitionBuilder.genericBeanDefinition(MethodInvokingTarget.class);
|
||||
BeanDefinitionBuilder invokerBuilder = BeanDefinitionBuilder.genericBeanDefinition(MethodInvokingConsumer.class);
|
||||
invokerBuilder.addConstructorArgReference(target);
|
||||
invokerBuilder.addConstructorArgValue(methodName);
|
||||
target = BeanDefinitionReaderUtils.registerWithGeneratedName(invokerBuilder.getBeanDefinition(), parserContext.getRegistry());
|
||||
|
||||
@@ -30,7 +30,7 @@ import org.springframework.integration.channel.PollableChannel;
|
||||
import org.springframework.integration.endpoint.MessageEndpoint;
|
||||
import org.springframework.integration.endpoint.OutboundChannelAdapter;
|
||||
import org.springframework.integration.endpoint.SourcePollingChannelAdapter;
|
||||
import org.springframework.integration.handler.MethodInvokingTarget;
|
||||
import org.springframework.integration.handler.MethodInvokingConsumer;
|
||||
import org.springframework.integration.message.MethodInvokingSource;
|
||||
import org.springframework.integration.scheduling.PollingSchedule;
|
||||
import org.springframework.integration.scheduling.Schedule;
|
||||
@@ -76,8 +76,8 @@ public class ChannelAdapterAnnotationPostProcessor implements MethodAnnotationPo
|
||||
endpoint = this.createInboundChannelAdapter(source, channel, pollerAnnotation);
|
||||
}
|
||||
else if (method.getParameterTypes().length > 0 && !hasReturnValue(method)) {
|
||||
MethodInvokingTarget target = new MethodInvokingTarget(bean, method);
|
||||
endpoint = this.createOutboundChannelAdapter(target, channel, pollerAnnotation);
|
||||
MethodInvokingConsumer consumer = new MethodInvokingConsumer(bean, method);
|
||||
endpoint = this.createOutboundChannelAdapter(consumer, channel, pollerAnnotation);
|
||||
}
|
||||
else {
|
||||
throw new ConfigurationException("The @ChannelAdapter can only be applied to methods that accept no arguments but have"
|
||||
@@ -104,8 +104,8 @@ public class ChannelAdapterAnnotationPostProcessor implements MethodAnnotationPo
|
||||
return adapter;
|
||||
}
|
||||
|
||||
private OutboundChannelAdapter createOutboundChannelAdapter(MethodInvokingTarget target, MessageChannel channel, Poller pollerAnnotation) {
|
||||
OutboundChannelAdapter adapter = new OutboundChannelAdapter(target);
|
||||
private OutboundChannelAdapter createOutboundChannelAdapter(MethodInvokingConsumer consumer, MessageChannel channel, Poller pollerAnnotation) {
|
||||
OutboundChannelAdapter adapter = new OutboundChannelAdapter(consumer);
|
||||
adapter.setInputChannel(channel);
|
||||
if (channel instanceof PollableChannel) {
|
||||
Schedule schedule = (pollerAnnotation != null)
|
||||
|
||||
@@ -18,32 +18,29 @@ package org.springframework.integration.endpoint;
|
||||
|
||||
import org.springframework.integration.channel.MessageChannel;
|
||||
import org.springframework.integration.message.Message;
|
||||
import org.springframework.integration.message.MessageDeliveryException;
|
||||
import org.springframework.integration.message.MessageTarget;
|
||||
import org.springframework.integration.message.MessageConsumer;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* A Channel Adapter implementation for connecting a {@link MessageChannel}
|
||||
* to a {@link org.springframework.integration.message.MessageTarget}.
|
||||
* to a {@link org.springframework.integration.message.MessageConsumer}.
|
||||
*
|
||||
* @author Mark Fisher
|
||||
*/
|
||||
public class OutboundChannelAdapter extends AbstractMessageConsumingEndpoint {
|
||||
|
||||
private final MessageTarget target;
|
||||
private final MessageConsumer consumer;
|
||||
|
||||
|
||||
public OutboundChannelAdapter(MessageTarget target) {
|
||||
Assert.notNull(target, "target must not be null");
|
||||
this.target = target;
|
||||
public OutboundChannelAdapter(MessageConsumer consumer) {
|
||||
Assert.notNull(consumer, "consumer must not be null");
|
||||
this.consumer = consumer;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected void onMessageInternal(Message<?> message) {
|
||||
if (!this.target.send(message)) {
|
||||
throw new MessageDeliveryException(message, "failed to deliver Message to target");
|
||||
}
|
||||
this.consumer.onMessage(message);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -19,33 +19,35 @@ package org.springframework.integration.handler;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
import org.springframework.integration.message.Message;
|
||||
import org.springframework.integration.message.MessageConsumer;
|
||||
import org.springframework.integration.message.MessageMappingMethodInvoker;
|
||||
import org.springframework.integration.message.MessageTarget;
|
||||
import org.springframework.integration.message.MessagingException;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* A messaging target that invokes the specified method on the provided object.
|
||||
* A {@link MessageConsumer} that invokes the specified method on the provided object.
|
||||
*
|
||||
* @author Mark Fisher
|
||||
*/
|
||||
public class MethodInvokingTarget extends MessageMappingMethodInvoker implements MessageTarget {
|
||||
public class MethodInvokingConsumer extends MessageMappingMethodInvoker implements MessageConsumer {
|
||||
|
||||
public MethodInvokingTarget(Object object, Method method) {
|
||||
public MethodInvokingConsumer(Object object, Method method) {
|
||||
super(object, method);
|
||||
Assert.isTrue(method.getReturnType().equals(void.class),
|
||||
"MethodInvokingConsumer requires a void-returning method");
|
||||
}
|
||||
|
||||
public MethodInvokingTarget(Object object, String methodName) {
|
||||
public MethodInvokingConsumer(Object object, String methodName) {
|
||||
super(object, methodName);
|
||||
}
|
||||
|
||||
|
||||
public boolean send(Message<?> message) {
|
||||
public void onMessage(Message<?> message) {
|
||||
Object result = this.invokeMethod(message);
|
||||
if (result != null) {
|
||||
throw new MessagingException(message, "the MethodInvokingTarget method must have a void or null return, "
|
||||
+ "but '" + this + "' received a non-null value: [" + result + "]");
|
||||
throw new MessagingException(message, "the MethodInvokingConsumer method must have a void return, "
|
||||
+ "but '" + this + "' received a value: [" + result + "]");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -13,15 +13,15 @@
|
||||
<queue capacity="10"/>
|
||||
</channel>
|
||||
|
||||
<channel-adapter id="outboundWithImplicitChannel" target="target"/>
|
||||
<channel-adapter id="outboundWithImplicitChannel" target="consumer"/>
|
||||
|
||||
<channel-adapter id="methodInvokingTarget" target="testBean" method="store"/>
|
||||
<channel-adapter id="methodInvokingConsumer" target="testBean" method="store"/>
|
||||
|
||||
<channel-adapter id="methodInvokingSource" source="testBean" method="getMessage" channel="queueChannel">
|
||||
<poller period="10000" max-messages-per-poll="1"/>
|
||||
</channel-adapter>
|
||||
|
||||
<beans:bean id="target" class="org.springframework.integration.config.TestTarget"/>
|
||||
<beans:bean id="consumer" class="org.springframework.integration.config.TestConsumer"/>
|
||||
|
||||
<beans:bean id="testBean" class="org.springframework.integration.config.TestBean"/>
|
||||
|
||||
|
||||
@@ -51,18 +51,18 @@ public class ChannelAdapterParserTests extends AbstractJUnit4SpringContextTests
|
||||
Object adapter = this.applicationContext.getBean(beanName + ".adapter");
|
||||
assertNotNull(adapter);
|
||||
assertTrue(adapter instanceof OutboundChannelAdapter);
|
||||
TestTarget target = (TestTarget) this.applicationContext.getBean("target");
|
||||
assertNull(target.getLastMessage());
|
||||
TestConsumer consumer = (TestConsumer) this.applicationContext.getBean("consumer");
|
||||
assertNull(consumer.getLastMessage());
|
||||
Message<?> message = new StringMessage("test");
|
||||
assertTrue(((MessageChannel) channel).send(message));
|
||||
assertNotNull(target.getLastMessage());
|
||||
assertEquals(message, target.getLastMessage());
|
||||
assertNotNull(consumer.getLastMessage());
|
||||
assertEquals(message, consumer.getLastMessage());
|
||||
bus.stop();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void methodInvokingTarget() {
|
||||
String beanName = "methodInvokingTarget";
|
||||
public void methodInvokingConsumer() {
|
||||
String beanName = "methodInvokingConsumer";
|
||||
Object channel = this.applicationContext.getBean(beanName);
|
||||
assertTrue(channel instanceof DirectChannel);
|
||||
MessageBus bus = (MessageBus) this.applicationContext.getBean(MessageBusParser.MESSAGE_BUS_BEAN_NAME);
|
||||
@@ -73,10 +73,10 @@ public class ChannelAdapterParserTests extends AbstractJUnit4SpringContextTests
|
||||
assertTrue(adapter instanceof OutboundChannelAdapter);
|
||||
TestBean testBean = (TestBean) this.applicationContext.getBean("testBean");
|
||||
assertNull(testBean.getMessage());
|
||||
Message<?> message = new StringMessage("target test");
|
||||
Message<?> message = new StringMessage("consumer test");
|
||||
assertTrue(((MessageChannel) channel).send(message));
|
||||
assertNotNull(testBean.getMessage());
|
||||
assertEquals("target test", testBean.getMessage());
|
||||
assertEquals("consumer test", testBean.getMessage());
|
||||
bus.stop();
|
||||
}
|
||||
|
||||
|
||||
@@ -17,12 +17,12 @@
|
||||
package org.springframework.integration.config;
|
||||
|
||||
import org.springframework.integration.message.Message;
|
||||
import org.springframework.integration.message.MessageTarget;
|
||||
import org.springframework.integration.message.MessageConsumer;
|
||||
|
||||
/**
|
||||
* @author Mark Fisher
|
||||
*/
|
||||
public class TestTarget implements MessageTarget {
|
||||
public class TestConsumer implements MessageConsumer {
|
||||
|
||||
private volatile Message<?> lastMessage;
|
||||
|
||||
@@ -31,9 +31,8 @@ public class TestTarget implements MessageTarget {
|
||||
return this.lastMessage;
|
||||
}
|
||||
|
||||
public boolean send(Message<?> message) {
|
||||
public void onMessage(Message<?> message) {
|
||||
this.lastMessage = message;
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -19,7 +19,6 @@ package org.springframework.integration.handler;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.concurrent.BlockingQueue;
|
||||
import java.util.concurrent.SynchronousQueue;
|
||||
@@ -40,29 +39,28 @@ import org.springframework.integration.message.StringMessage;
|
||||
/**
|
||||
* @author Mark Fisher
|
||||
*/
|
||||
public class MethodInvokingTargetTests {
|
||||
public class MethodInvokingConsumerTests {
|
||||
|
||||
@Test
|
||||
public void testValidMethod() {
|
||||
MethodInvokingTarget target = new MethodInvokingTarget(new TestSink(), "validMethod");
|
||||
target.afterPropertiesSet();
|
||||
boolean result = target.send(new GenericMessage<String>("test"));
|
||||
assertTrue(result);
|
||||
MethodInvokingConsumer consumer = new MethodInvokingConsumer(new TestSink(), "validMethod");
|
||||
consumer.afterPropertiesSet();
|
||||
consumer.onMessage(new GenericMessage<String>("test"));
|
||||
}
|
||||
|
||||
@Test(expected = ConfigurationException.class)
|
||||
public void testInvalidMethodWithNoArgs() {
|
||||
MethodInvokingTarget target = new MethodInvokingTarget(new TestSink(), "invalidMethodWithNoArgs");
|
||||
target.afterPropertiesSet();
|
||||
MethodInvokingConsumer consumer = new MethodInvokingConsumer(new TestSink(), "invalidMethodWithNoArgs");
|
||||
consumer.afterPropertiesSet();
|
||||
}
|
||||
|
||||
@Test(expected = MessagingException.class)
|
||||
public void testMethodWithReturnValue() {
|
||||
Message<?> message = new StringMessage("test");
|
||||
try {
|
||||
MethodInvokingTarget target = new MethodInvokingTarget(new TestSink(), "methodWithReturnValue");
|
||||
target.afterPropertiesSet();
|
||||
target.send(message);
|
||||
MethodInvokingConsumer consumer = new MethodInvokingConsumer(new TestSink(), "methodWithReturnValue");
|
||||
consumer.afterPropertiesSet();
|
||||
consumer.onMessage(message);
|
||||
}
|
||||
catch (MessagingException e) {
|
||||
assertEquals(e.getFailedMessage(), message);
|
||||
@@ -72,8 +70,8 @@ public class MethodInvokingTargetTests {
|
||||
|
||||
@Test(expected = ConfigurationException.class)
|
||||
public void testNoMatchingMethodName() {
|
||||
MethodInvokingTarget target = new MethodInvokingTarget(new TestSink(), "noSuchMethod");
|
||||
target.afterPropertiesSet();
|
||||
MethodInvokingConsumer consumer = new MethodInvokingConsumer(new TestSink(), "noSuchMethod");
|
||||
consumer.afterPropertiesSet();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -81,15 +79,15 @@ public class MethodInvokingTargetTests {
|
||||
GenericApplicationContext context = new GenericApplicationContext();
|
||||
SynchronousQueue<String> queue = new SynchronousQueue<String>();
|
||||
TestBean testBean = new TestBean(queue);
|
||||
MethodInvokingTarget target = new MethodInvokingTarget(testBean, "foo");
|
||||
target.afterPropertiesSet();
|
||||
MethodInvokingConsumer consumer = new MethodInvokingConsumer(testBean, "foo");
|
||||
consumer.afterPropertiesSet();
|
||||
QueueChannel channel = new QueueChannel();
|
||||
channel.setBeanName("channel");
|
||||
context.getBeanFactory().registerSingleton("channel", channel);
|
||||
Message<String> message = new GenericMessage<String>("testing");
|
||||
channel.send(message);
|
||||
assertNull(queue.poll());
|
||||
ServiceActivatorEndpoint endpoint = new ServiceActivatorEndpoint(target);
|
||||
ServiceActivatorEndpoint endpoint = new ServiceActivatorEndpoint(consumer);
|
||||
endpoint.setBeanName("testEndpoint");
|
||||
endpoint.setInputChannel(channel);
|
||||
context.getBeanFactory().registerSingleton("testEndpoint", endpoint);
|
||||
Reference in New Issue
Block a user