Home Getting java.io.IOException: No such file or directory intermittently
Reply: 0

Getting java.io.IOException: No such file or directory intermittently

user4815
1#
user4815 Published in August 15, 2018, 11:16 am

I have a spring scheduled job which runs every minute. Whenever a predefined criteria matches, it does following.

  1. Creates a directory
  2. Writes a bunch of files into it in a loop
  3. Zips it
  4. Copies the zip into another location
  5. Deletes the directory

Psuedo-code for above.

// id is numeric primary key. so it is guarenteed to be unique and contains no special characters
final List<AttachmentDTO> attachmentDtos = service.getAttachments(id);

if (!attachmentDtos.isEmpty()) {
    final File directoryFolder = new File(properties.getZIPFolder() + fileSeperator+ id);

    // create directory
    if (!directoryFolder.exists()) {
        logger.debug("Creating directory : {}", directoryFolder);
        final boolean isDirectoryCreated = directoryFolder.mkdir();
        logger.debug("Directory: {}, created : {}", directoryFolder, isDirectoryCreated);
        }

    // write files into directory
    for (final AttachmentDTO dto : attachmentDtos) {
        final byte[] dataFromGeneva = util.getStream(dto.getFileKey);
        final String fileName = dto.getFilename();

        final File file = new File(directoryFolder, fileName);
        logger.debug("getAbsolutePath ={}",file.getAbsolutePath());

        if (!file.exists()) {
            final boolean isFileCreated = file.createNewFile();
            logger.info("Is new file created : {}", isFileCreated);
        }

        try (FileOutputStream fostream = new FileOutputStream(file)) {
            IOUtils.write(data, fostream);
            fostream.flush();
        }
    }

    // Zip the directory
    final String zipFilePath = FileTransferUtil.writeZipFile(directoryFolder, properties.getZIPFileFolder());
    FileTransferUtil.copyZip(zipFilePath);
    FileUtils.deleteDirectory(directoryFolder);
}

This piece of code has been working fine for a long time, but recently started giving problems. In the for loop it works fine for few files and then suddenly it throws IOException.

Sample logs,

2018-04-16 12:17:39,385 - DEBUG - [pool-1-thread-1] com.test.Test.downloadAttachments(Test.java:250) : Creating directory : /app/sharedPath/zipfilefolder/125201
2018-04-16 12:17:39,386 - DEBUG - [pool-1-thread-1] com.test.Test.downloadAttachments(Test.java:252) : Directory: /app/sharedPath/zipfilefolder/125201, created : true
2018-04-16 12:17:40,306 - DEBUG - [pool-1-thread-1] com.test.Test.downloadAttachments(Test.java:269) : getAbsolutePath=/app/sharedPath/zipfilefolder/125201/sample file 1.pdf
2018-04-16 12:17:40,306 - INFO - [pool-1-thread-1] com.test.Test.downloadAttachments(Test.java:272) : Is new file created : true
2018-04-16 12:17:40,442 - DEBUG - [pool-1-thread-1] com.test.Test.downloadAttachments(Test.java:269) : getAbsolutePath=/app/sharedPath/zipfilefolder/125201/sample file 2.pdf
2018-04-16 12:17:40,442 - INFO - [pool-1-thread-1] com.test.Test.downloadAttachments(Test.java:272) : Is new file created : true
2018-04-16 12:17:40,793 - DEBUG - [pool-1-thread-1] com.test.Test.downloadAttachments(Test.java:269) : getAbsolutePath=/app/sharedPath/zipfilefolder/125201/sample file 3.pdf
2018-04-16 12:17:40,794 - ERROR - [pool-1-thread-1] com.test.sp.jobs.helper.SPJobHelper.handleError(SPJobHelper.java:80) : errMsg=java.io.IOException: No such file or directory
    at java.io.UnixFileSystem.createFileExclusively(Native Method)
    at java.io.File.createNewFile(File.java:1012)
    at com.test.Test.downloadAttachments(Test.java:271)
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:711)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
  1. If it was a path or access issue then it wouldnt work for the first few iterations.
  2. I thought since the file name has space in it "sample file 1.pdf", that might mess up the path. But thats untrue. It seems to be working just fine for other iterations and retries.
  3. There is no concurrency here; only 1 thread is writing, reading, deleting on that directory.
  4. After the scheduled job fails, we reset the criteria in DB so that it is picked up again. This time all the files go through. So it doesnt look like a problem with the file parameters.

The job is running on a linux machine.

I have gone through similar questions but did not find anything useful. Even tried to find the native implementation for UnixFileSystem.createFileExclusively(). I have no clue on why system is throwing this IOException intermittently. Hope someone can point out what am missing here. Thanks!

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.307085 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO