Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write a spring batch step without an itemwriter

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>
like image 819
user3247376 Avatar asked Oct 05 '14 11:10

user3247376


2 Answers

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
  }
}
like image 103
Luca Basso Ricci Avatar answered Oct 12 '22 10:10

Luca Basso Ricci


In maven repo you can find the framework "spring-batch-samples".
In this framework you will find this Writer :

org.springframework.batch.sample.support.DummyItemWriter
like image 27
Thierry Avatar answered Oct 12 '22 12:10

Thierry