12

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>
Community
  • 1
  • 1
user3247376
  • 205
  • 1
  • 4
  • 13
  • Why would you want chunk based reading but after that drop everything? Why would you only read and don't write anything? For chunk based processing the reader and writer are mandatory, only the processor is optional, which makes sense as reading without writing doesn't really makes sense... – M. Deinum Oct 06 '14 at 07:28
  • how the tasklet didn't work for you? I am calling a procedure in a tasklet, and it works pretty fine. If you don't need (why you ever need) chunk based reading, why not a simple tasklet then? – Shilan Mar 01 '17 at 10:04

3 Answers3

19

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
  }
}
Luca Basso Ricci
  • 17,829
  • 2
  • 47
  • 69
  • Can you please tell me how to configure a No-operation itemwriter in above example.. I am a beginner hence require some basic help.. Thanks in advance. – user3247376 Oct 06 '14 at 02:50
  • Thankyou so much..realised the implementation before you posted NoOpItemWriter.. or rather after I posted the question for the same ..Thanks anyways..:) – user3247376 Oct 06 '14 at 08:33
  • 1
    @Luca Basso Ricci - Since we need to specify input and output types for processor and input type for writer too so is processor returning `null` enough here? Also, since nothing is being written here so is it wise to specify chunk size as high as possible? Any suggestions on chunk size/commit-interval value? – Sabir Khan Oct 05 '16 at 11:48
  • 1
    Want to add that while this works for itemwriter, it WILL NOT work for itemreader. a tasklet is required the other way around. details in this post: https://stackoverflow.com/questions/48590704/spring-step-does-not-run-properly-when-i-fib-the-reader-must-i-use-a-tasklet#comment84179502_48590704 – cptwonton Feb 05 '18 at 15:01
4

I hope you got answer but I want to explain it for other readers, When we use chunk then usually we declare reader, processor and writer. In chunk reader and writer are mandatory and processor is optional. In your case if you don't need writer then u need to make a class which implements ItemWriter. Override write method and keep it blank. Now create a bean of writer class and pass it as reference of writer.

<batch:step id="recordProcessingStep" >
        <batch:tasklet>
            <batch:chunk reader="fileReader" processor="recordProcessor"
                writer="rocordWriter" commit-interval="1" />
        </batch:tasklet>
    </batch:step>

Your writer class will look like .

public class RecordWriter<T> implements ItemWriter<T> {
@Override
public void write(List<? extends T> items) throws Exception {
    // TODO Auto-generated method stub

}

}

Ashok Singh
  • 233
  • 1
  • 4
  • 12
1

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
Thierry
  • 89
  • 1
  • 5