title: Using end in single step batch job in Spring Batch
body: <p>We have configured batch job with single step. The general structure is like,</p>
  
  <pre><code>&lt;batch:job id="mainJob"&gt;
      &lt;batch:step id="step1"&gt;
          &lt;batch:tasklet&gt;
              &lt;batch:chunk reader="fileReader" writer="jmsItemWriter" &gt;
                  &lt;batch:retryable-exception-classes&gt;
                      &lt;batch:include class="org.springframework.dao.CannotAcquireLockException"/&gt;
                  &lt;/batch:retryable-exception-classes&gt;
              &lt;/batch:chunk&gt;
          &lt;/batch:tasklet&gt;
          &lt;batch:end on="UNKNOWN"/&gt;
          &lt;batch:next on="*" to="step1"/&gt;
          &lt;batch:listeners&gt;
              &lt;batch:listener ref="listener1"/&gt;
              &lt;batch:listener ref="listener2"/&gt;
          &lt;/batch:listeners&gt;
  
      &lt;/batch:step&gt;
     &lt;batch:listeners&gt;
          &lt;batch:listener ref="jobListener"/&gt;
     &lt;/batch:listeners&gt;
    &lt;/batch:job&gt;
  </code></pre>
  
  <p>fileReader is FlatFileItemReader which reads a file line by line and passes each line to 'listener2'. In listener2, if there is error in the line it should set the status to 'unknown' (not failed, because the job should not be started again) and then stop the job.</p>
  
  <p>To accomplish this, we have.</p>
  
  <pre><code>&lt;batch:end on="UNKNOWN"/&gt;
  &lt;batch:next on="*" to="step1"/&gt;
  </code></pre>
  
  <p>But the batch job doesn't end on Unknown and goes into the loop because of 'next'. There is an exception if 'next' is not mentioned.</p>
  
  <p>How can we resolve this?</p>
  
  <p>Edit:</p>
  
  <p>Some more explanation for Dean's answer.</p>
  
  <p>Listener2 gets FieldSet i.e. file entries line by line. While processing these entries, if some conditions (which involve database queries also) are met, the job should not read further file and stop. As the processing is done in override method 'mapFieldSet' which doesn't throw any exception, new exception can't be used and returning null just reads next line. Generally the files have thousands of line but within first 2 lines it is determined if further reading is necessary or not.</p>