MethodInvokingTarget now extends AbstractMessageHandler instead of AbstractMessageHandlerAdapter. SplitterMessageHandlerCreator no longer requires configuration of the "outputChannel" property.

This commit is contained in:
Mark Fisher
2008-08-12 11:30:18 +00:00
parent 178c438159
commit 56f5a9374c
4 changed files with 22 additions and 28 deletions

View File

@@ -100,7 +100,7 @@ public abstract class AbstractMessageHandler implements MessageHandler, Initiali
public void setMethod(Method method) {
Assert.notNull(method, "method must not be null");
if (method.getParameterTypes().length == 0) {
throw new IllegalArgumentException("method must accept at least one parameter");
throw new ConfigurationException("method must accept at least one parameter");
}
if (method.getParameterTypes()[0].equals(Message.class)) {
this.methodExpectsMessage = true;

View File

@@ -17,15 +17,15 @@
package org.springframework.integration.handler;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.MessagingException;
import org.springframework.integration.message.MessageTarget;
import org.springframework.integration.message.MessagingException;
/**
* A messaging target that invokes the specified method on the provided object.
*
* @author Mark Fisher
*/
public class MethodInvokingTarget extends AbstractMessageHandlerAdapter implements MessageTarget {
public class MethodInvokingTarget extends AbstractMessageHandler implements MessageTarget {
public boolean send(Message<?> message) {
this.handle(message);
@@ -33,9 +33,9 @@ public class MethodInvokingTarget extends AbstractMessageHandlerAdapter implemen
}
@Override
protected Message<?> handleReturnValue(Object returnValue, Message<?> originalMessage) {
if (returnValue != null) {
throw new MessagingException(originalMessage, "The target method returned a non-null Object. " +
protected Message<?> createReplyMessage(Object result, Message<?> requestMessage) {
if (result != null) {
throw new MessagingException(requestMessage, "The target method returned a non-null Object. " +
"MethodInvokingTarget should only be used for methods that return no value (preferably void).");
}
return null;

View File

@@ -19,10 +19,6 @@ package org.springframework.integration.splitter;
import java.lang.reflect.Method;
import java.util.Map;
import org.springframework.aop.support.AopUtils;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.integration.annotation.MessageEndpoint;
import org.springframework.integration.handler.AbstractMessageHandlerAdapter;
import org.springframework.integration.handler.MessageHandler;
import org.springframework.integration.handler.config.AbstractMessageHandlerCreator;
@@ -34,13 +30,6 @@ import org.springframework.integration.handler.config.AbstractMessageHandlerCrea
public class SplitterMessageHandlerCreator extends AbstractMessageHandlerCreator {
public MessageHandler doCreateHandler(Object object, Method method, Map<String, ?> attributes) {
String outputChannelName = (String) attributes.get(AbstractMessageHandlerAdapter.OUTPUT_CHANNEL_NAME_KEY);
if (outputChannelName == null) {
MessageEndpoint endpointAnnotation = AnnotationUtils.findAnnotation(AopUtils.getTargetClass(object), MessageEndpoint.class);
if (endpointAnnotation != null) {
outputChannelName = endpointAnnotation.output();
}
}
return new SplitterMessageHandler(object, method);
}

View File

@@ -27,6 +27,7 @@ import java.util.concurrent.TimeUnit;
import org.junit.Test;
import org.springframework.integration.ConfigurationException;
import org.springframework.integration.bus.DefaultMessageBus;
import org.springframework.integration.bus.MessageBus;
import org.springframework.integration.channel.QueueChannel;
@@ -50,32 +51,36 @@ public class MethodInvokingTargetTests {
assertTrue(result);
}
@Test(expected=MessagingException.class)
@Test(expected = ConfigurationException.class)
public void testInvalidMethodWithNoArgs() {
MethodInvokingTarget target = new MethodInvokingTarget();
target.setObject(new TestSink());
target.setMethodName("invalidMethodWithNoArgs");
target.afterPropertiesSet();
target.send(new StringMessage("test"));
}
@Test(expected=MessagingException.class)
@Test(expected = MessagingException.class)
public void testMethodWithReturnValue() {
MethodInvokingTarget target = new MethodInvokingTarget();
target.setObject(new TestSink());
target.setMethodName("methodWithReturnValue");
target.afterPropertiesSet();
boolean result = target.send(new StringMessage("test"));
assertTrue(result);
Message<?> message = new StringMessage("test");
try {
MethodInvokingTarget target = new MethodInvokingTarget();
target.setObject(new TestSink());
target.setMethodName("methodWithReturnValue");
target.afterPropertiesSet();
target.send(message);
}
catch (MessagingException e) {
assertEquals(e.getFailedMessage(), message);
throw e;
}
}
@Test(expected=MessagingException.class)
@Test(expected = ConfigurationException.class)
public void testNoMatchingMethodName() {
MethodInvokingTarget target = new MethodInvokingTarget();
target.setObject(new TestSink());
target.setMethodName("noSuchMethod");
target.afterPropertiesSet();
target.send(new StringMessage("test"));
}
@Test