How to Create Controllable Futures in Scala
In this article, we discuss how to create controllable Futures in Scala with the help of Promises.
Join the DZone community and get the full member experience.
Join For Freexxxxxxxxxx
val myFuture = Future {
// you have no future, you are DOOMED!
42
// JK.
}
You know the value inside will be evaluated on "some" thread, at "some" point in time, without your control.
The Scenario
xxxxxxxxxx
def gimmeMyPreciousValue(yourArg: Int): Future[String]
with the assumption that you're issuing a request to some multi-threaded service which, is getting called all the time. Let's also assume that the service looks like this:
xxxxxxxxxx
object MyService {
def produceThePreciousValue(theArg: Int): String = "The meaning of your life is " + (theArg / 42)
def submitTask[A](actualArg: A)(function: A => Unit): Boolean = {
// send the function to be evaluated on some thread, at the discretion of the scheduling logic
true
}
}
The service has two API methods:
-
A "production" function that is completely deterministic.
-
A submission function that has a pretty terrible API because the function argument will be evaluated on one of the service's threads, and you can't get the returned value back from another thread's call stack.
xxxxxxxxxx
def gimmeMyPreciousValue(yourArg: Int): Future[String] = Future {
MyService.produceThePreciousValue(yourArg)
}
because spawning up the thread responsible for evaluating the production function is not up to you.
The Solution
xxxxxxxxxx
// create an empty promise
val myPromise = Promise[String]()
// extract its future
val myFuture = myPromise.future
// do your thing with the future, assuming it will be filled with a value at some point
val furtherProcessing = myFuture.map(_.toUpperCase())
Then pass that promise to someone else, perhaps an asynchronous service:
xxxxxxxxxx
val asyncCall(promise: Promise[String]): Unit = {
promise.success("Your value here, your majesty")
}
And at the moment, the promise contains a value. Its future will automatically be fulfilled with that value, which will unlock the consumer.
How to Use it
def gimmeMyPreciousValue(yourArg: Int): Future[String] = {
// create promise now
val thePromise = Promise[String]()
// submit a task to be evaluated later, at the discretion of the service
// note: if the service is not on the same JVM, you can pass a tuple with the arg and the promise so the service has access to both
MyService.submit(yourArg) { x: Int =>
val preciousValue = MyService.producePreciousValue(x)
thePromise.success(preciousValue)
}
// return the future now, so it can be reused by whoever's consuming it
thePromise.future
}
We create a promise and then we return its future at the end for whoever wants to consume it. In the middle, we submit a function that will be evaluated at some point out of our control. At that moment, the service produces the value and fulfills the Promise, which will automatically fulfill the Future for the consumer.
failure
,
trySuccess
/
tryFailure
and more.
Published at DZone with permission of Daniel Ciocirlan. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments