as:使用 as-typed 来实现活动对象模式
akka: using akka-typed to implement the active objects pattern
阿卡Typed Actors documentation states that it will be superseded by Akka Typed. I am inferring from this that Akka Typed can be used to implement the Active Object pattern;但我不太清楚如何。到目前为止,这是我的尝试;我知道它很臭 :D
object HelloWorld {
final case class Greet(whom: String, replyTo: ActorRef[Greeted])
final case class Greeted(whom: String)
private val greeter = Static[Greet] { msg ⇒
println(s"Hello ${msg.whom}!")
msg.replyTo ! Greeted(msg.whom)
}
private val system = ActorSystem("HelloWorld", Props(greeter))
def greet(whom: String): Future[Greeted] = system ? (Greet(whom, _))
}
干杯
由于移除 TypedActor 的所有原因,您 link 访问的页面定义的活动对象模式并不理想:异步执行方法的事实非常重要,因此不应该被实现普通接口的代理对象等技术隐藏。相反,Akka Typed 允许您编写几乎相同的代码,就好像它是一个活动对象一样,同时保留异步标记:而不是使用 .
语法选择方法,而是使用 ?
发送消息(或!
如果协议不是简单的请求-响应)。您的示例如下所示:
object HelloWorld {
final case class Greet(whom: String)(replyTo: ActorRef[Greeted])
final case class Greeted(whom: String)
val greeter = Static[Greet] { msg ⇒
println(s"Hello ${msg.whom}!")
msg.replyTo ! Greeted(msg.whom)
}
}
object Sample extends App {
import HelloWorld._
val system = ActorSystem("HelloWorld", Props(greeter))
val fg = system ? Greet("John")
}
请注意,按照经典模式,为每个对象创建一个单独的线程(或 ActorSystem)听起来不错,但这样做会放弃消息驱动架构的许多好处,即许多 Actor 可以共享相同的线程更有效执行的资源,它们可以形成用于原则性故障处理等的监督层次结构。
阿卡Typed Actors documentation states that it will be superseded by Akka Typed. I am inferring from this that Akka Typed can be used to implement the Active Object pattern;但我不太清楚如何。到目前为止,这是我的尝试;我知道它很臭 :D
object HelloWorld {
final case class Greet(whom: String, replyTo: ActorRef[Greeted])
final case class Greeted(whom: String)
private val greeter = Static[Greet] { msg ⇒
println(s"Hello ${msg.whom}!")
msg.replyTo ! Greeted(msg.whom)
}
private val system = ActorSystem("HelloWorld", Props(greeter))
def greet(whom: String): Future[Greeted] = system ? (Greet(whom, _))
}
干杯
由于移除 TypedActor 的所有原因,您 link 访问的页面定义的活动对象模式并不理想:异步执行方法的事实非常重要,因此不应该被实现普通接口的代理对象等技术隐藏。相反,Akka Typed 允许您编写几乎相同的代码,就好像它是一个活动对象一样,同时保留异步标记:而不是使用 .
语法选择方法,而是使用 ?
发送消息(或!
如果协议不是简单的请求-响应)。您的示例如下所示:
object HelloWorld {
final case class Greet(whom: String)(replyTo: ActorRef[Greeted])
final case class Greeted(whom: String)
val greeter = Static[Greet] { msg ⇒
println(s"Hello ${msg.whom}!")
msg.replyTo ! Greeted(msg.whom)
}
}
object Sample extends App {
import HelloWorld._
val system = ActorSystem("HelloWorld", Props(greeter))
val fg = system ? Greet("John")
}
请注意,按照经典模式,为每个对象创建一个单独的线程(或 ActorSystem)听起来不错,但这样做会放弃消息驱动架构的许多好处,即许多 Actor 可以共享相同的线程更有效执行的资源,它们可以形成用于原则性故障处理等的监督层次结构。