What's the Scala way to implement a retry-able call like this one?

前端 未结 14 1733
甜味超标
甜味超标 2020-11-29 16:08

Still the newbie in Scala and I\'m now looking for a way to implement the following code on it:

@Override
public void store(InputStream source, String destin         


        
14条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-29 17:09

    There is an existing library that can help with that, called retry, and there is a Java library too, called guava-retrying.

    Here are some examples of using retry:

    // retry 4 times
    val future = retry.Directly(4) { () => doSomething }
    
    // retry 3 times pausing 30 seconds in between attempts
    val future = retry.Pause(3, 30.seconds) { () => doSomething }
    
    // retry 4 times with a delay of 1 second which will be multipled
    // by 2 on every attempt
    val future = retry.Backoff(4, 1.second) { () => doSomething }
    

提交回复
热议问题