【问题标题】:typed actors vs futures - how are they different?打字演员与期货 - 他们有什么不同?
【发布时间】:2017-09-28 00:57:14
【问题描述】:

我正在阅读有关typed actors 的信息,类型化的演员提供的界面如下所示:

trait Squarer {
  def squareDontCare(i: Int): Unit //fire-forget

  def square(i: Int): Future[Int] //non-blocking send-request-reply

  def squareNowPlease(i: Int): Option[Int] //blocking send-request-reply

  def squareNow(i: Int): Int //blocking send-request-reply

  @throws(classOf[Exception]) //declare it or you will get an UndeclaredThrowableException
  def squareTry(i: Int): Int //blocking send-request-reply with possible exception
}

这与简单的 Future 有何不同?

它们看起来确实很相似,def square(i: Int): Future[Int] 的界面相同。

类型化的actors是位置透明的(并且可以在其他节点上运行)但Futures不是吗?

所以类型化的actors可以被认为是一种更受限制的Futures形式?从某种意义上说,Future 构造受到限制,因此对于将来的构造,不能使用任何不能通过电线(不可序列化)的东西。例如,闭包(或任何函数)不能传递给类型化的 Actor,但可以用于构造 Future。

【问题讨论】:

  • Typed Actors:“该模块将被弃用,因为它将被当前以开放预览模式开发的 Akka Typed 项目所取代。” “类型化的 Actor 非常适合在 Actor 系统(“内部”)和非 Actor 代码(“外部”)之间架起桥梁,因为它们允许您在外部编写正常的 OO 外观代码。”

标签: scala akka future typedactor


【解决方案1】:

我可以看到以下用例:假设您在 actor 后面的服务具有可变状态。由于方法调用是一条消息,并且消息是按顺序处理的,因此您可以获得服务内部状态的同步。

顺便说一句,默认情况下,actor 不会将消息限制为可序列化。

【讨论】:

  • 好点!谢谢。那确实是一个很大的差异。期货是“执行一次”,可以说是短暂的,而演员的寿命更长。
猜你喜欢
  • 1970-01-01
  • 2018-04-25
  • 1970-01-01
  • 2021-11-25
  • 2011-07-04
  • 2018-03-28
  • 1970-01-01
  • 2019-02-18
  • 1970-01-01
相关资源
最近更新 更多