gsimard
gsimard

Reputation: 643

Akka: defining an actor in Scala with non-default constructor and creating it from Java code

An Akka Scala actor must extend akka.actor.Actor

An Akka Java actor must extend akka.actor.UntypedActor

Therefore, when defining a Scala actor with a non-default constructor and creating it from Java code, I run into this problem:

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

Of course, the UntypedActorFactory is expecting to create an object of type UntypedActor, but my actor is of type Actor.

What's the workaround ?

EDIT:

Following instructions from Viktor to use akka.japi.Creator, this works:

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");

Upvotes: 2

Views: 1370

Answers (1)

Viktor Klang
Viktor Klang

Reputation: 26597

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

Upvotes: 7

Related Questions