title: Spring Integration RecursiveDirectoryScanner gives too many files open exception
body: <p>I am using Spring Integration RecursiveDirectoryScanner to scan a directory recursively to process the incoming file that will be placed under the configured directory (/home/test). </p>
  
  <p>I am frequently getting the below error:</p>
  
  <pre><code>    ERROR org.springframework.integration.handler.LoggingHandler - java.lang.IllegalArgumentException: java.nio.file.FileSystemException: /home/test: Too many open files
          at org.springframework.integration.file.RecursiveDirectoryScanner.listFiles(RecursiveDirectoryScanner.java:89)
          at org.springframework.integration.file.FileReadingMessageSource.scanInputDirectory(FileReadingMessageSource.java:387)
          at org.springframework.integration.file.FileReadingMessageSource.doReceive(FileReadingMessageSource.java:361)
          at org.springframework.integration.file.FileReadingMessageSource.doReceive(FileReadingMessageSource.java:90)
          at org.springframework.integration.endpoint.AbstractMessageSource.receive(AbstractMessageSource.java:134)
          at org.springframework.integration.endpoint.SourcePollingChannelAdapter.receiveMessage(SourcePollingChannelAdapter.java:224)
          at org.springframework.integration.endpoint.AbstractPollingEndpoint.doPoll(AbstractPollingEndpoint.java:245)
          at org.springframework.integration.endpoint.AbstractPollingEndpoint.access$000(AbstractPollingEndpoint.java:58)
          at org.springframework.integration.endpoint.AbstractPollingEndpoint$1.call(AbstractPollingEndpoint.java:190)
          at org.springframework.integration.endpoint.AbstractPollingEndpoint$1.call(AbstractPollingEndpoint.java:186)
          at org.springframework.integration.endpoint.AbstractPollingEndpoint$Poller$1.run(AbstractPollingEndpoint.java:353)
          at org.springframework.integration.util.ErrorHandlingTaskExecutor$1.run(ErrorHandlingTaskExecutor.java:55)
          at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
          at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
          at java.lang.Thread.run(Thread.java:748)
  
  Caused by: java.nio.file.FileSystemException: /home/test: Too many open files
          at sun.nio.fs.UnixException.translateToIOException(UnixException.java:91)
          at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:102)
          at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:107)
          at sun.nio.fs.UnixFileSystemProvider.newDirectoryStream(UnixFileSystemProvider.java:427)
          at java.nio.file.Files.newDirectoryStream(Files.java:457)
          at java.nio.file.FileTreeWalker.visit(FileTreeWalker.java:300)
          at java.nio.file.FileTreeWalker.walk(FileTreeWalker.java:322)
          at java.nio.file.FileTreeIterator.&lt;init&gt;(FileTreeIterator.java:72)
          at java.nio.file.Files.walk(Files.java:3574)
          at org.springframework.integration.file.RecursiveDirectoryScanner.listFiles(RecursiveDirectoryScanner.java:73)
  </code></pre>
  
  <p>My Spring Integration flow is as below:</p>
  
  <p><strong>Configuration in XML</strong></p>
  
  <pre><code> &lt;task:executor id="pollerPool"
          pool-size="${pollerThreadPoolSize}"
          queue-capacity="${pollerThreadQueueCapacity}" rejection-policy="ABORT" /&gt;
  
      &lt;task:executor id="fileHandlerPool"
          pool-size="${fileHandlerPoolSize}"
          queue-capacity="${fileHandlerPoolThreadQueueCapacity}" rejection-policy="CALLER_RUNS" /&gt;
  
  
       &lt;bean id="iFilter" class="org.springframework.integration.file.filters.ChainFileListFilter"&gt;
          &lt;constructor-arg&gt;
               &lt;list&gt;
                    &lt;bean id="lastModifiedFileListFilter" class="org.springframework.integration.file.filters.LastModifiedFileListFilter"&gt;
                       &lt;property name="age" value="120" /&gt;
                    &lt;/bean&gt;
                   &lt;ref bean="acceptOnceFileListFilter"/&gt;
  
                   &lt;bean class="org.springframework.integration.file.filters.RegexPatternFileListFilter"&gt;
                      &lt;constructor-arg value="^.*\.(txt|csv|xls|xlsx|asc)$"/&gt;
                   &lt;/bean&gt;
  
              &lt;/list&gt;
          &lt;/constructor-arg&gt;
        &lt;/bean&gt;
  
       &lt;bean id="acceptOnceFileListFilter" name="acceptOnceFileListFilter" class="org.springframework.integration.file.filters.AcceptOnceFileListFilter" primary="true" /&gt;
  
       &lt;bean id="recursiveDirectoryScanner" class="org.springframework.integration.file.RecursiveDirectoryScanner"&gt;
          &lt;property name="filter" ref="iFilter" /&gt;
          &lt;property name="locker" ref="nioFileLocker" /&gt;
       &lt;/bean&gt;
  
      &lt;bean id="nioFileLocker" class="org.springframework.integration.file.locking.NioFileLocker" /&gt;
  
      &lt;int-file:inbound-channel-adapter
          id="fileSource" channel="fileReceivedChannel" auto-startup="true"
          directory="file:${polling.directory}" 
          scanner="recursiveDirectoryScanner"  &gt;
          &lt;int:poller task-executor="pollerPool"
              fixed-rate="${pollerFixedRate}"
              receive-timeout="${pollerReceiveTimeout}"&gt;
          &lt;/int:poller&gt;
      &lt;/int-file:inbound-channel-adapter&gt;
  </code></pre>
  
  <p>Dynamic parameters are as below:</p>
  
  <ul>
  <li>polling.directory=/home/test 
  pollerThreadPoolSize=1
  pollerThreadQueueCapacity=10 
  pollerFixedRate=5000
  pollerReceiveTimeout=5000 
  fileHandlerPoolSize=2
  fileHandlerPoolThreadQueueCapacity=100</li>
  </ul>
  
  <p>EDIT:</p>
  
  <p>I do unlock file in a service activator that comes in to picture when a file is picked. I get some information from file and unlock it.</p>
  
  <pre><code>@Autowired
    NioFileLocker nioFileLocker;
  
     protected void doTransform(Message&lt;?&gt; message) throws Exception {
    MessageBuilder&lt;File&gt; payload = (MessageBuilder&lt;File&gt;) message.getPayload();
      File inFile = payload.getPayload();
     try {
        nioFileLocker.unlock(inFile);
      } catch (Exception e) {
        LOGGER.error("file not unlock");
      }
   }
  </code></pre>
  
  <p>Is there any issue with the configuration ? How do I make sure this exception never appear again ?</p>
  
  <p>Thank you in advance.</p>