title: Spring Batch stop job execution from external class
body: <p>I have an existing spring batch project that have multiple steps. I want to modify a step so I can stop the job : <code>jobExecution.getStatus() == STOPPED</code>.</p>
  
  <p>My step :</p>
  
  <pre class="lang-java prettyprint-override"><code>@Autowired
  public StepBuilderFactory stepBuilderFactory;
  @Autowired
  private StepReader reader;
  @Autowired
  private StepProcessor processor;
  @Autowired
  private StepWriter writer;
  @Autowired
  public GenericListener listener;
  @Bean
  @JobScope
  @Qualifier("mystep")
  public Step MyStep() throws ReaderException {
      return stepBuilderFactory.get("mystep")
              .reader(reader.read())
              .listener(listener)
              .processor(processor)
              .writer(writer)
              .build();
  }
  </code></pre>
  
  <p><code>GenericListener</code> implements <code>ItemReadListener, ItemProcessListener, ItemWriteListener</code> and overrides before and after methods that basically write log.</p>
  
  <p>The focus here is on the <code>StepReader</code> class and its <code>read()</code> method that returns a <code>FlatFileItemReader</code> :</p>
  
  <pre class="lang-java prettyprint-override"><code>@Component
  public class StepReader {
      public static final String DELIMITER = "|";
      @Autowired
      private ClassToAccessProperties classToAccessProperties;
      private Logger log = Logger.create(StepReader.class);
      @Autowired
      private FlatFileItemReaderFactory&lt;MyObject&gt; flatFileItemReaderFactory;
  
      public ItemReader&lt;MyObject&gt; read() throws ReaderException {
          try {
              String csv = classToAccessProperties.getInputCsv();
              FlatFileItemReader&lt;MyObject&gt; reader = flatFileItemReaderFactory.create(csv, getLineMapper());
              return reader;
          } catch (ReaderException | EmptyInputfileException | IOException e) {
              throw new ReaderException(e);
          } catch (NoInputFileException e) {
              log.info("Oh no !! No input file");
              // Here I want to stop the job
              return null;
          }
      }
      private LineMapper&lt;MyObject&gt; getLineMapper () {
          DefaultLineMapper&lt;MyObject&gt; mapper = new DefaultLineMapper&lt;&gt;();
          DelimitedLineTokenizer delimitedLineTokenizer = new DelimitedLineTokenizer();
          delimitedLineTokenizer.setDelimiter(DELIMITER);
          mapper.setLineTokenizer(delimitedLineTokenizer);
          mapper.setFieldSetMapper(new MyObjectFieldSetMapper());
          return mapper;
      }
  }
  </code></pre>
  
  <p>I tried to implement <code>StepExecutionListener</code> in <code>StepReader</code> but with no luck, I think because the <code>reader</code> method in <code>StepBuilderFactory</code> is expecting an <code>ItemReader</code> from the <code>reader.read()</code> method and it doesn't care about the rest of the class.</p>
  
  <p>I'm looking for ideas or solution to be able to stop the entire job (not fail it) when <code>NoInputFileException</code> is catched.</p>