This week's book giveaway is in the Artificial Intelligence and Machine Learning forum.
We're giving away four copies of TensorFlow 2.0 in Action and have Thushan Ganegedara on-line!
See this thread for details.
Win a copy of TensorFlow 2.0 in Action this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

How to set timeout in a method in Java and retry method for a periodic amount of time

 
Ranch Hand
Posts: 167
1
Oracle Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

I have written the following until now:

       
 
Bartender
Posts: 1284
39
IBM DB2 Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if im understanding this properly then when future.cancel is true the connection failed and when its false the connection succeeded.

If i understood well then what i would suggest doing id putting the whole process in a do...while loop like this
you will need to initialise a conter set to 0 and add 1 to it in the loop

so in total



I hope i understood well and this helps
 
If you two don't stop this rough-housing somebody is going to end up crying. Sit down and read this tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic