在Play Framework中设置父子Actor

时间:2016-11-14 06:11:40

标签: java playframework-2.0 akka actor

我刚刚开始使用Play Framework + Akka。我有一个家长演员和一个孩子演员(将来可以超过)。我正在从控制器进行Api调用并期待来自儿童演员的回应。

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

Commctor(家长)

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

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

父Actor正确响应,但是当我用Notification tell替换它时。我得到例外;我无法弄明白事情出错的地方。

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)

1 个答案:

答案 0 :(得分:0)

我能够解决问题。万一有人遇到同样的问题。它基本上是从CommActor到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);
        }

CA / NA是Notification / CommActor的协议

NotificationActor:

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