cancel
Showing results for 
Search instead for 
Did you mean: 
Data Engineering
Join discussions on data engineering best practices, architectures, and optimization strategies within the Databricks Community. Exchange insights and solutions with fellow data engineers.
cancel
Showing results for 
Search instead for 
Did you mean: 

Using Thread.sleep in Scala

Sunny
New Contributor III

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)

}

6 REPLIES 6

ron_defreitas
Contributor

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

}

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.

Sunny
New Contributor III

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?

angrypanda
New Contributor II

Have you tried using Scala Futures?

Sounds like this is an instance of needing a synchronous blocking call, so a future wouldn't help the OP​

Vartika
Moderator
Moderator

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!

Join 100K+ Data Experts: Register Now & Grow with Us!

Excited to expand your horizons with us? Click here to Register and begin your journey to success!

Already a member? Login and join your local regional user group! If there isn’t one near you, fill out this form and we’ll create one for you to join!