title: Spring Batch: Always looking for files with MultiResourceItemReader
body: <p>I'm a newbie in Spring Batch. I have <em>inherited</em> a batch process implemented with Spring Batch.
  This works well, except for one thing I'll try to describe.</p>
  
  <p>I launch <em>parseJob</em> and, when it's reading XML to process in bean <em>parsingStepReader</em>,
  read() method is been invoking always.</p>
  
  <p>The directory *path_to_xml* contains only one XML, invoke read() and return XML parsed, which is processed OK. Then, read() method is invoked again, return a null object, and is invoked again, return null... and so on.</p>
  
  <p>When debugging, MultiResourceItemReader <em>read</em> method try to read, does not read anything (all resources has already been readed), increment currentResources and return null.</p>
  
  <p>I have readed something about the job stops when the reader return a null object, but that <em>read</em> method returns null and reads again and again...
  I changed restartable to false, but does not work.</p>
  
  <p>The job is launched in Linux, batch mode, with org.springframework.batch.core.launch.support.CommandLineJobRunner
  Because of this problem, the .sh that launch the job does not finish, and resources are busy.</p>
  
  <p>How can I avoid this, or stop the job when resources (XML) input directory have already been processed? 
  Any help would be very appreciated. Best regards.</p>
  
  <p>Beans file and Java class pieces are attached</p>
  
  <pre><code>&lt;batch:job id="parseJob" restartable="true" incrementer="jobParametersIncrementer"&gt;
      &lt;batch:flow parent="parseFlow"/&gt;
      &lt;batch:flow .../&gt;
      &lt;batch:flow .../&gt;
  &lt;/batch:job&gt;
  
  &lt;batch:flow id="parseFlow"&gt;
      &lt;batch:step id="parsingStep"&gt;
          &lt;batch:tasklet start-limit="100" allow-start-if-complete="true" transaction-manager="..." task-executor="taskExecutor" throttle-limit="$..."&gt;
              &lt;batch:chunk reader="parsingStepReader" writer="..." processor="..." commit-interval="..." skip-limit="10000000"&gt;
                  &lt;batch:skippable-exception-classes&gt;
                      &lt;batch:include class="java.lang.Exception" /&gt;
                  &lt;/batch:skippable-exception-classes&gt;
              &lt;/batch:chunk&gt;
              &lt;batch:listeners&gt;
                  &lt;batch:listener ref="iwListener" /&gt;
                  &lt;batch:listener ref="mySkipListener" /&gt;
                  &lt;batch:listener ref="myStep1Listener" /&gt;
              &lt;/batch:listeners&gt;
              &lt;batch:no-rollback-exception-classes&gt;
                  &lt;batch:include class="java.lang.Exception" /&gt;
              &lt;/batch:no-rollback-exception-classes&gt;
          &lt;/batch:tasklet&gt;
      &lt;/batch:step&gt;
  &lt;/batch:flow&gt;
  
  &lt;!-- --&gt;
  
  &lt;bean id="bpfReader" class="org.springframework.batch.item.xml.StaxEventItemReader" scope="prototype"&gt;
      &lt;property name="fragmentRootElementName" value="..." /&gt;
      &lt;property name="unmarshaller" ref="..." /&gt;
      &lt;property name="strict" value="false" /&gt;
  &lt;/bean&gt;
  
  &lt;bean id="multiresourceItemReader" class="...SyncMultiResourceItemReader" abstract="true"&gt;
      &lt;property name="strict" value="false" /&gt;
      &lt;property name="delegate" ref="bpfReader" /&gt;
  &lt;/bean&gt;
  
  &lt;bean id="parsingStepReader" parent="multiresourceItemReader" scope="step"&gt;
      &lt;property name="resources" value="&lt;path_to_xml&gt;" /&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>And the reader class is:</p>
  
  <pre><code>public class SyncMultiResourceItemReader&lt;T&gt; extends MultiResourceItemReader&lt;T&gt; {
      . . .
  
      @Override
      public T read() throws Exception, UnexpectedInputException, ParseException {
          synchronized (this) {
              return super.read();
          }
      }
  
      . . .
  }
  </code></pre>
  
  <p><strong>UPDATE</strong>: Solution suggested by @vsingh works perfectly. Once an input element is chosen, it must be removed from the input. I don't know why, but class <em>org.springframework.batch.item.file.MultiResourceItemReader</em> does not work as I expected, especially in an input error.</p>
  
  <p>I hope this helps. Best regards</p>