title: best practice for directory polling
body: <p>I have to do batch processing to automate business process. I have to poll directory at regular interval to detect new files and do processing. While old files is being processed, new files can come in. For now, I use quartz scheduler and thread synchronization to ensure that only one thread can process files.</p>
  
  <p>Part of the code are: <br/></p>
  
  <p>application-context.xml<br/></p>
  
  <pre><code>&lt;bean id="methodInvokingJob"
    class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"&gt;&lt;br/&gt;
    &lt;property name="targetObject" ref="documentProcessor" /&gt;&lt;br/&gt;
    &lt;property name="targetMethod" value="processDocuments" /&gt;&lt;br/&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>DocumentProcessor<br/>
  .....
  <br/></p>
  
  <pre><code>public void processDocuments() { 
    LOG.info(Thread.currentThread().getName() + " attempt to run.");
    if (!processing) {
       synchronized (this) {
          try {
             processing = true;
             LOG.info(Thread.currentThread().getName() + " is processing");
             List&lt;String&gt; xmlDocuments = documentManager.getFileNamesFromFolder(incomingFolderPath);               
             // loop over the files and processed unlock files.
             for (String xmlDocument : xmlDocuments) {
                processDocument(xmlDocument);
             }
          }
          finally {
             processing = false;
          }
       }
    }
  }
  </code></pre>
  
  <p>For the current code, I have to prevent other thread to process files when one thread is processing. Is that a good idea ? or we support multi-threaded processing. In that case how can I know which files is being process and which files has just arrived ? Any idea is really appreciated.</p>