INT-1448, INT-1442 accepted changes by Iwein, added small refactoring to AbstractMessageSplitter, added support for 'requires-reply' attribute, added test cases'

This commit is contained in:
Oleg Zhurakousky
2010-09-13 15:57:57 -04:00
parent e5f67740e4
commit f964f74f4e
6 changed files with 55 additions and 15 deletions

View File

@@ -31,6 +31,7 @@ import org.springframework.util.StringUtils;
public class SplitterFactoryBean extends AbstractMessageHandlerFactoryBean {
private volatile Long sendTimeout;
private volatile boolean requiresReply;
public void setSendTimeout(Long sendTimeout) {
this.sendTimeout = sendTimeout;
@@ -64,7 +65,14 @@ public class SplitterFactoryBean extends AbstractMessageHandlerFactoryBean {
if (this.sendTimeout != null) {
splitter.setSendTimeout(sendTimeout);
}
splitter.setRequiresReply(requiresReply);
return splitter;
}
public boolean isRequiresReply() {
return requiresReply;
}
public void setRequiresReply(boolean requiresReply) {
this.requiresReply = requiresReply;
}
}

View File

@@ -20,7 +20,10 @@ import org.springframework.integration.Message;
import org.springframework.integration.MessageHeaders;
import org.springframework.integration.handler.AbstractReplyProducingMessageHandler;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import java.lang.reflect.Array;
import java.util.*;
/**
@@ -38,7 +41,10 @@ public abstract class AbstractMessageSplitter extends AbstractReplyProducingMess
@SuppressWarnings("unchecked")
protected final Object handleRequestMessage(Message<?> message) {
Object result = this.splitMessage(message);
if (result == null) {
// return null if 'null', empty Collection or empty Array
if ( result == null ||
(result instanceof Collection && CollectionUtils.isEmpty((Collection<?>)result)) ||
(result.getClass().isArray() && ObjectUtils.isEmpty((Object[]) result)) ) {
return null;
}
MessageHeaders headers = message.getHeaders();
@@ -59,10 +65,6 @@ public abstract class AbstractMessageSplitter extends AbstractReplyProducingMess
List<MessageBuilder<?>> messageBuilders = new ArrayList<MessageBuilder<?>>();
if (result instanceof Collection) {
Collection<?> items = (Collection<?>) result;
//TODO put this return statement in a more obvious place
if(items.isEmpty()){
return null;
}
int sequenceNumber = 0;
int sequenceSize = items.size();
for (Object item : items) {

View File

@@ -1948,6 +1948,15 @@ Name of the header whose value to use.
<xsd:complexContent>
<xsd:extension base="expressionOrInnerEndpointDefinitionAware">
<xsd:attributeGroup ref="inputOutputChannelGroup" />
<xsd:attribute name="requires-reply" type="xsd:string" use="optional">
<xsd:annotation>
<xsd:documentation>
Specify whether the splitter method must return a non-null value. This value will be
FALSE by default, but if set to TRUE, a MessageHandlingException will be thrown when
the underlying service method (or expression) returns a NULL value.
</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>

View File

@@ -19,13 +19,19 @@ package org.springframework.integration.router.config;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import java.util.Collections;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.integration.Message;
import org.springframework.integration.MessageChannel;
import org.springframework.integration.MessageHandlingException;
import org.springframework.integration.channel.DirectChannel;
import org.springframework.integration.core.PollableChannel;
import org.springframework.integration.endpoint.EventDrivenConsumer;
import org.springframework.integration.message.GenericMessage;
import org.springframework.integration.support.MessageBuilder;
/**
* @author Mark Fisher
@@ -88,5 +94,14 @@ public class SplitterParserTests {
assertEquals("test", result4.getPayload());
assertNull(output.receive(0));
}
@Test(expected=MessageHandlingException.class)
public void splitterParserTestWithRequiresReply() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"splitterParserTests.xml", this.getClass());
context.start();
DirectChannel inputChannel = context.getBean("requiresReplyInput", DirectChannel.class);
inputChannel.send(MessageBuilder.withPayload(Collections.emptyList()).build());
}
}

View File

@@ -26,6 +26,11 @@
ref="splitterImpl"
input-channel="splitterImplementationInput"
output-channel="output"/>
<splitter id="splitterImplementationRequiresReply"
input-channel="requiresReplyInput"
output-channel="output"
requires-reply="true"/>
<beans:bean id="splitterBean" class="org.springframework.integration.router.config.TestSplitterBean"/>

View File

@@ -16,6 +16,17 @@
package org.springframework.integration.splitter;
import static junit.framework.Assert.assertEquals;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.junit.Test;
import org.springframework.integration.Message;
import org.springframework.integration.channel.DirectChannel;
@@ -23,15 +34,6 @@ import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.endpoint.EventDrivenConsumer;
import org.springframework.integration.support.MessageBuilder;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import static junit.framework.Assert.assertEquals;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.*;
/**
* @author Mark Fisher
* @author Iwein Fuld
@@ -104,5 +106,4 @@ public class DefaultSplitterTests {
Message<?> output = replyChannel.receive(15);
assertThat(output, is(nullValue()));
}
}