jsch 会话是否拥有错误流,当它被设置时,没有其他线程可以使用它?

Does jsch session own error stream, when it is set, and no other thread can use it?

我正在为 运行 远程命令创建 jsch 会话,我遇到了一个奇怪的行为。 在我创建会话的线程之一中,将输出流设置为 System.err 并使用会话。

ChannelExec channel = (ChannelExec) session.openChannel("exec");
channel.setErrStream(System.err);

在另一个线程中,我有一些东西使用 Java util 的记录器(默认情况下将日志写入 System.err)。 创建 jsch 会话后,我不再在第二个线程的控制台中看到记录器消息。当我删除此行时:

channel.setErrStream(System.err);

或者将通道的错误流设置为 null,来自我的第二个线程的记录器消息显示在控制台中就好了。

如果jsch会话在设置时拥有System.err流,这就是记录器不能使用的原因,那么当会话的错误流设置为空时会发生什么?如果不是这种情况,那么为什么在会话开始后使用 System.err 不显示记录器消息?

如果有人能解释一下,那将非常有用。

您可以找到 Jsch javadoc 的联机副本 here. This page documents ChannelExec. The description of the single-argument version of setErrStream() 说(强调):

public void setErrStream(OutputStream out)

Sets the error stream. The standard error output of the remote process will be sent to this stream. The stream will be closed on Channel.disconnect(com.jcraft.jsch.Session).

换句话说,将System.err传递给此函数将导致Jsch在断开频道时将其关闭。一旦 System.err 关闭,程序的其他部分(例如记录器)将无法写入 System.err.

文档描述了 another version of setErrorStream(),它有两个参数:

public void setErrStream(OutputStream out, boolean dontclose)

Sets the error stream. The standard error output of the remote process will be sent to this stream.

Parameters:

dontclose - if true, we do not close the stream on Channel.disconnect(com.jcraft.jsch.Session).

换句话说,这个版本的函数可以让你控制在断开通道时是否应该关闭错误流。