Advertisement
mitrakov

Scala: "try-Await" to "Try-onComplete"

Jan 17th, 2018
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 5.04 KB | None | 0 0
  1. // 1. Suppose we've got a Future[T] and usual blocking code:
  2. import java.time.LocalDateTime
  3.  
  4. import scala.concurrent.ExecutionContext.Implicits.global
  5. import scala.concurrent.{Await, Future}
  6. import scala.concurrent.duration._
  7. import scala.io.StdIn
  8. import scala.language.postfixOps
  9. import scala.util.control.NonFatal
  10.  
  11. object Main extends App {
  12.  
  13.   def f(x: Int, y: Int): Future[Int] = Future {
  14.     println(LocalDateTime.now() + ": Start calculation!")
  15.     Thread.sleep(3000)
  16.     println(LocalDateTime.now() + ": Done calculation!")
  17.     x / y
  18.   }
  19.  
  20.   try {
  21.     println(LocalDateTime.now() + ": Hello!")
  22.     val resultFut = f(5, 2)
  23.     val result = Await.result(resultFut, 5 seconds)
  24.     println(LocalDateTime.now() + s": Result is $result")
  25.   } catch {
  26.     case e: ArithmeticException => System.err.println(s"Arithmetic err! $e")
  27.     case NonFatal(e) => println(s"Error: $e")
  28.   }
  29.  
  30.   println(LocalDateTime.now() + ": Press ENTER to finish!")
  31.   StdIn.readLine()
  32.   println(LocalDateTime.now() + ": Bye!")
  33. }
  34.  
  35.         // Output:
  36.         // 2018-01-17T22:21:46.561: Hello!
  37.         // 2018-01-17T22:21:46.690: Start calculation!
  38.         // 2018-01-17T22:21:49.690: Done calculation!
  39.         // 2018-01-17T22:21:49.692: Result is 2
  40.         // 2018-01-17T22:21:49.695: Press ENTER to finish!
  41.  
  42.         // 2018-01-17T22:21:52.840: Bye!
  43.  
  44. // 2. Now try to fail and ensure it works properly:
  45. val resultFut = f(5, 0)
  46.  
  47.         // Output:
  48.         // 2018-01-17T22:23:18.419: Hello!
  49.         // 2018-01-17T22:23:18.543: Start calculation!
  50.         // 2018-01-17T22:23:21.543: Done calculation!
  51.         // Arithmetic err! java.lang.ArithmeticException: / by zero
  52.         // 2018-01-17T22:23:21.549: Press ENTER to finish!
  53.  
  54.         // 2018-01-17T22:23:24.544: Bye!
  55.  
  56. // 3. Let's make it async!
  57. println(LocalDateTime.now() + ": Hello!")
  58. f(5, 2) onComplete {
  59.   case Success(result) => println(LocalDateTime.now() + s": Result is $result")
  60.   case Failure(e: ArithmeticException) => System.err.println(s"Arithmetic err! $e")
  61.   case Failure(NonFatal(e)) => println(s"Error: $e")
  62. }
  63.  
  64.         // Output (note the order of lines!):
  65.         // 2018-01-17T22:26:25.523: Hello!
  66.         // 2018-01-17T22:26:25.645: Press ENTER to finish!
  67.         // 2018-01-17T22:26:25.645: Start calculation!
  68.         // 2018-01-17T22:26:28.645: Done calculation!
  69.         // 2018-01-17T22:26:28.645: Result is 2
  70.  
  71.         // 2018-01-17T22:26:30.726: Bye!
  72.  
  73. // 4. Let's check failures:
  74. f(5, 0)
  75.  
  76.         // Output (note the order of lines!):
  77.         // 2018-01-17T22:26:54.466: Hello!
  78.         // 2018-01-17T22:26:54.603: Press ENTER to finish!
  79.         // 2018-01-17T22:26:54.604: Start calculation!
  80.         // 2018-01-17T22:26:57.604: Done calculation!
  81.         // Arithmetic err! java.lang.ArithmeticException: / by zero
  82.  
  83.         // 2018-01-17T22:26:59.366: Bye!
  84.  
  85. // 5. Now what if we need to return a result? way #1: with 'transform' function:
  86. object Main extends App {
  87.  
  88.   def f(x: Int, y: Int): Future[Int] = Future {
  89.     println(LocalDateTime.now() + ": Start calculation!")
  90.     Thread.sleep(3000)
  91.     println(LocalDateTime.now() + ": Done calculation!")
  92.     x / y
  93.   }
  94.  
  95.   println(LocalDateTime.now() + ": Hello!")
  96.   val strResult = f(5, 0) transform {
  97.     case Success(result) => Success(LocalDateTime.now() + s": Result is $result")
  98.     case Failure(e: ArithmeticException) => Success(s"Arithmetic err! $e")
  99.     case Failure(NonFatal(e)) => Success(s"Error: $e")
  100.   }
  101.  
  102.   strResult foreach println // prints a result as soon as Future finishes
  103.  
  104.   println(LocalDateTime.now() + ": Press ENTER to finish!")
  105.   StdIn.readLine()
  106.   println(LocalDateTime.now() + ": Bye!")
  107. }
  108.  
  109.         // Output:
  110.         // 2018-01-17T22:33:45.196: Hello!
  111.         // 2018-01-17T22:33:45.333: Start calculation!
  112.         // 2018-01-17T22:33:45.334: Press ENTER to finish!
  113.         // 2018-01-17T22:33:48.333: Done calculation!
  114.         // Arithmetic err! java.lang.ArithmeticException: / by zero
  115.  
  116.         // 2018-01-17T22:33:50.794: Bye!
  117.  
  118. // 6. And way #2: with 'map-recover' functions:
  119. object Main extends App {
  120.  
  121.   def f(x: Int, y: Int): Future[Int] = Future {
  122.     println(LocalDateTime.now() + ": Start calculation!")
  123.     Thread.sleep(3000)
  124.     println(LocalDateTime.now() + ": Done calculation!")
  125.     x / y
  126.   }
  127.  
  128.   println(LocalDateTime.now() + ": Hello!")
  129.   val strResult = f(5, 0) map { result =>
  130.     LocalDateTime.now() + s": Result is $result"
  131.   } recover {
  132.     case e: ArithmeticException => s"Arithmetic err! $e"
  133.     case NonFatal(e) => s"Error: $e"
  134.   }
  135.  
  136.   strResult foreach println // prints a result as soon as Future finishes
  137.  
  138.   println(LocalDateTime.now() + ": Press ENTER to finish!")
  139.   StdIn.readLine()
  140.   println(LocalDateTime.now() + ": Bye!")
  141. }
  142.  
  143.         // Output:
  144.         // 2018-01-17T22:34:42.673: Hello!
  145.         // 2018-01-17T22:34:42.805: Start calculation!
  146.         // 2018-01-17T22:34:42.807: Press ENTER to finish!
  147.         // 2018-01-17T22:34:45.806: Done calculation!
  148.         // Arithmetic err! java.lang.ArithmeticException: / by zero
  149.  
  150.         // 2018-01-17T22:34:47.059: Bye!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement