score:0
This is an example of retry function based on Future.recoverWith If you run it you can see that it prints "run process" until the Future is successful but not more than 'times' times
object X extends App{
type Request = String
type Response = String
import scala.concurrent.ExecutionContext.Implicits.global
def retry(request: Request, process: Request => Future[Response], times: Int): Future[Response] ={
val responseF = process(request)
if(times > 0)
responseF.recoverWith{
case ex => println("fail")
retry(request, process, times - 1)
}
else
responseF
}
def process(s: Request): Future[Response] = {
println("run process")
if(Random.nextBoolean()) Future.successful("good") else Future.failed(new Exception)
}
val result = retry("", process, 3)
import scala.concurrent.duration._
println(Await.result(result, 1.second))
}
Source: stackoverflow.com
Related Query
- How to implement simple retry using AsyncHttpClient and scala
- How to implement security Authorization using scala and play?
- How to implement simple protocol using Actors and Akka?
- How can I avoid mutable variables in Scala when using ZipInputStreams and ZipOutpuStreams?
- How to set an expected exception using Scala and JUnit 4
- How can I fix missing conf files when using shadowJar and Scala dependencies?
- How to elegantly implement the pipeline pattern using Scala
- How to show images using Play framework and Scala in the view page
- How to implement a simple TCP protocol using Akka Streams?
- how to print variable name and value using a scala macro?
- How can I select a non-sequential subset elements from an array using Scala and Spark?
- Using vs code, how to get scala format to work and format my code?
- How to develop and run spark scala from vscode using sbt or Metals
- How to transform and apply a partial function using Scala Macros?
- How to print http request when using Dispatch and Scala
- How to implement a recursive Fibonacci sequence in Scala using FS2?
- How Scala achieve performance improvement for Map and Set by using different Class based on size?
- How to go from a List[F[String]] to F[List[String]] using Scala and Cats?
- How "this" keyword is using and working in Scala Auxiliary Constructor?
- How to run an existing Scala project using VS Code and Metals?
- How to implement scala generic function that takes subtype of parameterized traits and returns it
- How do I parse xml into "messages" and print them out in scala using stream parsing?
- How to implement this simple algorithm elegantly in Scala
- How to reaload scala application after code change using actors and sbt
- How to build spark application using Scala IDE and Maven?
- How to get application.conf variable in an object using Scala and Play 2.5.x?
- How to fetch records from the database using Play with Scala and Slick
- how to create a connection between scala and mysql using jdbc
- How to implement [x] -> x in scala using shapeless?
- Simple JSON to XML and then XML to JSON using Scala
More Query from same tag
- scala akka: random deadLetters while no actor is asked to stop
- Printing specific output in Scala
- Scala, list the existing files in URL
- java: how to draw lines above painting of canvas
- Underscore treats val like a def
- Library "libmaliinstr.so" not found
- Spark SQL count() returns wrong number
- Scala/Lift- How do I do a page redirect with params?
- Error while trying to save the data to Hive tables from dataframe
- getting list by the first element in the list of lists
- Scala - reduce/foldLeft
- update column of set type in Cassandra row
- How do I build a statically linked executable with scala-native?
- Find top N game for every ID based on total time using spark and scala
- Work with nested maps
- Scala numeric range but not collection
- Consumer Poll Rate with Akka, SQS, and Camel
- How to add MongoDB to Lift WebApp in Eclipse?
- Apache Spark - Apache Ignite SQL - Table "INTEGER" not found
- Share sparkRDD using apache Ignite
- Apache Spark: get elements of Row by name
- Slick 3 dynamic groupby - fields from its List[String]
- How to get the creation date of a file that is present on HDFS?
- How to use getOrElse in status.in() of Gatling?
- Cannot cast java ArrayList to scala immutable List
- Moving an element to the front of a list in Scala
- Spark Scala -difference between current date and max(day)
- How to solve the inheritance of conflicting members in Scala
- H2 Database Auto Server mode : Accessing through web console remotely
- How to write a generic function to evaluate column values inside withcolumn of spark dataframe