I need to attached the ReadCount to the file footer, I dont know why the JobExecution in writeFooter method is null.
I found this and according to the comments I should be able to write this information to the footer: Accessing the ExecutionContext values in HeaderCallBack in Spring Batch
public class Footer implements FlatFileFooterCallback {
private final String footer;
private String julianDate;
private StepExecution stepExecution;
@AfterStep
public void afterStep(StepExecution stepExecution) {
ExecutionContext je = stepExecution.getJobExecution().getExecutionContext();
je.putLong("writeCount", stepExecution.getReadCount());
}
@BeforeStep
public void beforeStep(StepExecution stepExecution) {
this.stepExecution = stepExecution;
}
// TODO: NEED TO REPLACE COUNT WITH NUMBER OF RECORDS IN FILE
public Footer(){
this.julianDate = createJulianDate();
this.footer = "";
}
@Override
public void writeFooter(Writer writer) throws IOException {
JobExecution jobExecution = this.stepExecution.getJobExecution();
ExecutionContext jobContext = jobExecution.getExecutionContext();
writer.write(footer);
}
I tried to implement StepExecutionListener and the stepExecution is null inside the writeFooter method.
public class Footer implements FlatFileFooterCallback, StepExecutionListener {
private final String footer;
private String julianDate;
private StepExecution stepExecution;
// TODO: NEED TO REPLACE COUNT WITH NUMBER OF RECORDS IN FILE
public Footer(){
this.julianDate = createJulianDate();
this.footer = "";
}
@Override
public void beforeStep(StepExecution stepExecution) {
this.stepExecution = stepExecution;
}
@Override
public ExitStatus afterStep(StepExecution stepExecution) {
System.out.println(stepExecution.getReadCount());
System.out.println("END");
return ExitStatus.COMPLETED;
}
@Override
public void writeFooter(Writer writer) throws IOException {
writer.write("Foot - number of items written: " + stepExecution.getWriteCount());
writer.write(footer);
}