【发布时间】:2018-08-04 04:20:25
【问题描述】:
嗨, 我们有两台服务器在生产环境中同时运行。有时,server1 会抛出异常“java.net.SocketException:资源暂时不可用”。以下是详细内容,
提前致谢:)
Failed to connect to ssh server [10.0.0.0:22]
org.apache.sshd.common.RuntimeSshException: Failed to get the session.
at org.apache.sshd.client.future.DefaultConnectFuture.getSession(DefaultConnectFuture.java:43)
at com.abc.OurSshClient.getSessionWithTimeOut(OurSshClient.java:328)
at com.abc.OurSshClient.connect(OurSshClient.java:228)
at com.abc.spi.ConnectionImpl.connectToServer(ConnectionImpl.java:92)
at com.abc.spi.ConnectionImpl.<init>(ConnectionImpl.java:84)
at com.abc.spi.ConnectionFactoryImpl.getInvalidConnections(ConnectionFactoryImpl.java:97)
at com.abc.resource.pool.ConnectionPoolRegistry.runHeartBeatCheck(ConnectionPoolRegistry.java:303)
at com.abc.resource.pool.ConnectionPoolRegistry$CheckConnectionThread.run(ConnectionPoolRegistry.java:498)
Caused by: java.net.SocketException: Resource temporarily unavailable
at sun.nio.ch.Net.connect0(Native Method)
at sun.nio.ch.Net.connect(Net.java:484)
at sun.nio.ch.Net.connect(Net.java:476)
at sun.nio.ch.UnixAsynchronousSocketChannelImpl.implConnect(UnixAsynchronousSocketChannelImpl.java:326)
at sun.nio.ch.AsynchronousSocketChannelImpl.connect(AsynchronousSocketChannelImpl.java:209)
at org.apache.sshd.common.io.nio2.Nio2Connector.connect(Nio2Connector.java:53)
at org.apache.sshd.SshClient.connect(SshClient.java:298)
at org.apache.sshd.SshClient.connect(SshClient.java:284)
at org.apache.sshd.SshClient.connect(SshClient.java:274)
at com.abc.OurSshClient.getSessionWithTimeOut(OurSshClient.java:317)
... 6 more
private ClientSession getSession(int connectionTimeout) throws TimeoutException, IOException {
ConnectFuture connection;
boolean hasTimedOut = true;
try {
connection = this.client.connect(username, host, port);
hasTimedOut = !connection.await(connectionTimeout);
} catch (Exception e) {
throw new IOException("Failed to connect", e);
}
if (hasTimedOut) {
this.client.stop();
this.client = null;
this.clientStarted = false;
throw new TimeoutException("TimeOut Exception while attempting to connect to [" + host + "]:[" + port + "]");
}
return connection.getSession();
}
【问题讨论】:
-
有时,server1 会抛出异常。有时什么时候?你的问题不清楚
-
我们有 2 个有效载荷 PL-1 和 PL-2。来自 PL-2 的连接正常,没有问题。但是,PL-1 无法连接到网络节点。另外,我可以从 PL-1 ping 到节点。所以没有网络问题,但超出了异常。
-
您显示的代码与堆栈跟踪不匹配。在堆栈跟踪中,
com.abc.OurSshClient.getSessionWithTimeOut引发了异常,但您的代码适用于getSession。 -
基于其他类似问题以及您使用的方法可能设置
O_NONBLOCK,该错误可能意味着您正在泄漏套接字和所有可用的套接字由于协议超时,正在使用或等待终止。使用netstat -nat监控应用程序的端口使用情况。 -
@JimGarrison 感谢您的及时回复。方法名称是 getSessionWithTimeOut 但不是 getSession.. 这是我发布代码时的拼写错误:(
标签: java socketexception sshd