关于Smart Upload文件上载应用组件引起CPU资源100%占用问题
文件上载应用引起CPU资源100%占用问题
问题现象:
1.不定时出现内存异常增长、GC无法释放并导致JVM崩溃现象,JVM Heap堆被Upload类及其中间数据占满。
2.偶尔出现CPU占用率异常,达到100%,但没有发生JVM崩溃,其他应用也能较正常进行,JVM中有多个Upload进程在持续运行。
问题出现的源头:1.网络传输延迟/错误
2.操作人员连续多次点击,造成同一文件在数秒内多次上传引起的传输错误。
问题出现的原因:
应用服务器之前装有IHS HTTP服务器,用户在访问过程中,在没有得到后台服务器相应的情况下断开连接,而重新发起新的请求。但由于IHS HTTP中间起着缓冲的作用,客户强行断开连接的异常,后台并不能及时得到,而认为连接正常,导致SmartUpload类持续读InputStream(造成CPU占用异常), 或读取了不完整的文件内容在处理过程中无法结束(造成内存泄漏)。
package com.jspsmart.upload;
……
public class SmartUpload
{
……
public void upload()
throws SmartUploadException, IOException, ServletException
{
int totalRead = 0;
int readBytes = 0;
long totalFileSize = 0L;
boolean found = false;
……
//////// ----- Begin ----- //// Add for protecting Endless Do While Clause
int protectEndlessDoWhile = 0;
//////// ----- End ----- //// Add for protecting Endless Do While Clause
//////// Add condition to determinate the InputStream status, to protect endless reading closed InputStream
//////// Original code shows as this:
//////// for(; totalRead < m_totalBytes; totalRead += readBytes)
//////// Revised code shows as:
//////// for(; totalRead < m_totalBytes && readBytes >=0; totalRead += readBytes)
for(; totalRead < m_totalBytes && readBytes >=0; totalRead += readBytes)
try
{
m_request.getInputStream();
readBytes = m_request.getInputStream().read(m_binArray, totalRead, m_totalBytes - totalRead);
}
catch(Exception e)
{
throw new SmartUploadException("Unable to upload.");
}
……
do
{
//////// ----- Begin ----- //// Add for protecting Endless Do While Clause
protectEndlessDoWhile ++;
//////// ----- End ----- //// Add for protecting Endless Do While Clause
//////// Add condition to protect endless multiple files processing
//////// Original code shows as this:
//////// if(m_currentIndex >= m_totalBytes)
//////// Revised code shows as:
//////// if(m_currentIndex >= m_totalBytes || protectEndlessDoWhile >20)
if(m_currentIndex >= m_totalBytes || protectEndlessDoWhile >20)
break;
dataHeader = getDataHeader();
m_currentIndex = m_currentIndex + 2;
isFile = dataHeader.indexOf("filename") > 0;
fieldName = getDataFieldValue(dataHeader, "name");
……
} while(true);
}
……
private Request m_formRequest;
}
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/8819325/viewspace-902925/ ,如需转载,请注明出处,否则将追究法律责任。