2

Akka Scala 演员必须扩展 akka.actor.Actor

Akka Java Actor 必须扩展 akka.actor.UntypedActor

因此,在使用非默认构造函数定义 Scala Actor 并从 Java 代码创建它时,我遇到了这个问题:

ActorRef myActor = system.actorOf(new Props(new UntypedActorFactory() {
  public UntypedActor create() {
    return new MyActor("...");
  }
}), "myactor");

当然,UntypedActorFactory 期望创建一个 UntypedActor 类型的对象,但我的 Actor 是 Actor 类型。

有什么解决方法?

编辑:

按照 Viktor 使用 akka.japi.Creator 的说明,此方法有效:

Props props1 = new Props();
Props props2 = props1.withCreator(new akka.japi.Creator() {
        public Actor create() {
            return new MyActor("...");
        }
    });
ActorRef actorRef = Main.appClient().actorOf(props2, "myactor");
4

1 回答 1

7

Pass in an akka.japi.Creator instead of the UntypedActorFactory in this case.

Also, at least in 2.0.1 and forward it doesn't require an UntypedActor:

trait UntypedActorFactory extends Creator[Actor] with Serializable

https://github.com/akka/akka/blob/v2.0.1/akka-actor/src/main/scala/akka/actor/UntypedActor.scala#L161

于 2012-06-07T09:00:50.017 回答