JSch session timeout limit

前端 未结 1 1462
孤城傲影
孤城傲影 2021-01-04 18:06

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 t

相关标签:
1条回答
  • 2021-01-04 18:51

    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.

    Those 20 seconds is probably an OS-level default limit.

    To override it, try implementing the SocketFactory and attach it to the session using the Session.setSocketFactory.

    In the factory use the 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();
      }
    }
    
    0 讨论(0)
提交回复
热议问题