开发者

Can I configure JSch to automatically reconnect on connection failures?

开发者 https://www.devze.com 2023-04-06 22:55 出处:网络
I am using the JSch API for Java for SFTP connections. Sometimes t开发者_如何转开发he server may be down for a second or the connection may be busy. In these cases I would need to re-connect to the se

I am using the JSch API for Java for SFTP connections. Sometimes t开发者_如何转开发he server may be down for a second or the connection may be busy. In these cases I would need to re-connect to the server three times at least before I decide the connection has failed.

Does JSch provide any configuration option to do this automatically?


JSch has no such configuration option, but you can simply do this yourself.

Session s = new Session(...);
for(int i = 0; i < MAX_TRIES; i++) {
    try {
       s.connect();
       break;
    }
    catch(JSchException ex) {
       if(i == MAX_TRIES - 1)
           throw ex;
       continue;
    }
}

After executing this block, either the session is connected or a JSchException is thrown.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号