JSch Session Timeout Limit

I use JSch 0.1.50 to configure the connection to the remote server for my CI Jenkins plugin. Suppose I try to use session.connect(60000);60 seconds here for a timeout:

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 while connecting to a rather slow server, I encounter a timeout every time Exceptionafter about 20 seconds:

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 ms

Does anyone know what is the reason for this 20 second timeout?

+5
source share
1 answer

, Util.createSocket, , timeout , , timeout Socket.

20 , , .

, SocketFactory Session.setSocketFactory.

factory Socket.connect(SocketAddress endpoint, int timeout).

- :

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();
  }
}
+2

Source: https://habr.com/ru/post/1625953/


All Articles