【发布时间】:2017-05-20 10:34:03
【问题描述】:
我正在使用带有线程组(不同会话)的 JSch 从 SFTP 服务器并行获取不同目录中的文件列表,我正面临:
"FileProcessThread4" prio=10 tid=0x00007fd826c9e000 nid=0x1e0c waiting for monitor entry [0x00007fd841824000]
java.lang.Thread.State: BLOCKED (on object monitor)
问题是因为 JSch 0.1.54 还是并行?
使用 find 命令获取最近 10 分钟的旧文件,
我正在使用的代码:
public static String[] getRemoteFiles(String host, String user, String password, String command, String port) {
String line = null;
Session session = null;
ChannelExec channelExec = null;
InputStream in = null;
List<String> list = null;
BufferedReader reader = null;
try {
list = new ArrayList<String>();
JSchUtil jSchUtil = new JSchUtil();
session = jSchUtil.connect(host, user, password, port);
channelExec = (ChannelExec) session.openChannel("exec");
logger.debug("SSH exec channel opened");
in = channelExec.getInputStream();
channelExec.setCommand(command);
channelExec.connect()
reader = new BufferedReader(new InputStreamReader(in));
//Here block is happens
while ((line = reader.readLine()) != null) {
list.add(line);
}
} catch (Exception exception) {
logger.error("Exception-->" + exception.getMessage(), exception);
} finally {
if (channelExec != null) {
try {
channelExec.disconnect();
} catch (Exception exception) {
logger.error("Exception-->" + exception.getMessage(), exception);
} finally {
channelExec = null;
}
}
if (session != null) {
try {
session.disconnect();
} catch (Exception exception) {
logger.error("Exception-->" + exception.getMessage(), exception);
} finally {
session = null;
}
}
if (reader != null) {
try {
reader.close();
} catch (Exception exception) {
logger.error("Exception-->" + exception.getMessage(), exception);
} finally {
reader = null;
}
}
if (in != null) {
try {
in.close();
} catch (Exception exception) {
logger.error("Exception-->" + exception.getMessage(), exception);
} finally {
in = null;
}
}
}
return list.toArray(new String[] {});
}
public class JSchUtil {
private Logger logger = Logger.getLogger(JSchUtil.class);
private static final String STRICT_HOSTKEY_CHECKIN_KEY = "StrictHostKeyChecking";
private static final String STRICT_HOSTKEY_CHECKIN_VALUE = "no";
public Session connect(String host, String username, String password, String port) throws Exception {
JSch jsch = new JSch();
Session sshSession = jsch.getSession(username, host, Integer.valueOf(port));
sshSession.setUserInfo(new UserInfo() {
public String getPassphrase() {
return null;
}
public String getPassword() {
return null;
}
public boolean promptPassphrase(String string) {
return false;
}
public boolean promptPassword(String string) {
return false;
}
public boolean promptYesNo(String string) {
return true;
}
public void showMessage(String string) {
}
});
sshSession.setPassword(password);
Properties config = new Properties();
config.put(STRICT_HOSTKEY_CHECKIN_KEY, STRICT_HOSTKEY_CHECKIN_VALUE);
sshSession.setConfig(config);
logger.debug("SSH_Connection|Host->" + host + "|Port->" + port + "|Username->" + username + "|Password->" + password);
sshSession.connect();
logger.debug("SSH_Connection|Status->Success");
return sshSession;
}
}
感谢您的建议
【问题讨论】:
-
向我们展示您的代码!
-
问题中添加的代码。
-
代码与SFTP无关
-
正确,没有 SFTP 通道,但是当我获取带有 exec 通道的列表时,线程在代码中的注释行被阻塞。
-
那么为什么你的问题是“SFTP”?为什么你甚至使用“exec”通道检索文件列表?为什么不使用 SFTP?
标签: java multithreading sftp jsch