MethodInvokingTarget no longer extends AbstractMessageHandler.

This commit is contained in:
Mark Fisher
2008-09-02 20:23:20 +00:00
parent 457a85e17a
commit 30c244aefb
4 changed files with 32 additions and 49 deletions

View File

@@ -23,7 +23,6 @@ import org.springframework.beans.factory.config.BeanDefinitionHolder;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionReaderUtils;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.xml.AbstractBeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.integration.ConfigurationException;
@@ -68,7 +67,10 @@ public class ChannelAdapterParser extends AbstractBeanDefinitionParser {
throw new ConfigurationException("both 'source' and 'target' are not allowed, provide only one");
}
if (StringUtils.hasText(methodName)) {
source = parseMethodInvokingAdapter(source, methodName, MethodInvokingSource.class, parserContext.getRegistry());
BeanDefinitionBuilder invokerBuilder = BeanDefinitionBuilder.genericBeanDefinition(MethodInvokingSource.class);
invokerBuilder.addPropertyReference("object", source);
invokerBuilder.addPropertyValue("methodName", methodName);
source = BeanDefinitionReaderUtils.registerWithGeneratedName(invokerBuilder.getBeanDefinition(), parserContext.getRegistry());
}
adapterBuilder = BeanDefinitionBuilder.genericBeanDefinition(InboundChannelAdapter.class);
if (pollerElement != null) {
@@ -88,7 +90,10 @@ public class ChannelAdapterParser extends AbstractBeanDefinitionParser {
}
else if (StringUtils.hasText(target)) {
if (StringUtils.hasText(methodName)) {
target = this.parseMethodInvokingAdapter(target, methodName, MethodInvokingTarget.class, parserContext.getRegistry());
BeanDefinitionBuilder invokerBuilder = BeanDefinitionBuilder.genericBeanDefinition(MethodInvokingTarget.class);
invokerBuilder.addConstructorArgReference(target);
invokerBuilder.addConstructorArgValue(methodName);
target = BeanDefinitionReaderUtils.registerWithGeneratedName(invokerBuilder.getBeanDefinition(), parserContext.getRegistry());
}
adapterBuilder = BeanDefinitionBuilder.genericBeanDefinition(OutboundChannelAdapter.class);
adapterBuilder.addPropertyReference("target", target);
@@ -113,13 +118,6 @@ public class ChannelAdapterParser extends AbstractBeanDefinitionParser {
return adapterBuilder.getBeanDefinition();
}
private String parseMethodInvokingAdapter(String objectRef, String methodName, Class<?> type, BeanDefinitionRegistry registry) {
BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(type);
builder.addPropertyReference("object", objectRef);
builder.addPropertyValue("methodName", methodName);
return BeanDefinitionReaderUtils.registerWithGeneratedName(builder.getBeanDefinition(), registry);
}
private String createDirectChannel(Element element, ParserContext parserContext) {
String channelId = element.getAttribute("id");
if (!StringUtils.hasText(channelId)) {

View File

@@ -47,9 +47,7 @@ public class TargetAnnotationPostProcessor extends AbstractAnnotationMethodPostP
protected MessageTarget processMethod(Object bean, Method method, Annotation annotation) {
MethodInvokingTarget target = new MethodInvokingTarget();
target.setObject(bean);
target.setMethod(method);
MethodInvokingTarget target = new MethodInvokingTarget(bean, method);
ChannelAdapter channelAdapterAnnotation = AnnotationUtils.findAnnotation(bean.getClass(), ChannelAdapter.class);
if (channelAdapterAnnotation != null) {
OutboundChannelAdapter adapter = new OutboundChannelAdapter();

View File

@@ -16,7 +16,10 @@
package org.springframework.integration.handler;
import java.lang.reflect.Method;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.MessageMappingMethodInvoker;
import org.springframework.integration.message.MessageTarget;
import org.springframework.integration.message.MessagingException;
@@ -25,30 +28,24 @@ import org.springframework.integration.message.MessagingException;
*
* @author Mark Fisher
*/
public class MethodInvokingTarget extends AbstractMessageHandler implements MessageTarget {
public class MethodInvokingTarget extends MessageMappingMethodInvoker implements MessageTarget {
public MethodInvokingTarget(Object object, Method method) {
super(object, method);
}
public MethodInvokingTarget(Object object, String methodName) {
super(object, methodName);
}
public boolean send(Message<?> message) {
this.handle(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 + "]");
}
return true;
}
@Override
protected Message<?> createReplyMessage(Object result, Message<?> requestMessage) {
this.failIfNotNull(result, requestMessage);
return null;
}
@Override
protected Message<?> postProcessReplyMessage(Message<?> replyMessage, Message<?> requestMessage) {
this.failIfNotNull(replyMessage, requestMessage);
return null;
}
private void failIfNotNull(Object result, Message<?> requestMessage) {
if (result != null) {
throw new MessagingException(requestMessage, "the MethodInvokingTarget method must have a void or null return, "
+ "but '" + this + "' received a non-null value: [" + result + "]");
}
}
}

View File

@@ -44,9 +44,7 @@ public class MethodInvokingTargetTests {
@Test
public void testValidMethod() {
MethodInvokingTarget target = new MethodInvokingTarget();
target.setObject(new TestSink());
target.setMethodName("validMethod");
MethodInvokingTarget target = new MethodInvokingTarget(new TestSink(), "validMethod");
target.afterPropertiesSet();
boolean result = target.send(new GenericMessage<String>("test"));
assertTrue(result);
@@ -54,9 +52,7 @@ public class MethodInvokingTargetTests {
@Test(expected = ConfigurationException.class)
public void testInvalidMethodWithNoArgs() {
MethodInvokingTarget target = new MethodInvokingTarget();
target.setObject(new TestSink());
target.setMethodName("invalidMethodWithNoArgs");
MethodInvokingTarget target = new MethodInvokingTarget(new TestSink(), "invalidMethodWithNoArgs");
target.afterPropertiesSet();
}
@@ -64,9 +60,7 @@ public class MethodInvokingTargetTests {
public void testMethodWithReturnValue() {
Message<?> message = new StringMessage("test");
try {
MethodInvokingTarget target = new MethodInvokingTarget();
target.setObject(new TestSink());
target.setMethodName("methodWithReturnValue");
MethodInvokingTarget target = new MethodInvokingTarget(new TestSink(), "methodWithReturnValue");
target.afterPropertiesSet();
target.send(message);
}
@@ -78,9 +72,7 @@ public class MethodInvokingTargetTests {
@Test(expected = ConfigurationException.class)
public void testNoMatchingMethodName() {
MethodInvokingTarget target = new MethodInvokingTarget();
target.setObject(new TestSink());
target.setMethodName("noSuchMethod");
MethodInvokingTarget target = new MethodInvokingTarget(new TestSink(), "noSuchMethod");
target.afterPropertiesSet();
}
@@ -88,9 +80,7 @@ public class MethodInvokingTargetTests {
public void testSubscription() throws Exception {
SynchronousQueue<String> queue = new SynchronousQueue<String>();
TestBean testBean = new TestBean(queue);
MethodInvokingTarget target = new MethodInvokingTarget();
target.setObject(testBean);
target.setMethodName("foo");
MethodInvokingTarget target = new MethodInvokingTarget(testBean, "foo");
target.afterPropertiesSet();
QueueChannel channel = new QueueChannel();
channel.setBeanName("channel");