I java a java method, which makes a connection to a web service.
Sometimes this method takes too long to make the connection.
I want for example it it takes longer than 5 seconds, then to stop the current procedure and restart all over for 3 more times. If all times fail, then abort completely.
The very first thing I'd try is to verify why timeouts occur, and, possibly, fix the root cause of the problem. Supposing that fixing isn't possible for whatever reason, I'm not aware of an automatic way of retrying a call n times until it succeeds.
A first, simple approach is to loop over n retries using future and get with a timeout, breaking the loop if the connection succeedes.