Reputation: 711
My batch job reader return Nullpointerexception, but the file still generated. Why will this happened? I thought the writer will only being called when reader and processor done the process?
<bean id="myWriter" class="org.springframework.batch.item.file.FlatFileItemWriter" >
<property name="resource"
value="file:D:\out\file.txt" />
<property name="lineAggregator">
<bean
class="org.springframework.batch.item.file.transform.PassThroughLineAggregator" />
</property>
<property name="footerCallback" ref="myCustomWriter" />
<!-- <property name="headerCallback" ref="myCustomWriter" /> -->
</bean>
<bean id="myCustomWriter" class="com.spring.batch.MyWriter"
scope="step">
<property name="delegate" ref="myWriter" />
<property name="stepContext" value="#{stepExecution.stepName}" />
</bean>
Upvotes: 1
Views: 597
Reputation: 1171
The commit interval on the step controls when the writer is called. The way it works is
This is explained in detail in the documentation in the chapter - http://docs.spring.io/spring-batch/reference/html/configureStep.html
Upvotes: 0
Reputation: 4444
What do you mean by generated? Is it empty, or does it contain data?
Before a step es exectued, the spring-batch framework calls the open()-method of the ItemStream interface for Readers and Writers (provided that you have Readers and Writers implementing ItemStream).
Look at the FlatFileItemWriter implementation. You see that it implements ItemStream and, therefore, has an implemenation for the open() method.
Looking at the code in more detail, you see that open() calls doOpen(), which calls the HeaderCallback (if defined). So the the file is generated and the header is written before any call to the writer-method or the reader-method of the Reader happens.
I would recommend that you place a breakpoint in the open Methods of the Reader and Writer and debug a little bit around in the code of the spring-batch framework. It will give you a better understanding about what is going on "under the hood".
This may be out of scope to your initial question but if you would like to delve even deeper in the "transaction handling" inside a step, there are three excellent blog entries by Tobias Flöhre covering this topic:
Upvotes: 3