Reputation: 316
I am trying to create an actor with untyped actor factory, compilation happens fine. But while running the application, I get the following error. Am I missing anything in configuration?
Java Code:
MyActor myactor = new MyActor(); //MyActor extends UnTypedActor
ActorSystem system = ActorSystem.create("mynamespace");
ActorRef actor = system.actorOf(new Props(new UntypedActorFactory() {
public UntypedActor create() {
return myactor;
}
}));
Error during runtime:
Caused by: akka.actor.ActorInitializationException: You cannot create an instance of [com.practice.MyActor] explicitly using the constructor (new). You have to use one of the factory methods to create a new actor. Either use: 'val actor = context.actorOf(Props[MyActor])'
(to create a supervised child actor from within an actor), or 'val actor = system.actorOf(Props(new MyActor(..)))' (to create a top level actor from the ActorSystem)
Upvotes: 2
Views: 4249
Reputation: 24413
That's because you are creating the instance of MyActor
outside the ActorSystem
. Create the Actor inside of your factory (that's what it is for ;-) ) and it should be fine.
ActorSystem system = ActorSystem.create("mynamespace");
ActorRef actor = system.actorOf(new Props(new UntypedActorFactory() {
public UntypedActor create() {
return new MyActor();
}
}));
In this case you don't even need a factory, because you have a default constructor. Just pass the class as parameter to the Props
:
system.actorOf(new Props(MyActor.class));
Upvotes: 3