cancel
Showing results forย 
Search instead forย 
Did you mean:ย 
Data Engineering
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!

Welcome to Databricks Community: Lets learn, network and celebrate together

Join our fast-growing data practitioner and expert community of 80K+ members, ready to discover, help and collaborate together while making meaningful connections. 

Click here to register and join today! 

Engage in exciting technical discussions, join a group with your peers and meet our Featured Members.