使用 ZIO Actor 创建两个执行基本乒乓通信的 actor
Creating two actors performing basic ping-pong communication using ZIO Actors
我是 Actors 的新手,正在尝试使用类型层次结构和有状态实例来执行简单的乒乓球示例。我完全按照 zio actors [微型站点][1] 对程序代码进行了一些更新。
我在 ./src/main/resources/application.conf 的配置文件如下所示:
testSystemOne.zio.actors.remoting {
hostname = "127.0.0.1"
port = 8055
}
testSystemTwo.zio.actors.remoting {
hostname = "127.0.0.1"
port = 8056
}
和MyApp.scala :
import zio.actors.Actor.Stateful
import zio.actors._
import zio.RIO
import zio.console._
object MyApp extends zio.App {
sealed trait PingPong[+_]
case class Ping(sender: ActorRef[PingPong]) extends PingPong[Unit]
case object Pong extends PingPong[Unit]
case class GameInit(recipient: ActorRef[PingPong]) extends PingPong[Unit]
def run(args: List[String]) =
program.exitCode
val protoHandler = new Stateful[Console, Unit, PingPong] {
override def receive[A](state: Unit, msg: PingPong[A], context: Context): RIO[Console, (Unit, A)] =
msg match {
case Ping(sender) =>
for {
_ <- putStrLn("Ping!")
path <- sender.path
_ <- sender ! Pong
} yield ((), ())
case Pong =>
for {
_ <- putStrLn("Pong!")
} yield ((), ())
case GameInit(to) =>
for {
self <- context.self[PingPong]
_ <- to ! Ping(self)
} yield ((), ())
}
}
val program = for {
actorSystemRoot <- ActorSystem("testSystemOne")
one <- actorSystemRoot.make("actorOne", Supervisor.none, (), protoHandler)
actorSystem <- ActorSystem("testSystemTwo")
_ <- actorSystem.make("actorTwo", Supervisor.none, (), protoHandler)
remoteActor <- actorSystemRoot.select[PingPong](
"zio://testSystemTwo@127.0.0.1:8056/actorTwo"
)
_ <- one ! GameInit(remoteActor)
} yield ()
}
我不确定我是否正确更新了代码,这是我 运行 代码
时得到的结果
Fiber failed.
An unchecked error was produced.
java.lang.ClassCastException: class zio.ZRef$Atomic cannot be cast to class zio.Ref (zio.ZRef$Atomic and zio.Ref are in unnamed module of loader 'app')
at zio.actors.ActorSystem$.$anonfun$apply$adapted(ActorSystem.scala:34)
at zio.internal.FiberContext.evaluateNow(FiberContext.scala:350)
at zio.internal.FiberContext.$anonfun$fork(FiberContext.scala:767)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at java.base/java.lang.Thread.run(Thread.java:834)
Fiber:Id(1595333184488,1) was supposed to continue to:
a future continuation at MyApp$.program(MyApp.scala:41)
a future continuation at zio.ZIO.exitCode(ZIO.scala:578)
Fiber:Id(1595333184488,1) execution trace:
at zio.actors.ActorSystem$.apply(ActorSystem.scala:34)
at zio.ZRef$.make(ZRef.scala:609)
Fiber:Id(1595333184488,1) was spawned by:
Fiber:Id(1595333184392,0) was supposed to continue to:
a future continuation at zio.App.main(App.scala:57)
a future continuation at zio.App.main(App.scala:56)
Fiber:Id(1595333184392,0) ZIO Execution trace: <empty trace>
Fiber:Id(1595333184392,0) was spawned by: <empty trace>
Process finished with exit code 1
任何帮助!!
[1]: https://zio.github.io/zio-actors/docs/usecases/usecases_pingpong
检查了一下,我记得这个文档站点在 API 更改后没有更新 - 您需要提供 configFile
指向 application.conf
启用远程配置。很抱歉,将在下一个版本中对其进行更新,我希望将来使其更符合人体工程学(因为它仍然是原型)。
请参考https://github.com/zio/zio-actors/blob/master/actors/src/test/scala/zio/actors/RemoteSpec.scala#L100测试用例:
val configFile = Some(new File("./src/test/resources/application.conf"))
... <- ActorSystem("...", configFile)
这是包含详细信息的 scaladoc:https://github.com/zio/zio-actors/blob/master/actors/src/main/scala/zio/actors/ActorSystem.scala#L24
我是 Actors 的新手,正在尝试使用类型层次结构和有状态实例来执行简单的乒乓球示例。我完全按照 zio actors [微型站点][1] 对程序代码进行了一些更新。 我在 ./src/main/resources/application.conf 的配置文件如下所示:
testSystemOne.zio.actors.remoting {
hostname = "127.0.0.1"
port = 8055
}
testSystemTwo.zio.actors.remoting {
hostname = "127.0.0.1"
port = 8056
}
和MyApp.scala :
import zio.actors.Actor.Stateful
import zio.actors._
import zio.RIO
import zio.console._
object MyApp extends zio.App {
sealed trait PingPong[+_]
case class Ping(sender: ActorRef[PingPong]) extends PingPong[Unit]
case object Pong extends PingPong[Unit]
case class GameInit(recipient: ActorRef[PingPong]) extends PingPong[Unit]
def run(args: List[String]) =
program.exitCode
val protoHandler = new Stateful[Console, Unit, PingPong] {
override def receive[A](state: Unit, msg: PingPong[A], context: Context): RIO[Console, (Unit, A)] =
msg match {
case Ping(sender) =>
for {
_ <- putStrLn("Ping!")
path <- sender.path
_ <- sender ! Pong
} yield ((), ())
case Pong =>
for {
_ <- putStrLn("Pong!")
} yield ((), ())
case GameInit(to) =>
for {
self <- context.self[PingPong]
_ <- to ! Ping(self)
} yield ((), ())
}
}
val program = for {
actorSystemRoot <- ActorSystem("testSystemOne")
one <- actorSystemRoot.make("actorOne", Supervisor.none, (), protoHandler)
actorSystem <- ActorSystem("testSystemTwo")
_ <- actorSystem.make("actorTwo", Supervisor.none, (), protoHandler)
remoteActor <- actorSystemRoot.select[PingPong](
"zio://testSystemTwo@127.0.0.1:8056/actorTwo"
)
_ <- one ! GameInit(remoteActor)
} yield ()
}
我不确定我是否正确更新了代码,这是我 运行 代码
时得到的结果Fiber failed.
An unchecked error was produced.
java.lang.ClassCastException: class zio.ZRef$Atomic cannot be cast to class zio.Ref (zio.ZRef$Atomic and zio.Ref are in unnamed module of loader 'app')
at zio.actors.ActorSystem$.$anonfun$apply$adapted(ActorSystem.scala:34)
at zio.internal.FiberContext.evaluateNow(FiberContext.scala:350)
at zio.internal.FiberContext.$anonfun$fork(FiberContext.scala:767)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at java.base/java.lang.Thread.run(Thread.java:834)
Fiber:Id(1595333184488,1) was supposed to continue to:
a future continuation at MyApp$.program(MyApp.scala:41)
a future continuation at zio.ZIO.exitCode(ZIO.scala:578)
Fiber:Id(1595333184488,1) execution trace:
at zio.actors.ActorSystem$.apply(ActorSystem.scala:34)
at zio.ZRef$.make(ZRef.scala:609)
Fiber:Id(1595333184488,1) was spawned by:
Fiber:Id(1595333184392,0) was supposed to continue to:
a future continuation at zio.App.main(App.scala:57)
a future continuation at zio.App.main(App.scala:56)
Fiber:Id(1595333184392,0) ZIO Execution trace: <empty trace>
Fiber:Id(1595333184392,0) was spawned by: <empty trace>
Process finished with exit code 1
任何帮助!! [1]: https://zio.github.io/zio-actors/docs/usecases/usecases_pingpong
检查了一下,我记得这个文档站点在 API 更改后没有更新 - 您需要提供 configFile
指向 application.conf
启用远程配置。很抱歉,将在下一个版本中对其进行更新,我希望将来使其更符合人体工程学(因为它仍然是原型)。
请参考https://github.com/zio/zio-actors/blob/master/actors/src/test/scala/zio/actors/RemoteSpec.scala#L100测试用例:
val configFile = Some(new File("./src/test/resources/application.conf"))
... <- ActorSystem("...", configFile)
这是包含详细信息的 scaladoc:https://github.com/zio/zio-actors/blob/master/actors/src/main/scala/zio/actors/ActorSystem.scala#L24