【发布时间】:2022-01-23 00:00:15
【问题描述】:
例子
import akka.actor.typed.scaladsl.AskPattern._
object MyActor {
sealed trait Command
case class MyCommand(replyTo: ActorRef[Try[String]]) extends Command
def apply(): Behaviors.Receive[Command] = {
val failureMessage = util.Failure(new RuntimeException("Timeout"))
Behaviors.receiveMessage {
case MyCommand(replyTo) => replyTo ! failureMessage
Behaviors.same
}
}
}
val worker = testKit.spawn(MyActor())
// Act
val res = worker.ask[Try[String]](me => MyActor.MyCommand(replyTo = me))
// Assert
println(Await.result(res, Duration.Inf)) // Return Failure(java.lang.RuntimeException: Timeout)
// But i want to do:
intercept[RuntimeException] {
Await.result(res, Duration.Inf)
}
// and on success i want to return instead of Success[T] as T
}
请注意,我使用Try 包装结果,以便返回Success 或Failure。问题是我不想这样做,我想在ask 请求中使整个Future 失败。
有可能吗?
【问题讨论】:
-
删除了我的答案,因为我不确定它是否仍然适用于 akka typed,但您应该能够回复
Status.Failure以导致ask未来会失败。 -
@ZviMints 通常最好用
akka-typed标记使用类型化 API 的问题。 -
@Dylan
Status.Failure不能直接与 Akka Typed 一起使用
标签: scala akka akka-typed