INT-765, INT-766 Added MessageProcessor interface. The MessageMappingMethodInvoking now implements that interface.

This commit is contained in:
Mark Fisher
2009-08-21 22:32:02 +00:00
parent 3be8f7b46d
commit e0dfba2ae9
10 changed files with 95 additions and 47 deletions

View File

@@ -29,24 +29,23 @@ import org.springframework.util.Assert;
*/
public class CorrelationStrategyAdapter implements CorrelationStrategy {
private final MessageMappingMethodInvoker invoker;
private final MessageMappingMethodInvoker invoker;
public CorrelationStrategyAdapter(Object object, String methodName) {
this.invoker = new MessageMappingMethodInvoker(object, methodName, true);
}
public CorrelationStrategyAdapter(Object object, String methodName) {
this.invoker = new MessageMappingMethodInvoker(object, methodName, true);
}
public CorrelationStrategyAdapter(Object object, Method method) {
Assert.notNull(object, "'object' must not be null");
Assert.notNull(method, "'method' must not be null");
Assert.isTrue(method.getParameterTypes().length == 1, "Method must accept exactly one parameter");
Assert.isTrue(!Void.TYPE.equals(method.getReturnType()), "Method return type must not be void");
this.invoker = new MessageMappingMethodInvoker(object, method);
}
public CorrelationStrategyAdapter(Object object, Method method) {
Assert.notNull(object, "'object' must not be null");
Assert.notNull(method, "'method' must not be null");
Assert.isTrue(method.getParameterTypes().length == 1, "Method must accept exactly one parameter");
Assert.isTrue(!Void.TYPE.equals(method.getReturnType()), "Method return type must not be void");
this.invoker = new MessageMappingMethodInvoker(object, method);
}
public Object getCorrelationKey(Message<?> message) {
return invoker.processMessage(message);
}
public Object getCorrelationKey(Message<?> message) {
return invoker.invokeMethod(message);
}
}

View File

