简体   繁体   English

如何等到从 Java 中的 ftp 服务器下载整个文件?

[英]How to wait until whole files is downloaded from ftp server in Java?

One ThreadPool is downloading files from the FTP server and another thread pool is reading files from it.一个线程池正在从 FTP 服务器下载文件,另一个线程池正在从中读取文件。

在此处输入图像描述

Both ThreadPool are running concurrently.两个线程池同时运行。 So exactly what happens, I'll explain you by taking one example.那么究竟会发生什么,我将举一个例子来解释你。

Let's assume, I've one csv file with 100 records.假设,我有一个包含 100 条记录的 csv 文件。

While threadPool-1 is downloading and writing it in a file in pending folder, and at the same time threadpool-2 reads the content from that file, but assume in 1 sec only 10 records can be written in a file in /pending folder and threadpool - 2 reads only 10 record.虽然 threadPool-1 正在下载并将其写入挂起文件夹中的文件,同时 threadpool-2 从该文件中读取内容,但假设在 1 秒内只能将 10 条记录写入 /pending 文件夹中的文件中,并且threadpool - 2 只读取 10 条记录。

ThreadPool - 2 doesn't know about that 90 records are currently in process of downloading. ThreadPool - 2 不知道当前正在下载 90 条记录。 Now, threadPool - 2 will not read 90 records because it doesn't know that whole file is downloaded or not.现在,threadPool - 2 不会读取 90 条记录,因为它不知道是否下载了整个文件。 After reading it'll move that file in another folder.阅读后,它将将该文件移动到另一个文件夹中。 So, my 90 records will be proceed further.因此,我的 90 条记录将继续进行。

My question is, how to wait until whole file is downloaded and then only threadPool 2 can read contents from the file.我的问题是,如何等到整个文件被下载,然后只有 threadPool 2 可以从文件中读取内容。

One more thing is that both threadPools are use scheduleFixedRate method and run at every 10 sec.另一件事是两个线程池都使用scheduleFixedRate方法并每 10 秒运行一次。

Please guide me on this.请指导我。

I'm a fan of Mark Rotteveel's #6 suggestion (in comments above):我是 Mark Rotteveel 的 #6 建议的粉丝(在上面的评论中):

  • use a temporary name when downloading,下载时使用临时名称,
  • rename when download is complete.下载完成后重命名。

That looks like:看起来像:

  • FTP download threads write all files with some added extension – perhaps .pending – but name it whatever you want. FTP 下载线程写入所有文件并添加一些扩展名 - 可能是.pending - 但您可以随意命名。
  • When a file is downloaded – say some.pdf – the FTP download thread writes the file to some.pdf.pending下载文件时——比如some.pdf ——FTP 下载线程将文件写入some.pdf.pending
  • When an FTP download thread completes a file, the last step is a file rename operation – this is the mechanism for ensuring only "done" files are ready to be processed.当 FTP 下载线程完成文件时,最后一步是文件重命名操作——这是确保只有“完成”文件准备好处理的机制。 So it downloads the file to some.pdf.pending , then at the end, renames it to some.pdf .因此它将文件下载到some.pdf.pending ,然后在最后将其重命名为some.pdf
  • Reader threads look for files, ignoring anything matching *.pending阅读器线程查找文件,忽略匹配*.pending的任何内容

I've built systems using this approach and they worked out well.我已经使用这种方法构建了系统,并且效果很好。 In contrast, I've also worked with more complicated systems that tried to coordinate across threads and.. those often did not work so well.相比之下,我还使用过更复杂的系统,这些系统试图跨线程进行协调,而且……那些通常不能很好地工作。

Over time, any software system will have bugs.随着时间的推移,任何软件系统都会出现错误。 Edsger Dijkstra captured this so well: Edsger Dijkstra 很好地捕捉到了这一点:

"If debugging is the process of removing software bugs, then programming must be the process of putting them in." “如果调试是消除软件错误的过程,那么编程一定是把它们放进去的过程。”

However difficult it is to reason about program correctness now – while the program is still in design phase, and has not yet been built – it will be harder to reason about correctness when things are broken in production (which will happen, because bugs).无论现在要推理程序的正确性是多么困难——当程序仍处于设计阶段,尚未构建时——当产品在生产中出现问题时,推理正确性将更加困难(这将发生,因为错误)。 That is, when things are broken and you're under time pressure to find the root cause (and fix it,).也就是说,当事情被打破并且您面临时间压力来寻找根本原因(并修复它)时。 even the best of us would be at a disadvantage with a complicated (vs. simple) system.即使是我们中最好的人也会在复杂(相对于简单)的系统中处于劣势。

The approach of using temporary names is simple to reason about, which should minimize code complexity and thus make it easier to implement.使用临时名称的方法很容易推理,它应该最大限度地降低代码复杂性,从而使其更易于实现。 In turn, maintenance and bug fixes should be easier, too.反过来,维护和错误修复也应该更容易。

Keep it simple – let the filesystem help you out.保持简单——让文件系统帮助你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM