Java Reference
In-Depth Information
the beginning of the step normally. As each chunk completes, you use an ItemWriteListener to update
the records you just processed as having been processed.
To apply this concept to the statement job's calculateTransactionFees step, you begin by adding
two columns to the transaction table: jobId and processed. The jobId stores the run.id of the current run
of the statement job. The second column is a boolean with the value true if the record has been
processed and false if it hasn't. Figure 11-13 shows the updated table definition.
Figure 11-13. Updated data model for the transaction table with staging columns included
In order to put the columns to use, you need to create a StepListener to update the records you
process with the jobId and set the processed flag to false for the records you process. To do this, you
create a StepListener called StagingStepListener that updates these columns on whatever table you
configure as well as reuse it for other tables. Listing 11-4 shows the code for the StagingStepListener .
Listing 11-4. StagingStepListener
package com.apress.springbatch.statement.listener;
import org.springframework.batch.core.StepListener;
import org.springframework.batch.core.annotation.BeforeStep;
import org.springframework.jdbc.core.JdbcTemplate;
public class StagingStepListener extends JdbcTemplate implements StepListener {
private String SQL = " set jobId = ?, processed = false ";
private String tableName;
private String whereClause = "";
private long jobId;
@BeforeStep
public void stageRecords() {
update("update " + tableName + SQL + whereClause, new Object [] {jobId});
}
public void setTableName(String tableName) {
this.tableName = tableName;
}
 
Search WWH ::




Custom Search