This week's book giveaway is in the Testing forum.
We're giving away four copies of Data Structures the Fun Way: An Amusing Adventure with Coffee-Filled Examples and have Jeremy Kubica on-line!
See this thread for details.
  • 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

How can I set a timeout in a method invocation?

 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys, I have a method that sends a request from another server. However, sometimes the server did not send responses. What I want is to put a timeout in my code so that if certain time has elapsed (for example 1 minute), I will just reinvoke that method until I receive a response.... Is that possible in Java? Can someone please give example/advice.

TIA
 
Saloon Keeper
Posts: 14498
325
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Only if the method you use to send a request to the server supports either timeouts or interrupts.

If it doesn't support timeouts, you'll have to perform the timeout yourself by interrupting the thread that makes the request.
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If it is J2ee application, then running thread is not recommended as per J2ee specification.

Matt Taylor wrote:Hi guys, I have a method that sends a request from another server. However, sometimes the server did not send responses. What I want is to put a timeout in my code so that if certain time has elapsed (for example 1 minute), I will just reinvoke that method until I receive a response.... Is that possible in Java? Can someone please give example/advice.

TIA

 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's an example of how you can do that using an ExecutorService and a Future.

 
Sheriff
Posts: 22701
129
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can do the same in JEE using @Asynchronous. For instance, the code that will run in the background:
The calling code:

 
Matt Taylor
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for your response. That is too advanced. We are using a spring webapp framework and a tomcat 7 server....

Can you please tell me how can I substitute my method in your example?

I have a "service.execute()" that needs timeout.
 
Whip out those weird instruments of science and probe away! I think it's a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic