How to write a spring batch step without an itemwriter
Asked Answered
S

3

13

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>
Stavro answered 5/10, 2014 at 11:35 Comment(2)
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...Iona
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?Breeze
T
21

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 that does...nothing!
Just let your class implement desirable 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
  }
}
Teletypewriter answered 5/10, 2014 at 18:16 Comment(4)
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.Stavro
Thankyou so much..realised the implementation before you posted NoOpItemWriter.. or rather after I posted the question for the same ..Thanks anyways..:)Stavro
@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?Bernhard
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: #48591204Durance
K
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

}

}

Known answered 17/7, 2015 at 10:10 Comment(0)
M
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
Mathia answered 3/12, 2015 at 17:24 Comment(1)
This writer is just used for testing or demonstration purposes, not production code, since it calls Thread.sleep(500);.Dissidence

© 2022 - 2024 — McMap. All rights reserved.