java JSch 会话超时限制
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35009009/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JSch session timeout limit
提问by Khmelevskikh Alex
I'm using JSch 0.1.50 to set up a connection to the remote server for my CI Jenkins plugin. Let's assume I'm trying to use here session.connect(60000);
for the timeout 60 sec:
我正在使用 JSch 0.1.50 为我的 CI Jenkins 插件设置到远程服务器的连接。让我们假设我试图在这里使用session.connect(60000);
超时 60 秒:
Session session = null;
try {
JSch jsch = new JSch();
if (rsaIdentity != null && !rsaIdentity.equals("")) {
jsch.addIdentity(rsaIdentity.trim());
}
session = jsch.getSession(serverLogin, serverHost, Integer.parseInt(serverPort));
session.setPassword(getDescriptor().getOpenPassword(encryptedPasswordString));
session.setConfig("StrictHostKeyChecking", "no"); // not use RSA key
int timeOut = Integer.parseInt(getDescriptor().getConnectionTimeOut());
session.connect(60000);
} catch (SocketTimeoutException e) {
logger.error(e.getMessage());
return false;
} catch (JSchException e) {
logger.error(e.getMessage());
return false;
}
But in fact during the execution of this code during the connection to pretty slow sever I'm facing the timeout Exception
in approximately 20 seconds every time:
但实际上,在连接到非常慢的服务器期间执行此代码期间,我Exception
每次都面临大约 20 秒的超时:
2016-01-25 13:15:55.982 [INFO] Connecting to server: devsrv26:22 as [user] ...
2016-01-25 13:16:16.991 [ERROR] java.net.ConnectException: Connection timed out: connect
2016-01-25 13:16:16.992 com.jcraft.jsch.JSchException: java.net.ConnectException: Connection timed out: connect
2016-01-25 13:16:16.992 at com.jcraft.jsch.Util.createSocket(Util.java:389)
2016-01-25 13:16:16.993 at com.jcraft.jsch.Session.connect(Session.java:215)
2016-01-25 13:16:16.993 at com.mycomp.jenkins.MyPlugin.perform(MyPlugin.java:225)
76991-55982=21008 msec
76991-55982=21008 毫秒
Does anyone know what is the reason for this 20 seconds timeout?
有谁知道这 20 秒超时的原因是什么?
回答by Martin Prikryl
If you check how the Util.createSocket
is implemented, you will see that the timeout
defines an upper limit of the connection only, not a lower limit, because the timeout
is strangely not passed to an underlying Socket
.
如果您检查 是如何Util.createSocket
实现的,您将看到timeout
仅定义了连接的上限,而不是下限,因为timeout
奇怪的是没有传递给底层Socket
.
Those 20 seconds is probably an OS-level default limit.
这 20 秒可能是操作系统级别的默认限制。
To override it, try implementing the SocketFactory
and attach it to the session using the Session.setSocketFactory
.
要覆盖它,请尝试SocketFactory
使用Session.setSocketFactory
.
In the factory use the Socket.connect(SocketAddress endpoint, int timeout)
.
在工厂中使用Socket.connect(SocketAddress endpoint, int timeout)
.
Something like:
就像是:
public class SocketFactoryWithTimeout implements SocketFactory {
public Socket createSocket(String host, int port) throws IOException,
UnknownHostException
{
socket=new Socket();
int timeout = 60000;
socket.connect(new InetSocketAddress(host, port), timeout);
return socket;
}
public InputStream getInputStream(Socket socket) throws IOException
{
return socket.getInputStream();
}
public OutputStream getOutputStream(Socket socket) throws IOException
{
return socket.getOutputStream();
}
}