0

我是演员的新手,并尝试使用类型层次结构和有状态实例来执行简单的乒乓球示例。我完全按照 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

4

1 回答 1

2

检查了它,我记得这个文档站点在 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

于 2020-07-25T11:56:08.203 回答