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
Databricks Employee
Databricks Employee

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!

Connect with Databricks Users in Your Area

Join a Regional User Group to connect with local Databricks users. Events will be happening in your city, and you wonโ€™t want to miss the chance to attend and share knowledge.

If there isnโ€™t a group near you, start one and help create a community that brings people together.

Request a New Group