Einn Hann
Einn Hann

Reputation: 711

Spring Batch Framework writer still processing even the reader encountered Nullpointerexception

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

Answers (2)

Saifuddin Merchant
Saifuddin Merchant

Reputation: 1171

The commit interval on the step controls when the writer is called. The way it works is

  1. Reader is called to bulk read N items (that's how it's practically implemented, conceptually it is supposed to read one item and then process one item)
    1. Process each of the N items one by one
    2. At commit count, write all of the N items at once.

This is explained in detail in the documentation in the chapter - http://docs.spring.io/spring-batch/reference/html/configureStep.html

  • Header callback an footer callback will also be called sometime before the file is first written into, during the initialization of the writer

Upvotes: 0

Hansjoerg Wingeier
Hansjoerg Wingeier

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

Related Questions