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;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user