如何在 Scala 中跟踪嵌套期货的异常和结果
How to track exceptions and results of nested futures in scala
我有一个想计算嵌套期货的场景。以下是场景:
def firstFuture(factor: Int): Future[Int] = Future {
println("Running future 1")
Thread.sleep(3000)
5 * factor
}
def secondFuture(factor: Int) = Future {
println("Running future 2")
throw new Exception("fjdfj")
Thread.sleep(4000); 3 * factor
}
def thirdFuture = Future {
println("Running future 3")
Thread.sleep(5000)
throw new Exception("mai fat raha hu")
}
def method = {
(Future(5).map { factor =>
firstFuture(factor).recover { case ex: Exception => throw new Exception("First future failed") }
secondFuture(factor).recover { case ex: Exception => throw new Exception("Second future failed") }
thirdFuture.recover { case ex: Exception => throw new Exception("Third future failed") }
}).flatMap(identity).recover { case ex: Exception =>
println("Inside recover")
println(ex.getMessage)
}
}
Await.result(method, 20 seconds)
我想处理主要未来完成的所有嵌套未来的异常。假设如果 secondFuture 失败,那么结果应该是 secondFuture 失败。但我只是在第三个未来得到了反映。我怎样才能做到这一点。应该怎么实现。
注意:嵌套的三个期货应该运行并行。
你只得到第三个未来的错误的原因是因为整个块的值是块的最后一个表达式,所以
Future(5).map { factor =>
firstFuture(factor) // this executes but the result is discarded
secondFuture(factor) // this executes but the result is discarded
thirdFuture // the last expression becomes the value of the whole block
}
还要考虑当我们有嵌套的 future 并且我们将其放入内部 future 时会发生什么
Future(41).map { v =>
Future(throw new RuntimeException("boom")) // the exception is simply swallowed
v + 1
}
尽管内部 Future
内部抛出异常,但结果是 Future(42)
。理解这一点很重要,否则我们可能会在系统中引入 无声故障。
达到你的要求try combination of for-comprehension and Future.sequence
for {
factor <- Future(5)
results <- Future.sequence(List(firstFuture(factor), secondFuture(factor), thirdFuture))
} yield results
传递给 sequence
的三个 future 将同时执行,如果其中任何一个失败,sequence
将 return 一个失败的 future。
我有一个想计算嵌套期货的场景。以下是场景:
def firstFuture(factor: Int): Future[Int] = Future {
println("Running future 1")
Thread.sleep(3000)
5 * factor
}
def secondFuture(factor: Int) = Future {
println("Running future 2")
throw new Exception("fjdfj")
Thread.sleep(4000); 3 * factor
}
def thirdFuture = Future {
println("Running future 3")
Thread.sleep(5000)
throw new Exception("mai fat raha hu")
}
def method = {
(Future(5).map { factor =>
firstFuture(factor).recover { case ex: Exception => throw new Exception("First future failed") }
secondFuture(factor).recover { case ex: Exception => throw new Exception("Second future failed") }
thirdFuture.recover { case ex: Exception => throw new Exception("Third future failed") }
}).flatMap(identity).recover { case ex: Exception =>
println("Inside recover")
println(ex.getMessage)
}
}
Await.result(method, 20 seconds)
我想处理主要未来完成的所有嵌套未来的异常。假设如果 secondFuture 失败,那么结果应该是 secondFuture 失败。但我只是在第三个未来得到了反映。我怎样才能做到这一点。应该怎么实现。
注意:嵌套的三个期货应该运行并行。
你只得到第三个未来的错误的原因是因为整个块的值是块的最后一个表达式,所以
Future(5).map { factor =>
firstFuture(factor) // this executes but the result is discarded
secondFuture(factor) // this executes but the result is discarded
thirdFuture // the last expression becomes the value of the whole block
}
还要考虑当我们有嵌套的 future 并且我们将其放入内部 future 时会发生什么
Future(41).map { v =>
Future(throw new RuntimeException("boom")) // the exception is simply swallowed
v + 1
}
尽管内部 Future
内部抛出异常,但结果是 Future(42)
。理解这一点很重要,否则我们可能会在系统中引入 无声故障。
达到你的要求try combination of for-comprehension and Future.sequence
for {
factor <- Future(5)
results <- Future.sequence(List(firstFuture(factor), secondFuture(factor), thirdFuture))
} yield results
传递给 sequence
的三个 future 将同时执行,如果其中任何一个失败,sequence
将 return 一个失败的 future。