Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read one record/item and write multiple records/items using spring batch

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 associated to coordinate. So one geo coordinate will result in one or more POI's that needs to be inserted into another table (output).

I'm currently using a JdbcCursorItemReader and JdbcBatchItemWriter to read one item/record and write one item/record. There is also an ItemProcessor that generates the POI's for a give geo coordinate.

Does a custom JdbcBatchItemWriter help me achieve this?

Any ideas? TIA.

like image 365
user977505 Avatar asked Oct 25 '11 19:10

user977505


1 Answers

What you are really looking for is called a Splitter pattern:

enter image description here

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.

like image 142
tolitius Avatar answered Sep 23 '22 00:09

tolitius