Kamlesh Arya Kamlesh Arya - 21 days ago 6
Java Question

Setting up Parent-Child Actor in Play Framework

I just started working on Play Framework + Akka. I have one Parent Actor and one child Actor(in future it can exceed). I am making an Api call from a controller and expecting a response from child actor.

NotificationController:

@Singleton
public class NotificationController extends Controller{
final ActorRef commActor;

@Inject
public NotificationController(ActorSystem system) {
commActor = system.actorOf(CommActor.props, "comm-actor");
}

public CompletionStage<Result> communicate(int isDirect, int mode, int messageId){
return FutureConverters.toJava(ask(commActor,
new CommActorProtocol.CA(isDirect, mode, messageId), 1000))
.thenApply(response -> ok((String) response).as("application/json"));
}
}


CommActor(Parent Actor)

public class CommActor extends UntypedActor {
public static Props props = Props.create(CommActor.class);
private ActorRef notificationActor;

public CommActor(){
notificationActor = this.getContext().actorOf(NotificationActor.props, "notification-actor");
}

@Override
public void onReceive(Object message) throws Exception {
notificationActor.tell(message, self());
}


NotificationActor (Child Actor):

public class NotificationActor extends UntypedActor{
public static Props props = Props.create(NotificationActor.class);

@Override
public void onReceive(Object message) throws Exception {
sender().tell("Hi from notification", self());
}
}


Parent Actor is responding correctly, but when I replace it with Notification tell. I get exception; I am not able to figure it out where the thing is going wrong.

play.api.http.HttpErrorHandlerExceptions$$anon$1: Execution exception[[Completio
nException: akka.pattern.AskTimeoutException: Ask timed out on [Actor[akka://app
lication/user/comm-actor#1301952259]] after [1000 ms]. Sender[null] sent message
of type "protocols.CommActorProtocol$CA".]]
at play.api.http.HttpErrorHandlerExceptions$.throwableToUsefulException(
HttpErrorHandler.scala:280)
at play.api.http.DefaultHttpErrorHandler.onServerError(HttpErrorHandler.
scala:206)
at play.api.GlobalSettings$class.onError(GlobalSettings.scala:160)
at play.api.DefaultGlobal$.onError(GlobalSettings.scala:188)
at play.api.http.GlobalSettingsHttpErrorHandler.onServerError(HttpErrorH
andler.scala:98)
at play.core.server.netty.PlayRequestHandler$$anonfun$2$$anonfun$apply$1
.applyOrElse(PlayRequestHandler.scala:100)
at play.core.server.netty.PlayRequestHandler$$anonfun$2$$anonfun$apply$1
.applyOrElse(PlayRequestHandler.scala:99)
at scala.concurrent.Future$$anonfun$recoverWith$1.apply(Future.scala:346
)
at scala.concurrent.Future$$anonfun$recoverWith$1.apply(Future.scala:345
)
at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:32)
Caused by: java.util.concurrent.CompletionException: akka.pattern.AskTimeoutExce
ption: Ask timed out on [Actor[akka://application/user/comm-actor#1301952259]] a
fter [1000 ms]. Sender[null] sent message of type "protocols.CommActorProtocol$C
A".
at java.util.concurrent.CompletableFuture.encodeThrowable(Unknown Source
)
at java.util.concurrent.CompletableFuture.completeThrowable(Unknown Sour
ce)
at java.util.concurrent.CompletableFuture.uniApply(Unknown Source)
at java.util.concurrent.CompletableFuture$UniApply.tryFire(Unknown Sourc
e)
at java.util.concurrent.CompletableFuture.postComplete(Unknown Source)
at java.util.concurrent.CompletableFuture.completeExceptionally(Unknown
Source)
at scala.concurrent.java8.FuturesConvertersImpl$CF.apply(FutureConverter
sImpl.scala:21)
at scala.concurrent.java8.FuturesConvertersImpl$CF.apply(FutureConverter
sImpl.scala:18)
at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:32)
at scala.concurrent.BatchingExecutor$Batch$$anonfun$run$1.processBatch$1
(BatchingExecutor.scala:63)
Caused by: akka.pattern.AskTimeoutException: Ask timed out on [Actor[akka://appl
ication/user/comm-actor#1301952259]] after [1000 ms]. Sender[null] sent message
of type "protocols.CommActorProtocol$CA".
at akka.pattern.PromiseActorRef$$anonfun$1.apply$mcV$sp(AskSupport.scala
:604)
at akka.actor.Scheduler$$anon$4.run(Scheduler.scala:126)
at scala.concurrent.Future$InternalCallbackExecutor$.unbatchedExecute(Fu
ture.scala:601)
at scala.concurrent.BatchingExecutor$class.execute(BatchingExecutor.scal
a:109)
at scala.concurrent.Future$InternalCallbackExecutor$.execute(Future.scal
a:599)
at akka.actor.LightArrayRevolverScheduler$TaskHolder.executeTask(LightAr
rayRevolverScheduler.scala:331)
at akka.actor.LightArrayRevolverScheduler$$anon$4.executeBucket$1(LightA
rrayRevolverScheduler.scala:282)
at akka.actor.LightArrayRevolverScheduler$$anon$4.nextTick(LightArrayRev
olverScheduler.scala:286)
at akka.actor.LightArrayRevolverScheduler$$anon$4.run(LightArrayRevolver
Scheduler.scala:238)
at java.lang.Thread.run(Unknown Source)

Answer

I am able to solve the problem. In case any one stuck with same problem. It was basically going to an infinite loop from CommActor to NotificationActor.

CommActor:

if (message instanceof CA) {
            int mode = ((CA) message).mode;
            this.originator = getContext().sender();
            switch(mode){
            case 1: //SMS
                 break;
             case 2: //Email
                 break;
             case 3: //Notification
                 notificationActor.tell(new NA((CA)message), getSelf());
                 break;
            }
        }
        else if (message instanceof Response) {
            originator.tell(((Response) message).getResponse(), getSelf());
        }
        else{
            unhandled(message);
        }

Where CA/NA are protocols for Notification/CommActor

NotificationActor:

@Override
    public void onReceive(Object message) throws Exception {
        if (message instanceof NA){
getSender().tell(new Response(response), getSelf());}}