Let's say I have a custom module in Spring XD (I'm using Spring XD + Spring Integration + hibernate). The module basically gets something from the DB (let's say I store it using an hibernate entity, so I use an objet called "DataFromDB"). DataFromDB is a list, then I get each element from the list and I want to send it using something like:
String payload = convertDataFromDBToJson(record);
return MessageBuilder.createMessage(payload, message.getHeaders());
- The problem is every time I have to send a message I have to return a Message.
- So I would like to loop over the DataFromDB list and send each element as a message.
Is there a way to send multiple messages?
Edit:
I just created a little example according to the comments trying to replicate the scenario. This is what I have:
my transformer class:
public class TransformerClass {
public Collection<Message<?>> transformerMethod(Message<?> message) {
List<Message<?>> messages = new ArrayList<Message<?>>();
messages.add(new GenericMessage<>("foo"));
messages.add(new GenericMessage<>("bar"));
return messages;
}
My xml configuration:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:int="http://www.springframework.org/schema/integration"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<tx:annotation-driven />
<int:channel id="input" />
<bean id="transFormerClass" class="myModule.TransformerClass">
</bean>
<int:transformer input-channel="input" output-channel="output" ref="transFormerClass" method="transformerMethod"/>
<int:channel id="output"/>
</beans>
My test class:
@ModuleName(value = "someModule", type = ModuleType.processor)
public class TransformerClassTest extends xDTest {
private String streamName = "myStream";
private String chainTest = "someModule";
@SuppressWarnings("unchecked")
@Test
public void testPartnerNotification() throws IOException {
this.chain = SingleNodeProcessingChainSupport.chain(application, streamName, chainTest);
//Just to send something to the module as a input.
Message<String> input = MessageBuilder.createMessage("hello world", buildHeaders());
this.chain.send(input);
//Receiving a Single message
Message<String> result = (Message<String>) chain.receive(5000);
System.out.println("Result: " + result);
}
private MessageHeaders buildHeaders() {
Map<String, Object> hashMap = new HashMap<String,Object>();
hashMap.put("test", "testing");
MessageHeaders headers = new MessageHeaders(hashMap);
return headers;
}
}
Output is:
Result: GenericMessage [payload=[GenericMessage [payload=foo, headers={timestamp=1475072951345, id=7b6c79a2-db85-563e-c238-262a31141456}], GenericMessage [payload=bar, headers={timestamp=1475072951345, id=31c8ef0e-3513-b95e-3a25-4fd3550f2fea}]], headers={timestamp=1475072951347, id=f90d94c4-e323-70ed-62ee-4b8bce64814d, test=testing}]
I'm using Spring Integration 4.2.2.RELEASE.
MessageBuilder.createMessage(payload, message.getHeaders());
Therefore in that case, just one message (then only one element in the lis)t would be sent. – Columb1a