If I use JSch from more than one thread, how should I use it
As any other non-thread safe class.
Access it from a single thread at a time only.
Use synchronized
statement:
https://docs.oracle.com/javase/tutorial/essential/concurrency/locksync.html
If this downgrades performance, you can create a connection pool.
Though I do not think this exception is caused by a concurrent access.
It's rather that the server rejects too frequent connection attempts from the same host (what is quite common).
Just to complement @Martin Prikryl answer that actually solved my problem.
In my case it was the server that didn't allow more than 20 simultaneous connections. After spending 4 hours, I finally talked to the infrastructure team and they increased the maximum SSH connections on the Linux server to 50.
For the record, the steps are (you'll need root access on the server):
Edit the
/etc/ssh/sshd_config
file and set theMaxSessions
parameter to50
connections (in my case).In the same file set the
MaxStartups
parameter to50/30/50
.Restart the SSH service (or restart the whole box).