user2185287
user2185287

Reputation: 35

Generic Item Reader and Item Writer in spring-batch

How can I Write Generic Item Reader and Item Writer so that same readers and writers can be reused for all steps in the job.Please help?

thanks in advance.

Upvotes: 0

Views: 3358

Answers (1)

incomplete-co.de
incomplete-co.de

Reputation: 2137

Spring Batch as ItemReader, ItemProcessor and ItemWriter adapters "out-of-the-box" that can help make a lot of the functionality generic. Essentially the ItemReaderAdapter allows you to call an existing object and method without having to write your own reader.

here's a configuration example

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:batch="http://www.springframework.org/schema/batch"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch.xsd">


    <batch:job id="sampleJob">
        <batch:step id="sampleJob.step1">
            <batch:tasklet>
                <batch:chunk reader="readerAdapter" writer="..." commit-interval="10"/>
            </batch:tasklet>
        </batch:step>
    </batch:job>

    <bean id="readerAdapter" class="org.springframework.batch.item.adapter.ItemReaderAdapter" scope="step">
        <property name="targetObject" ref="myService"/>
        <property name="targetMethod" value="get"/>
        <property name="arguments" value="#{jobParameters['parameterName']}"/>
    </bean>

    <bean id="myService"/>

</beans>

there are adapters for each of the 'phases' in a chunk. have a look at the javadoc on each of the adapters, they'll help getter a better picture if your service returns the right type of object to participate in the chunk.

Upvotes: 1

Related Questions