Using Thread.sleep in Scala
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-17-2022 05:04 AM
We need to hit REST web service every 5 mins until success message is received. The Scala object is inside a Jar file and gets invoked by Databricks task within a workflow.
Thread.sleep(5000)
is working fine but not sure if it is safe practice or is there any alternates.
Currently I have this implementation
while (true) {
// hit the rest web service
if(response == "success")
return;
Thread.sleep(300000)
}
- Labels:
-
Jobs & Workflows
-
Scala
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-29-2022 12:24 PM
Nothing wrong with using Sleep per se, but this business logic lacks timeout logic.
If it never returns success, your job will run forever, consuming DBUs as well as cloud compute costs.
Using the scala concurrent duration classes, you can create a deadline, then write your code to incorporate that.
while (deadline.hasTimeLeft()) {
// your break logic
// sleep
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-29-2022 12:26 PM
I'll add that you may wish to hit the endpoint more frequently if your web service can take it... it's wasteful to poll once every five minutes for a response that might return at 5 minutes and 1 second.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-30-2022 05:06 AM
Thank you. For ex, if the job has a timeout set to 15 mins and the code has while(true), after 15 mins isn't the job killed automatically?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-29-2022 01:09 PM
Have you tried using Scala Futures?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-29-2022 01:33 PM
Sounds like this is an instance of needing a synchronous blocking call, so a future wouldn't help the OP
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-24-2022 09:18 AM
Hey there @Sundeep P
Hope all is well!
Just wanted to check in if you were able to resolve your issue and would you be happy to share the solution or mark an answer as best? Else please let us know if you need more help.
We'd love to hear from you.
Cheers!

