問題描述
我們正在使用以下 Apache Commons Net FTP 代碼連接到 FTP 服務(wù)器,輪詢一些目錄中的文件,如果找到文件,則將它們檢索到本地計(jì)算機(jī):
We are using the following Apache Commons Net FTP code to connect to an FTP server, poll some directories for files, and if files are found, to retrieve them to the local machine:
try {
logger.trace("Attempting to connect to server...");
// Connect to server
FTPClient ftpClient = new FTPClient();
ftpClient.setConnectTimeout(20000);
ftpClient.connect("my-server-host-name");
ftpClient.login("myUser", "myPswd");
ftpClient.changeWorkingDirectory("/loadables/");
// Check for failed connection
if(!FTPReply.isPositiveCompletion(ftpClient.getReplyCode()))
{
ftpClient.disconnect();
throw new FTPConnectionClosedException("Unable to connect to FTP server.");
}
// Log success msg
logger.trace("...connection was successful.");
// Change to the loadables/ directory where we poll for files
ftpClient.changeWorkingDirectory("/loadables/");
// Indicate we're about to poll
logger.trace("About to check loadables/ for files...");
// Poll for files.
FTPFile[] filesList = oFTP.listFiles();
for(FTPFile tmpFile : filesList)
{
if(tmpFile.isDirectory())
continue;
FileOutputStream fileOut = new FileOutputStream(new File("tmp"));
ftpClient.retrieveFile(tmpFile.getName(), fileOut);
// ... Doing a bunch of things with output stream
// to copy the contents of the file down to the local
// machine. Ommitted for brevity but I assure you this
// works (except when the WAR decides to hang).
//
// This was used because FTPClient doesn't appear to GET
// whole copies of the files, only FTPFiles which seem like
// file metadata...
}
// Indicate file fetch completed.
logger.trace("File fetch completed.");
// Disconnect and finish.
if(ftpClient.isConnected())
ftpClient.disconnect();
logger.trace("Poll completed.");
} catch(Throwable t) {
logger.trace("Error: " + t.getMessage());
}
我們計(jì)劃每分鐘每分鐘運(yùn)行一次.當(dāng)部署到 Tomcat (7.0.19) 時(shí),此代碼加載得非常好,并且可以順利開始工作.但每次,在某個(gè)時(shí)候,它似乎只是掛起.我的意思是:
We have this scheduled to run every minute, on the minute. When deployed to Tomcat (7.0.19) this code loads up perfectly fine and begins working without a hitch. Every time though, at some point or another, it seems to just hang. By that I mean:
- 不存在堆轉(zhuǎn)儲(chǔ)
- Tomcat 仍在運(yùn)行(我可以看到它的 pid 并且可以登錄到 Web 管理器應(yīng)用程序)
- 在管理器應(yīng)用中,我可以看到我的 WAR 仍在運(yùn)行/啟動(dòng)
catalina.out
和我的特定于應(yīng)用程序的日志顯示沒有任何異常被拋出的跡象
- No heap dumps exist
- Tomcat is still running (I can see its pid and can log into the web manager app)
- Inside the manager app, I can see my WAR is still running/started
catalina.out
and my application-specific log show no signs of any exceptions being thrown
所以 JVM 仍在運(yùn)行.Tomcat 仍在運(yùn)行,我部署的 WAR 仍在運(yùn)行,但它只是掛起.有時(shí)運(yùn)行 2 小時(shí)然后掛起;其他時(shí)候它會(huì)運(yùn)行幾天然后掛起.但是當(dāng)它掛起時(shí),它會(huì)在讀取 About to check loadables/for files...
的行(我確實(shí)在日志中看到)和讀取 File fetch 的行之間這樣做完成.
(我沒看到).
So the JVM is still running. Tomcat is still running, and my deployed WAR is still running, but its just hanging. Sometimes it runs for 2 hours and then hangs; other times it runs for days and then hangs. But when it does hang, it does so between the line that reads About to check loadables/ for files...
(which I do see in the logs) and the line that reads File fetch completed.
(which I don't see).
這告訴我在文件的實(shí)際輪詢/獲取過程中發(fā)生了掛起,這使我指向了與 this question 我能夠找到與 FTPClient 死鎖有關(guān)的問題.這讓我想知道這些問題是否相同(如果是,我會(huì)很樂意刪除這個(gè)問題!).但是我不認(rèn)為相信它們是相同的(我在我的日志中沒有看到相同的異常).
This tells me the hang occurs during the actual polling/fetching of the files, which kind of points me in the same direction as this question that I was able to find which concerns itself with FTPClient deadlocking. This has me wondering if these are the same issues (if they are, I'll happily delete this question!). However I don't think believe they're the same (I don't see the same exceptions in my logs).
一位同事提到它可能是被動(dòng)"與主動(dòng)"的 FTP 事情.不知道有什么區(qū)別,我對(duì) FTPClient 字段 ACTIVE_REMOTE_DATA_CONNECTION_MODE
、PASSIVE_REMOTE_DATA_CONNECTION_MODE
等感到有些困惑,并且不知道 SO 認(rèn)為這是一個(gè)潛在問題.
A co-worker mentioned it might be a "Passive" vs. "Active" FTP thing. Not really knowing the difference, I am a little confused by the FTPClient fields ACTIVE_REMOTE_DATA_CONNECTION_MODE
, PASSIVE_REMOTE_DATA_CONNECTION_MODE
, etc. and didn't know what SO thought about that as being a potential issue.
由于我在這里將 Throwable
作為最后的手段,因此如果出現(xiàn)問題,我本來希望在日志中看到 something.因此,我覺得這是一個(gè)明確的掛起問題.
Since I'm catching Throwable
s as a last resort here, I would have expected to see something in the logs if something is going wrong. Ergo, I feel like this is a definite hang issue.
有什么想法嗎?不幸的是,我對(duì)這里的 FTP 內(nèi)部知識(shí)知之甚少,無法做出明確的診斷.這可能是服務(wù)器端的東西嗎?跟FTP服務(wù)器有關(guān)嗎?
Any ideas? Unfortunately I don't know enough about FTP internals here to make a firm diagnosis. Could this be something server-side? Related to the FTP server?
推薦答案
這可能是很多事情,但你朋友的建議是值得的.
This could be a number of things, but your friend's suggestion would be worthwhile.
試試 ftpClient.enterLocalPassiveMode();
看看是否有幫助.
Try ftpClient.enterLocalPassiveMode();
to see if it helps.
我還建議將斷開連接放在 finally
塊中,這樣它就不會(huì)留下連接.
I would also suggest to put the disconnect in the finally
block so that it never leaves a connection out there.
這篇關(guān)于Apache Commons FTPClient 掛起的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!