How to write a spring batch step without an itemwriter

user3247376 picture user3247376 · Oct 5, 2014 · Viewed 25.8k times · Source

I am trying to configure a spring batch step without an item writer using below configuraion. However i get error saying that writer element has neither a 'writer' attribute nor a element.

I went through the link spring batch : Tasklet without ItemWriter. But could not resolve issue. Could any one tell me the specific changes to be made in the code snippet I mentioned

<batch:job id="helloWorldJob">
        <batch:step id="step1">
            <batch:tasklet>
                <batch:chunk reader="cvsFileItemReader"
                    commit-interval="10">
                </batch:chunk>
            </batch:tasklet>
        </batch:step>
    </batch:job>

    <bean id="cvsFileItemReader" class="org.springframework.batch.item.file.FlatFileItemReader">

        <property name="resource" value="classpath:cvs/input/report.csv" />

        <property name="lineMapper">
            <bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
                <property name="lineTokenizer">
                    <bean
                        class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">
                        <property name="names" value="id,sales,qty,staffName,date" />
                    </bean>
                </property>
                <property name="fieldSetMapper">
                    <bean class="com.mkyong.ReportFieldSetMapper" />

                    <!-- if no data type conversion, use BeanWrapperFieldSetMapper to map by name
                    <bean
                        class="org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper">
                        <property name="prototypeBeanName" value="report" />
                    </bean>
                     -->
                </property>
            </bean>
        </property>

    </bean>

Answer

Luca Basso Ricci picture Luca Basso Ricci · Oct 5, 2014

For chunk-based step reader and writer are mandatory.
If you don't want a writer use a No-operation ItemWriter that does nothing.

EDIT:
A no-op implementation is an empty implementation of interface tha does...nothing!
Just let your class implements desiderable inteface(s) with empty methods.

No-op ItemWriter:

public class NoOpItemWriter implements ItemWriter {
  void write(java.util.List<? extends T> items) throws java.lang.Exception {
    // no-op
  }
}