(我希望我能正确理解这个问题)
我无法让我的会话保持活跃,这个问题出现在 Google 搜索结果中,所以我希望其他人会发现它有用:
@Test
public void testClientWithHeartBeat() throws Exception {
SshClient client = SshClient.setUpDefaultClient();
client.getProperties().put(ClientFactoryManager.HEARTBEAT_INTERVAL, "500");
client.start();
ClientSession session = client.connect("localhost", port).await().getSession();
session.authPassword("smx", "smx").await().isSuccess();
ClientChannel channel = session.createChannel(ClientChannel.CHANNEL_SHELL);
int state = channel.waitFor(ClientChannel.CLOSED, 2000);
assertTrue((state & ClientChannel.CLOSED) == 0);
channel.close(false);
client.stop();
}
(来源:https ://issues.apache.org/jira/browse/SSHD-185 )
在较新的版本(例如 2.8.0 版)中,启用心跳更改为CoreModuleProperties.HEARTBEAT_INTERVAL.set(client, Duration.ofMillis(500));