Skip to content

Instantly share code, notes, and snippets.

@jconwell
Created December 27, 2013 23:51
Show Gist options
  • Save jconwell/8154233 to your computer and use it in GitHub Desktop.
Save jconwell/8154233 to your computer and use it in GitHub Desktop.
Another utility actor used in unit testing. Use this when you need to refer to the actor being tested by its path, but don't want to create all the actor parent actors.
package com.turbo.akka;
import akka.actor.ActorRef;
import akka.actor.Props;
import akka.actor.UntypedActor;
import akka.event.Logging;
import akka.event.LoggingAdapter;
/**
* Utility actor that does nothing but take a Props and create
* whatever actor the Props points to
*/
public class ChildCreationActor extends UntypedActor {
private final LoggingAdapter LOG = Logging.getLogger(getContext().system(), this);
@Override
public void onReceive(Object o) throws Exception {
if (o instanceof ChildArgs) {
ChildArgs childArgs = (ChildArgs)o;
ActorRef child = getContext().actorOf(childArgs.props, childArgs.actorName);
LOG.info("Created child actor: " + child.path().toString());
}
else {
unhandled(o);
}
}
/**
* The only usable msg to ChildCreationActor. Defines the child actor to create
*/
public static class ChildArgs {
public final String actorName;
public final Props props;
public ChildArgs(Props props, String actorName) {
this.actorName = actorName;
this.props = props;
}
}
}
@coolFreight
Copy link

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment