scala - 使用 ZIO Actors 创建两个执行基本乒乓通信的 Actor
问题描述
我是演员的新手,并尝试使用类型层次结构和有状态实例来执行简单的乒乓球示例。我完全按照 zio 演员 [微型网站][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$1$adapted(ActorSystem.scala:34)
at zio.internal.FiberContext.evaluateNow(FiberContext.scala:350)
at zio.internal.FiberContext.$anonfun$fork$15(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
推荐阅读
- full-text-search - 带有特殊字符和“单词开头”的 MariaDB 全文搜索
- python - Python3 列表推导未按预期工作
- javascript - 使用不同的键创建对象
- python - 在特定字符串之后返回文本中的数字
- javascript - 正则表达式超过 10 个数字
- javascript - 如何使用 javascript 创建 HTML 文件
- java - BOX API - 让外部用户选择他们的文件,然后像 Dropbox 选择器一样以编程方式下载选定的文件
- jquery - jquery API 数据未正确更新 - 1 数字低
- java - 什么是 d:c,它在这个程序中做了什么,还有其他方法可以做到这一点吗?
- intellij-idea - IDEA - 取消静音一个断点