jsch session get lock に基づいて SShClient 経由でコマンドを実行する場合:
Exec thread devapp090@1046" prio=5 tid=0x14 nid=NA runnable
java.lang.Thread.State: RUNNABLE
at java.io.FileInputStream.readBytes(FileInputStream.java:-1)
at java.io.FileInputStream.read(FileInputStream.java:220)
at java.io.BufferedInputStream.read1(BufferedInputStream.java:256)
at java.io.BufferedInputStream.read(BufferedInputStream.java:317)
- locked <0x420> (a java.io.BufferedInputStream)
at com.jcraft.jsch.ChannelSession.run(ChannelSession.java:245)
at java.lang.Thread.run(Thread.java:662)
ここでクライアントを呼び出す方法:
client.openSession();
String[] result = client.execCommands(new String[]{"ps ax|grep karaf"});
client.openSession();
ここで実行方法:
public String[] execCommands(String[] commands) throws JSchException, IOException {
String[] results = new String[commands.length];
for (int j = 0; j < commands.length; j++) {
ChannelExec channel = (ChannelExec) session.openChannel("exec");
channel.setInputStream(System.in, true);
channel.setCommand(commands[j]);
channel.connect();
InputStream in = channel.getInputStream();
byte[] tmp = new byte[1024];
StringBuilder result = new StringBuilder();
long startTime = System.currentTimeMillis();
while (System.currentTimeMillis() - startTime < timeout) {
while (in.available() > 0 && System.currentTimeMillis() - startTime < timeout) {
int i = in.read(tmp, 0, 1024);
if (i < 0) break;
result.append(new String(tmp, 0, i));
if (isLoggerEnabled)
LOGGER.info(new String(tmp, 0, i));
}
if (channel.isClosed()) {
if (isLoggerEnabled)
LOGGER.info("exit-status: " + channel.getExitStatus());
break;
}
}
in.close();
channel.disconnect();
results[j] = result.toString();
}
return results;
}
セッション メソッドのオープンとクローズ:
public void openSession() throws JSchException {
JSch jsch = new JSch();
Session session = jsch.getSession(user, host, port);
session.setUserInfo(new SSHUserInfo(user, password));
session.setTimeout(timeout);
session.connect();
this.session = session;
}
public void closeSession() {
if (session != null && session.isConnected()) {
session.disconnect();
}
}
入力を閉じて、チャネルとセッションから切断しています。しかし、何も役に立ちません。そして、何がロックされているのかわからない。私が正しく理解していれば、ChannelSession.java の System.in からの読み取りがロックされています。アイデアはありますか?