Advertisement
Guest User

Untitled

a guest
Apr 20th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 2.35 KB | None | 0 0
  1. import scala.concurrent.{Future, Promise}
  2. import scala.util.{Failure, Random, Success}
  3.  
  4. import scala.concurrent.ExecutionContext.Implicits.global
  5.  
  6. object FutureTest {
  7.  
  8.   /** This immitates the BaseRecognizeCallback that you have. */
  9.   trait BaseRecognizeCallback {
  10.     def onTranscription(result: String): Unit
  11.     def onFailure(exception: Throwable): Unit
  12.   }
  13.  
  14.   /**
  15.    * A function that computes the result asynchronously and informs the calling code through
  16.    * a callback. In your case, this would be this 'recognizeUsingWebSocket'.
  17.    * @param callback the function that will be called, when the result is available.
  18.    */
  19.   def recognizeUsingWebSocket(callback: BaseRecognizeCallback): Unit = {
  20.     val randomSeconds = new Random().nextInt(5)
  21.  
  22.     println(s"Waiting randomly for $randomSeconds seconds")
  23.     Thread.sleep(randomSeconds * 1000L)
  24.  
  25.     if (randomSeconds % 2 == 0) {
  26.       callback.onFailure(new RuntimeException(s"Randomly Failed after $randomSeconds seconds"))
  27.     } else {
  28.       callback.onTranscription("Complete Success!")
  29.     }
  30.   }
  31.  
  32.   def getResultsLater(): Future[String] = {
  33.     val maybeSolution = Promise[String]()
  34.  
  35.     val cb = new BaseRecognizeCallback {
  36.       override def onTranscription(result: String): Unit =
  37.         maybeSolution.complete(Success(result))
  38.  
  39.       override def onFailure(exception: Throwable): Unit =
  40.         maybeSolution.complete(Failure(new RuntimeException("Error while doing complicated work!", exception)))
  41.     }
  42.  
  43.     // now tell our asynchronous computation to compute the result
  44.     recognizeUsingWebSocket(cb)
  45.  
  46.     // return the future result
  47.     maybeSolution.future
  48.   }
  49.  
  50.   /**
  51.    * Just to make the example complete, here's the case where we don't have to wait.
  52.    * @return future results.
  53.    */
  54.   def getImmediateResults(): Future[String] = {
  55.     Future("This was quick!")
  56.   }
  57.  
  58.  
  59.   def main(args: Array[String]): Unit = {
  60.     getResultsLater().onComplete {
  61.       case Success(result) => println(s"This will only be executed once the future has succeeded: $result")
  62.       case Failure(problem) => println(s"Desaster! ${problem.getMessage}")
  63.     }
  64.  
  65.     getImmediateResults().onComplete {
  66.       case Success(result) => println(s"$result")
  67.       case Failure(problem) => println(s"This should never happen.. ;-) If it does, here's why: ${problem.getMessage}")
  68.     }
  69.  
  70.   }
  71.  
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement