本文介绍了在Play框架中设置亲子演员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始使用Play Framework + Akka.我有一名家长演员和一名儿童演员(将来可能会超过).我正在从控制器发出Api呼叫,并期待儿童演员的回应.

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:

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(父演员)

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(儿童演员):

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正确响应,但是当我将其替换为Notification tell时.我例外.我无法弄清楚哪里出了问题.

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)

推荐答案

我能够解决问题.万一有人遇到同样的问题.从CommActor到NotificationActor,这基本上是一个无限循环.

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:

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

CA/NA是Notification/CommActor的协议

Where CA/NA are protocols for Notification/CommActor

NotificationActor:

NotificationActor:

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

这篇关于在Play框架中设置亲子演员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 13:56