Venkatesh
Venkatesh

Reputation: 83

Can I configure JSch to automatically reconnect on connection failures?

I am using the JSch API for Java for SFTP connections. Sometimes the 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?

Upvotes: 2

Views: 4791

Answers (1)

Paŭlo Ebermann
Paŭlo Ebermann

Reputation: 74750

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.

Upvotes: 5

Related Questions