title: Accessing the ExecutionContext values in FooterCallBack with Spring batch
body: <p>I need to attached the ReadCount to the file footer, I dont know why the JobExecution in writeFooter method is null. </p>
  
  <p>I found this and according to the comments I should be able to write this information to the footer: <a href="https://stackoverflow.com/questions/52076902/accessing-the-executioncontext-values-in-headercallback-in-spring-batch">Accessing the ExecutionContext values in HeaderCallBack in Spring Batch</a></p>
  
  <pre><code>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);
      }
  </code></pre>
  
  <p>I tried to implement StepExecutionListener and the stepExecution is null inside the writeFooter method.</p>
  
  <pre><code>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);
      }
  </code></pre>