gpt4 book ai didi

java - 该进程无法访问该文件,因为该文件正在被另一个进程使用

转载 作者:行者123 更新时间:2023-12-01 21:49:56 41 4
gpt4 key购买 nike

我有一段代码可以监视目录中是否添加了文件。每当一个新文件添加到目录中时,该文件的内容就会被选取并发布到 kafka 上,然后该文件将被删除。

当我发出单个请求时,这是有效的,但一旦我将我的代码接受来自 jMeter 的 5 或 10 个用户请求,内容就会成功发布到 kafka 上,但代码无法删除该文件。我收到一个 FileSystemException ,其中包含一条消息:该进程无法访问该文件,因为该文件正在被另一个进程使用。

我猜想存在一些我无法看到的并发问题。

public void monitor() throws IOException, InterruptedException {
Path faxFolder = Paths.get(TEMP_FILE_LOCATION);
WatchService watchService = FileSystems.getDefault().newWatchService();
faxFolder.register(watchService, StandardWatchEventKinds.ENTRY_CREATE);
boolean valid = true;
do {
WatchKey watchKey = watchService.take();
for (WatchEvent<?> event : watchKey.pollEvents()) {
if (StandardWatchEventKinds.ENTRY_CREATE.equals(event.kind())) {
String fileName = event.context().toString();
publishToKafka(new File(TEMP_FILE_LOCATION + fileName).toPath(), "topic");
}
}
valid = watchKey.reset();
} while (valid);
}

private void publishToKafka(Path path, String topic) {
try (BufferedReader reader = Files.newBufferedReader(path)) {
String input = null;
while ((input = reader.readLine()) != null) {
kafkaProducer.publishMessageOnTopic(input, topic);
}
} catch (IOException e) {
LOG.error("Could not read buffered file to send message on kafka.", e);
} finally {
try {
Files.deleteIfExists(path); // This is where I get the exception
} catch (IOException e) {
LOG.error("Problem in deleting the buffered file {}.", path.getFileName(), e);
}
}
}

异常日志:

java.nio.file.FileSystemException: D:\upload\notif-1479974962595.csv: The process cannot access the file because it is being used by another process.

at sun.nio.fs.WindowsException.translateToIOException(Unknown Source)
at sun.nio.fs.WindowsException.rethrowAsIOException(Unknown Source)
at sun.nio.fs.WindowsException.rethrowAsIOException(Unknown Source)
at sun.nio.fs.WindowsFileSystemProvider.implDelete(Unknown Source)
at sun.nio.fs.AbstractFileSystemProvider.deleteIfExists(Unknown Source)
at java.nio.file.Files.deleteIfExists(Unknown Source)
at com.panasonic.mdw.core.utils.MonitorDirectory$FileContentPublisher.publishToKafka(MonitorDirectory.java:193)
at com.panasonic.mdw.core.utils.MonitorDirectory$FileContentPublisher.sendData(MonitorDirectory.java:125)
at com.panasonic.mdw.core.utils.MonitorDirectory$FileContentPublisher.run(MonitorDirectory.java:113)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

最佳答案

查看您的代码,似乎当线程选择一个文件再次发布时,另一个线程正在选择它进行发布。这就是为什么没有人能够删除它。这一定只是并发问题。您应该根据标准重新设计代码:可以同时运行的步骤和不能同时运行的步骤。所以整个过程的步骤是:

  1. 拾取一个文件(主线程应该执行此操作)
  2. 发布文件(调用其他线程来执行此操作)
  3. 删除文件(调用线程应该删除它)
  4. 检查是否存在任何文件(同样主线程可以做到这一点)

此外,选择文件后,您可以将其读入 buffer ,删除它,然后继续发布。这将确保主线程不会将此文件分配给其他线程。

关于java - 该进程无法访问该文件,因为该文件正在被另一个进程使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40779633/

41 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com