Skip to content

Instantly share code, notes, and snippets.

@Sergey80
Forked from nicholasren/blocking-with-future.scala
Last active August 29, 2015 14:27
Show Gist options
  • Save Sergey80/7bf7b11c7b21ffabd16e to your computer and use it in GitHub Desktop.
Save Sergey80/7bf7b11c7b21ffabd16e to your computer and use it in GitHub Desktop.
solving nested future problem
val fa = callToRemoteServiceA();
val fb = callToRemoteServiceB();
val fc = callToRemoteServiceC(fa.get());
val fd = callToRemoteServiceD(fb.get());
val fe = callToRemoteServiceE(fb.get());
val executor = new ThreadPoolExecutor(4, 4, 1, TimeUnit.MINUTES, new LinkedBlockingQueue[Runnable]())
def callToRemoteServiceA() = {
executor.submit(new Callable[String] {
override def call(): String = "response A"
})
}
def callToRemoteServiceB() = {
executor.submit(new Callable[String] {
override def call(): String = "response B"
})
}
def callToRemoteServiceC(res: String) = {
executor.submit(new Callable[String] {
override def call(): String = "responseC depends on " + res
})
}
def callToRemoteServiceD(res: String) = {
executor.submit(new Callable[String] {
override def call(): String = "responseD depends on " + res
})
}
def callToRemoteServiceE(res: String) = {
executor.submit(new Callable[String] {
override def call(): String = "responseE depends on " + res
})
}
val fa = callToRemoteServiceA();
val fb = callToRemoteServiceB();
val fc = executor.submit(new Callable[String]() {
override def call(): String = callToRemoteServiceC(fa.get).get
})
val fd = executor.submit(new Callable[String]() {
override def call(): String = callToRemoteServiceD(fb.get).get
})
val fe = executor.submit(new Callable[String]() {
override def call(): String = callToRemoteServiceE(fb.get).get
})
val executor = new ThreadPoolExecutor(4, 4, 1, TimeUnit.MINUTES, new LinkedBlockingQueue[Runnable]())
def callToRemoteServiceA() = {
executor.submit(new Callable[String] {
override def call(): String = "response A"
})
}
def callToRemoteServiceB() = {
executor.submit(new Callable[String] {
override def call(): String = "response B"
})
}
def callToRemoteServiceC(res: String) = {
executor.submit(new Callable[String] {
override def call(): String = "responseC depends on " + res
})
}
def callToRemoteServiceD(res: String) = {
executor.submit(new Callable[String] {
override def call(): String = "responseD depends on " + res
})
}
def callToRemoteServiceE(res: String) = {
executor.submit(new Callable[String] {
override def call(): String = "responseE depends on " + res
})
}
import java.util.Date
import rx.lang.scala.Observable._
import scala.concurrent.duration._
object Filtering extends App {
val actionList = List("click", "drag", "drop", "click", "click")
val actions = interval(1 seconds).map(_.toInt).take(5).map(actionList(_))
actions.filter( _ == "click").subscribe(x => println( "clicked at " + new Date()))
}
import rx.lang.scala.Observable._
import scala.concurrent.Future
import scala.concurrent.future
object Main extends App {
import scala.concurrent.ExecutionContext.Implicits.global
val oa = from(callToRemoteServiceA)
val ob = from(callToRemoteServiceB())
val oc = oa.flatMap { res => from(callToRemoteServiceC(res)) }
val od = oa.flatMap { res => from(callToRemoteServiceD(res))}
val oe = oa.flatMap { res => from(callToRemoteServiceE(res))}
oc.subscribe(println(_))
od.subscribe(println(_))
oe.subscribe(println(_))
private
def callToRemoteServiceA(): Future[String] = future {
"responseA"
}
def callToRemoteServiceB(): Future[String] = future {
"responseB"
}
def callToRemoteServiceC(res: String): Future[String] = future {
"responseC depends on " + res
}
def callToRemoteServiceD(res: String): Future[String] = future {
"responseD depends on " + res
}
def callToRemoteServiceE(res: String): Future[String] = future {
"responseE depends on " + res
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment