3
votes

I need to create an actor whose exact type is only read at runtime from a configuration file. The constructor for that actor is non trivial, i.e. it requires some parameters which are defined in the configuration file.

Until now, I've tried to do this using reflection as follows:

val actor = actorOf(constructor.newInstance(parameters: _*).asInstanceOf[T]).start

Unfortunately, this results in an "akka.actor.ActorInitializationException: ActorRef for instance of actor [...] is not in scope. You can not create an instance of an actor explicitly using 'new MyActor'. [...]"!

Now, I understand this protects the framework from leaking a reference to the inner object, but how can I get past this problem? Alternatively, how else could I do what I'm trying to do in a way that will work?

1

1 Answers

4
votes

Maybe use the Java interface for creating actors with parameters. The (Java) code for creation looks like this:

ActorRef actor = actorOf(new UntypedActorFactory() {
  public UntypedActor create() {
    return new MyUntypedActor("service:name", 5);
   }
});

So you pass a factory instead of a function. This might work better...