MethodInvokingTarget now extends AbstractMessageHandler instead of AbstractMessageHandlerAdapter. SplitterMessageHandlerCreator no longer requires configuration of the "outputChannel" property.
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user