我使用Jsch作为SFTP客户端从远程SFTP目录读取和写入XML文件。
我使用5秒钟的作业检查新文件是否可用于草稿,在30或40分钟循环后我得到以下错误
Caused by: java.lang.OutOfMemoryError: unable to create new native thread
at java.lang.Thread.start0(Native Method) [rt.jar:1.7.0_65]
at java.lang.Thread.start(Thread.java:714) [rt.jar:1.7.0_65]
at com.jcraft.jsch.Session.connect(Session.java:528) [jsch-0.1.53.jar:]
at com.jcraft.jsch.Session.connect(Session.java:183) [jsch-0.1.53.jar:]
这是用于创建连接的源代码
public InputStream getFile(String path){
Session session = null;
Channel channel = null;
try {
ChannelSftp sftp = openConnexion(session, channel);
return sftp.get(path);
} catch (SftpException e) {
new RuntimeException("Error detected during get file from SFTP specific path : " + e.getMessage(), e);
} finally {
closeConnexion(session, channel);
}
}
private ChannelSftp openConnexion(Session session, Channel channel) {
try {
JSch ssh = new JSch();
session = ssh.getSession("user", "hostname", 22);
session.setPassword("password");
session.setConfig("StrictHostKeyChecking", "no");
session.connect();
channel = session.openChannel(SFTP_CHANNEL);
channel.connect();
ChannelSftp sftp = (ChannelSftp) channel;
return sftp;
} catch (JSchException e) {
throw new RuntimeException("Error detected during open SFTP connexion : " + e.getMessage(), e);
}
}
private void closeConnexion(Session session, Channel channel) {
if (channel != null) {
channel.disconnect();
}
if (session != null) {
session.disconnect();
}
}
我尝试增加JVM线程堆栈的大小,并且还增加了unix =>允许的本机进程限制。同样的错误。
我使用以下命令执行此操作:
ulimit -u unlimited
我试图创建一个jsch会话池,当jsch会话未断开连接时,它是不可用的=> " SFTP错误4"
我的工作已经在jboss-as-7上部署了战争,这是JVM选项:
JAVA_OPTS="-Xms1024m -Xmx1024m -XX:MaxPermSize=256m -Xss1024k"
你对这种治疗有什么建议吗?
谢谢!
答案 0 :(得分:0)
问题是你在每次循环后都没有关闭通道和会话,这至少会泄漏用于通过SFTP执行下载的线程。
尝试关闭finally块中的会话和通道,如果它有效,很遗憾会使您尝试读取的InputStream无效;阻止您正确处理文件。
我将稍微重构代码,这应该解决资源耗尽问题,并附有评论:
// session and channel are at the object scope
Session session = null;
Channel channel = null;
public InputStream getFile(String path){
// First, close any existing connections.
try {
closeConnexion();
} catch (SftpException e) {
// You can try to handle an issue here; but it's
// probably not worth it
}
try {
ChannelSftp sftp = openConnexion();
return sftp.get(path);
} catch (SftpException e) {
new RuntimeException("Error detected during get file from SFTP specific path : " + e.getMessage(), e);
} finally {
}
}
private ChannelSftp openConnexion() {
try {
JSch ssh = new JSch();
// use the object's session variable
session = ssh.getSession("user", "hostname", 22);
session.setPassword("password");
session.setConfig("StrictHostKeyChecking", "no");
session.connect();
// use the object's channel object
channel = session.openChannel(SFTP_CHANNEL);
channel.connect();
ChannelSftp sftp = (ChannelSftp) channel;
return sftp;
} catch (JSchException e) {
throw new RuntimeException("Error detected during open SFTP connexion : " + e.getMessage(), e);
}
}
private void closeConnexion() {
// close object's channel and session
if (channel != null) {
channel.disconnect();
channel = null;
}
if (session != null) {
session.disconnect();
session = null;
}
}
如果我要重新设计它,我会返回一个容器类而不是包含channel,session和InputStream的InputStream。容器类会有一个“关闭”的容器类。方法,它会关闭InputStream,channel和session,然后我就不会在对象中存储通道和会话。