@@ -47,7 +47,7 @@ public class MethodInvokingSelector implements MessageSelector {
public boolean accept(Message<?> message) {
Object result = this.invoker.invokeMethod(message);
Object result = this.invoker.processMessage(message);
Assert.notNull(result, "result must not be null");
Assert.isAssignable(Boolean.class, result.getClass(), "a boolean result is required");
return (Boolean) result;

View File

@@ -50,7 +50,7 @@ import org.springframework.util.StringUtils;
* @author Marius Bogoevici
* @see MethodParameterMessageMapper
*/
public class MessageMappingMethodInvoker {
public class MessageMappingMethodInvoker implements MessageProcessor {
protected static final Log logger = LogFactory.getLog(MessageMappingMethodInvoker.class);
@@ -90,9 +90,8 @@ public class MessageMappingMethodInvoker {
this.object = object;
this.methodResolver = this.createResolverForMethodName(methodName, requiresReturnValue);
}
public Object invokeMethod(Message<?> message) {
public Object processMessage(Message<?> message) {
Assert.notNull(message, "message must not be null");
if (message.getPayload() == null) {
if (logger.isDebugEnabled()) {
@@ -104,7 +103,7 @@ public class MessageMappingMethodInvoker {
Object[] args = null;
try {
args = this.createArgumentArrayFromMessage(method, message);
return this.doInvokeMethod(method, args, message);
return this.invokeMethod(method, args, message);
}
catch (InvocationTargetException e) {
if (e.getCause() != null && e.getCause() instanceof RuntimeException) {
@@ -122,7 +121,7 @@ public class MessageMappingMethodInvoker {
}
}
private Object doInvokeMethod(Method method, Object[] args, Message<?> message) throws Exception {
private Object invokeMethod(Method method, Object[] args, Message<?> message) throws Exception {
Object result = null;
MethodInvoker invoker = null;
try {

View File

@@ -0,0 +1,49 @@
/*
* Copyright 2002-2009 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.handler;
import org.springframework.integration.core.Message;
/**
* This defines the lowest-level strategy of processing a Message and returning
* some Object (or null). Implementations will be focused on generic concerns,
* such as invoking a method, running a script, or evaluating an expression.
* <p>
* Higher level MessageHandler implementations can delegate to these processors
* for such functionality, but it is the responsibility of each handler type to
* add the semantics such as routing, splitting, transforming, etc.
* <p>
* In some cases the return value might be a Message itself, but it does not
* need to be. It is the responsibility of the caller to determine how to treat
* the return value. That may require creating a Message or even creating
* multiple Messages from that value.
* <p>
* This strategy and its various implementations are considered part of the
* internal "support" API, intended for use by Spring Integration's various
* message-handling components. As such, it is subject to change.
*
* @author Mark Fisher
* @since 2.0
*/
public interface MessageProcessor {
/**
* Process the Message and return a value (or null).
*/
Object processMessage(Message<?> message);
}

View File

@@ -54,7 +54,7 @@ public class MethodInvokingMessageHandler extends MessageMappingMethodInvoker im
}
public void handleMessage(Message<?> message) {
Object result = this.invokeMethod(message);
Object result = this.processMessage(message);
if (result != null) {
throw new MessagingException(message, "the MethodInvokingMessageHandler method must "
+ "have a void return, but '" + this + "' received a value: [" + result + "]");

View File

@@ -46,7 +46,7 @@ public class ServiceActivatingHandler extends AbstractReplyProducingMessageHandl
@Override
protected void handleRequestMessage(Message<?> message, ReplyMessageHolder replyHolder) {
try {
Object result = this.invoker.invokeMethod(message);
Object result = this.invoker.processMessage(message);
if (result != null) {
replyHolder.set(result);
}

View File

@@ -54,7 +54,7 @@ public class MethodInvokingRouter extends AbstractChannelNameResolvingMessageRou
@Override
protected List<Object> getChannelIndicatorList(Message<?> message) {
Object result = this.invoker.invokeMethod(message);
Object result = this.invoker.processMessage(message);
List<Object> asList = new ArrayList<Object>();
asList.add(result);
return asList;

View File

@@ -51,7 +51,7 @@ public class MethodInvokingSplitter extends AbstractMessageSplitter {
@Override
protected Object splitMessage(Message<?> message) {
return this.invoker.invokeMethod(message);
return this.invoker.processMessage(message);
}
}

View File

@@ -22,6 +22,7 @@ import java.util.Properties;
import org.springframework.integration.core.Message;
import org.springframework.integration.handler.MessageMappingMethodInvoker;
import org.springframework.integration.handler.MessageProcessor;
import org.springframework.integration.message.MessageBuilder;
import org.springframework.integration.message.MessageHandlingException;
@@ -30,25 +31,25 @@ import org.springframework.integration.message.MessageHandlingException;
*/
public class MethodInvokingTransformer implements Transformer {
private final MessageMappingMethodInvoker invoker;
private final MessageProcessor messageProcessor;
public MethodInvokingTransformer(Object object, Method method) {
this.invoker = new MessageMappingMethodInvoker(object, method);
this.messageProcessor = new MessageMappingMethodInvoker(object, method);
}
public MethodInvokingTransformer(Object object, String methodName) {
this.invoker = new MessageMappingMethodInvoker(object, methodName);
this.messageProcessor = new MessageMappingMethodInvoker(object, methodName);
}
public MethodInvokingTransformer(Object object) {
this.invoker = new MessageMappingMethodInvoker(object,
this.messageProcessor = new MessageMappingMethodInvoker(object,
org.springframework.integration.annotation.Transformer.class);
}
public Message<?> transform(Message<?> message) {
Object result = this.invoker.invokeMethod(message);
Object result = this.messageProcessor.processMessage(message);
if (result == null) {
return null;
}

View File

@@ -42,7 +42,7 @@ public class MessageMappingMethodInvokerTests {
public void payloadAsMethodParameterAndObjectAsReturnValue() {
MessageMappingMethodInvoker invoker = new MessageMappingMethodInvoker(
new TestBean(), "acceptPayloadAndReturnObject");
Object result = invoker.invokeMethod(new StringMessage("testing"));
Object result = invoker.processMessage(new StringMessage("testing"));
assertEquals("testing-1", result);
}
@@ -50,7 +50,7 @@ public class MessageMappingMethodInvokerTests {
public void payloadAsMethodParameterAndMessageAsReturnValue() {
MessageMappingMethodInvoker invoker = new MessageMappingMethodInvoker(
new TestBean(), "acceptPayloadAndReturnMessage");
Message<?> result = (Message<?>) invoker.invokeMethod(new StringMessage("testing"));
Message<?> result = (Message<?>) invoker.processMessage(new StringMessage("testing"));
assertEquals("testing-2", result.getPayload());
}
@@ -58,7 +58,7 @@ public class MessageMappingMethodInvokerTests {
public void messageAsMethodParameterAndObjectAsReturnValue() {
MessageMappingMethodInvoker invoker = new MessageMappingMethodInvoker(
new TestBean(), "acceptMessageAndReturnObject");
Object result = invoker.invokeMethod(new StringMessage("testing"));
Object result = invoker.processMessage(new StringMessage("testing"));
assertEquals("testing-3", result);
}
@@ -66,7 +66,7 @@ public class MessageMappingMethodInvokerTests {
public void messageAsMethodParameterAndMessageAsReturnValue() {
MessageMappingMethodInvoker invoker = new MessageMappingMethodInvoker(
new TestBean(), "acceptMessageAndReturnMessage");
Message<?> result = (Message<?>) invoker.invokeMethod(new StringMessage("testing"));
Message<?> result = (Message<?>) invoker.processMessage(new StringMessage("testing"));
assertEquals("testing-4", result.getPayload());
}
@@ -74,7 +74,7 @@ public class MessageMappingMethodInvokerTests {
public void messageSubclassAsMethodParameterAndMessageAsReturnValue() {
MessageMappingMethodInvoker invoker = new MessageMappingMethodInvoker(
new TestBean(), "acceptMessageSubclassAndReturnMessage");
Message<?> result = (Message<?>) invoker.invokeMethod(new StringMessage("testing"));
Message<?> result = (Message<?>) invoker.processMessage(new StringMessage("testing"));
assertEquals("testing-5", result.getPayload());
}
@@ -82,7 +82,7 @@ public class MessageMappingMethodInvokerTests {
public void messageSubclassAsMethodParameterAndMessageSubclassAsReturnValue() {
MessageMappingMethodInvoker invoker = new MessageMappingMethodInvoker(
new TestBean(), "acceptMessageSubclassAndReturnMessageSubclass");
Message<?> result = (Message<?>) invoker.invokeMethod(new StringMessage("testing"));
Message<?> result = (Message<?>) invoker.processMessage(new StringMessage("testing"));
assertEquals("testing-6", result.getPayload());
}
@@ -92,23 +92,23 @@ public class MessageMappingMethodInvokerTests {
new TestBean(), "acceptPayloadAndHeaderAndReturnObject");
Message<?> request = MessageBuilder.withPayload("testing")
.setHeader("number", new Integer(123)).build();
Object result = invoker.invokeMethod(request);
Object result = invoker.processMessage(request);
assertEquals("testing-123", result);
}
@Test
public void testVoidMethodsIncludedbyDefault() {
MessageMappingMethodInvoker invoker = new MessageMappingMethodInvoker(new TestBean(), "testVoidReturningMethods");
assertNull(invoker.invokeMethod(MessageBuilder.withPayload("Something").build()));
assertEquals(12, invoker.invokeMethod(MessageBuilder.withPayload(12).build()));
assertNull(invoker.processMessage(MessageBuilder.withPayload("Something").build()));
assertEquals(12, invoker.processMessage(MessageBuilder.withPayload(12).build()));
}
@Test
public void testVoidMethodsExcludedByFlag() {
MessageMappingMethodInvoker invoker = new MessageMappingMethodInvoker(new TestBean(), "testVoidReturningMethods", true);
assertEquals(12, invoker.invokeMethod(MessageBuilder.withPayload(12).build()));
assertEquals(12, invoker.processMessage(MessageBuilder.withPayload(12).build()));
try {
assertNull(invoker.invokeMethod(MessageBuilder.withPayload("Something").build()));
assertNull(invoker.processMessage(MessageBuilder.withPayload("Something").build()));
fail();
} catch(IllegalArgumentException ex){
@@ -120,7 +120,7 @@ public class MessageMappingMethodInvokerTests {
AnnotatedTestService service = new AnnotatedTestService();
Method method = service.getClass().getMethod("messageOnly", Message.class);
MessageMappingMethodInvoker invoker = new MessageMappingMethodInvoker(service, method);
Object result = invoker.invokeMethod(new StringMessage("foo"));
Object result = invoker.processMessage(new StringMessage("foo"));
assertEquals("foo", result);
}
@@ -129,7 +129,7 @@ public class MessageMappingMethodInvokerTests {
AnnotatedTestService service = new AnnotatedTestService();
Method method = service.getClass().getMethod("integerMethod", Integer.class);
MessageMappingMethodInvoker invoker = new MessageMappingMethodInvoker(service, method);
Object result = invoker.invokeMethod(new GenericMessage<Integer>(new Integer(123)));
Object result = invoker.processMessage(new GenericMessage<Integer>(new Integer(123)));
assertEquals(new Integer(123), result);
}
@@ -138,7 +138,7 @@ public class MessageMappingMethodInvokerTests {
AnnotatedTestService service = new AnnotatedTestService();
Method method = service.getClass().getMethod("integerMethod", Integer.class);
MessageMappingMethodInvoker invoker = new MessageMappingMethodInvoker(service, method);
Object result = invoker.invokeMethod(new StringMessage("456"));
Object result = invoker.processMessage(new StringMessage("456"));
assertEquals(new Integer(456), result);
}
@@ -147,7 +147,7 @@ public class MessageMappingMethodInvokerTests {
AnnotatedTestService service = new AnnotatedTestService();
Method method = service.getClass().getMethod("integerMethod", Integer.class);
MessageMappingMethodInvoker invoker = new MessageMappingMethodInvoker(service, method);
Object result = invoker.invokeMethod(new StringMessage("foo"));
Object result = invoker.processMessage(new StringMessage("foo"));
assertEquals(new Integer(123), result);
}
@@ -158,7 +158,7 @@ public class MessageMappingMethodInvokerTests {
MessageMappingMethodInvoker invoker = new MessageMappingMethodInvoker(service, method);
Message<String> message = MessageBuilder.withPayload("foo")
.setHeader("number", 42).build();
Object result = invoker.invokeMethod(message);
Object result = invoker.processMessage(message);
assertEquals("foo-42", result);
}
@@ -170,7 +170,7 @@ public class MessageMappingMethodInvokerTests {
Message<String> message = MessageBuilder.withPayload("foo")
.setHeader("prop", "bar")
.setHeader("number", 42).build();
Object result = invoker.invokeMethod(message);
Object result = invoker.processMessage(message);
assertEquals("bar-42", result);
}