Java Reference
In-Depth Information
curIndex = 51;
} else {
curIndex = index;
}
} else {
curIndex = 0;
}
}
public void update(ExecutionContext executionContext) throws ItemStreamException {
executionContext.putInt(INDEX_KEY, curIndex);
}
}
The bold sections of Listing 7-58 show the updates to the CustomerItemReader. First, the class was
changed to implement the ItemStreamReader interface. Then the close , open and update methods were
added. In the update method, you add a key value pair to the executionContext that indicates the current
record being processed. The open method will check to see if that value has been set. If it has been set,
that means that this is the restart of your job. In the run method, to force the job to end, you added code
to throw a RuntimeException after the 50 th customer. In the open method, if the index being restored is
50, you'll know it was due to your previous code so you will just skip that record. Otherwise, you'll try
again.
The other piece you need to do is configure your new ItemReader implementation. In this case, your
ItemReader has no dependencies so all you will need to do is define the bean with the correct name (so it
is referred to in your existing copyJob). Listing 7-59 shows the configuration of the CustomerItemReader.
Listing 7-59. CustomerItemReader Configuration
<beans:bean id="customerItemReader"
class="com.apress.springbatch.chapter7.CustomerItemReader"/>
That really is it. Now if you execute your job, after you process 50 records, your
CustomerItemReader will throw an Exception causing your job to fail. However, if you look in the
BATCH_STEP_EXECUTION_CONTEXT table of your jobRepository, you will be happy to see what is
listed in Listing 7-60.
Listing 7-60. The Step Execution Context
mysql> select * from BATCH_STEP_EXECUTION_CONTEXT where STEP_EXECUTION_ID = 8495;
+-------------------+-----------------------------------------------------------------------
-----+------------------------+
| STEP_EXECUTION_ID | SHORT_CONTEXT
| SERIALIZED_CONTEXT |
+-------------------+-----------------------------------------------------------------------
-----+------------------------+
| 8495 |
{"map":{"entry":[{"string":"FlatFileItemWriter.current.count","long":2655},{"string":"FlatFi
leItemWriter.written","long":50},{"string":"current.index.customers","int":50}]}} | NULL
|
 
 
Search WWH ::




Custom Search