我有一个春季预定的工作,每分钟运行一次。每当预定义的标准匹配时,它就会跟随。
上面的Psuedo代码。
// 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);
}
这段代码已经很好地工作了很长时间,但最近开始出现问题。在for循环中,它适用于少数文件,然后突然抛出IOException。
示例日志,
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)
作业在linux机器上运行。
我遇到了类似的问题,但没有找到任何有用的东西。甚至试图找到UnixFileSystem.createFileExclusively()的本机实现。我不清楚为什么系统会间歇性地抛出这个IOException。希望有人能指出这里缺少的东西。谢谢!