I did some searching but couldn\'t find any sample/example.
I\'ve a requirement where geo coordinates from one table (input) are read, processed to generate POI\'s a
What you are really looking for is called a Splitter pattern:
Here is how it is defined in Spring Integration:
A Splitter is a type of Message Endpoint whose responsibility is to accept a Message from its input channel, split that Message into multiple Messages, and then send each of those to its output channel. This is typically used for dividing a "composite" payload object into a group of Messages containing the sub-divided payloads.
Configuration is extremely simple:
<channel id="inputChannel"/>
<splitter id="splitter"
ref="splitterBean"
method="split"
input-channel="inputChannel"
output-channel="outputChannel" />
<channel id="outputChannel"/>
<beans:bean id="splitterBean" class="sample.PojoSplitter"/>
Or you can use annotations:
@Splitter
List<LineItem> extractItems(Order order) {
return order.getItems()
}
You can of course write your own JdbcBatchItemWriter
if it feels simpler. However Spring Integration already does it for you.
You can use Spring Integration JDBC Support => jdbc:inbound-channel-adapter
/ jdbc:outbound-channel-adapter
and the above splitter to achieve what you want and.. simplicity.
I did this by extending the Writer class (in my case HibernateItemWriter). I see one answer describes how to use a 'splitter'. If anyone has a working example of how that might work in an environment using the spring-boot-starter-parent, I would love to see it. For what I am doing (creating a List from a single record) it would be so much easier if Spring provided a write method that handled a List of Lists.
Here is how I extended the Writer to handle multiple writes for every line read/processed: Spring-Batch Multi-line record Item Writer with variable number of lines per record
if you just want to spread the items to different writers (read duplicate output) you can use the existing CompositeItemWriter
but i'm not sure if your processor will produce different item types or if you want to spread the content of one complex item type to multiple writers, for those cases you can use a slightly changed version for a multiline-record-writer question
public class MultiOutputItemWriter implements ItemWriter<Object> {
private JdbcBatchItemWriter<ClassFoo> delegateFoo;
private JdbcBatchItemWriter<ClassBar> delegateBar;
public void write(List<? extends Object> items) throws Exception {
// if you have different types of items
// check Object Class
// add to new List<Classfoo>
// call delegate e.g. delegateFoo.write(List with ClassFoo);
//
// or if you have complex objects
// same procedure as above, but with
// add to new List<Classfoo> with item.getClassFoo
}
}
if you use FlatFileItemWriter, do not forget to register the delegates as ItemStreams (so spring batch will open/close them for you)