Play中的异常处理

时间:2014-08-12 06:10:39

标签: akka

我有一名主工,有一名监督员监督其童工。 子工作者抛出一个IOException,由父母查看,然后调用SupervisorStartegy的resume()方法。

直到现在这个摘要被抛出,异常被转移到父级,父级正在调用简历但之后我得到了超时异常

public class EmailServiceActor extends UntypedActor{
ActorRef actorRef ;

private static SupervisorStrategy strategy =  
           new OneForOneStrategy(3, Duration.create("1 minute"),  
               new Function<Throwable, Directive>() {  
                 @Override  
                 public Directive apply(Throwable t) {  
                   if (t instanceof IOException) {  
                       System.out.println("IO Exception occurred");
                     return restart();  
                   } else if (t instanceof Exception) {  
                     return stop();  
                   } else {  
                     return escalate();  
                   }  
                 }  
               });  

 @Override  
    public void onReceive(Object message) {  
    System.out.println("Received .....");
    if(message instanceof MyLocalMessage){
        System.out.println("received instr from EmailServiceWorker by EmailServiceActor");
        actorRef.tell(message, self());
    }else{
        actorRef = getSender();
    System.out.println("received instr by EmailServiceActor from Application");
    getContext().actorOf(Props.create(EmailServiceWorker.class),"EmailServiceWorker").tell(message, self());
    }
   }  
   @Override  
   public SupervisorStrategy supervisorStrategy() {  
    return strategy;  
  }  

   @Override
  public void preStart() {
 System.out.println("Pre Restart...");
   }

}

我也有一名童工

public class EmailServiceWorker extends UntypedActor{

 @Override  
   public void onReceive(Object message) throws IOException{  

         System.out.println("received instr by EmailServiceWorker");
         System.out.println("Sending mail");
         FileReader reader = new FileReader("someFile");
         MyLocalMessage myLocalMessage = new MyLocalMessage("Hello");
         getSender().tell( myLocalMessage, getSelf() );
         getContext().stop(getSelf());

 }

 @Override
   public void preStart() {
     System.out.println("Pre Restart");
   }

}

没有&#34; someFile&#34;这样的文件。我故意试图提出错误。

控制器如下         public class Application extends Controller {

    //We need the ActorSystem to host our actors as well as to send and receive messages
    static ActorSystem actorSystem = ActorSystem.create( "play" );

    static {
     // Create our local actors
    actorSystem.actorOf( Props.create( MainActor.class ), "MainActor" );
    actorSystem.actorOf( Props.create( EmailServiceActor.class ), "EmailServiceActor" );

    }

    public static Result index() {
    return ok(index.render("Your new application is ready."));
    }

    @SuppressWarnings("unchecked")
    public static Promise<Result> localHello( String name )
    {
     // Look up the actor
     ActorSelection myActor =
               actorSystem.actorSelection( "user/MainActor" );

     // Connstruct our message
     MyMessage message = new MyMessage( name );

     // As the actor for a response to the message (and a 30 second timeout);
     // ask returns an Akka Future, so we wrap it with a Play Promise
     Future future = ask(myActor, message, 30000);
     Promise promiseValue = Promise.wrap(future);
     Function<Object, Result> function = new Function<Object, Result>() {
         public Result apply(Object response) {
              if( response instanceof MyLocalMessage ) {
                   MyLocalMessage message = ( MyLocalMessage )response;
                   return ok( message.getMessage() );
              }
             return notFound( "Message is not of type MyMessage" );
         }
     };


     Promise<Result> promiseResult = promiseValue.map(function);
     return promiseResult;
    }


    public static Promise<Result> sendMail(){

        ActorSelection emailActor = actorSystem.actorSelection("user/EmailServiceActor");
        Future future = ask(emailActor,"Hello",190000);
        // Look up the actor

        Promise promiseValue = Promise.wrap(future);
        Function<Object, Result> function = new Function<Object, Result>(){
            public Result apply(Object response) {
            return ok("Email is being sent");
       }
        };

        Promise<Result> promiseResult = promiseValue.map(function);
    return promiseResult;

    }


}

但是当我运行应用程序时,我收到了错误

        Pre Restart...
        Received .....
        received instr by EmailServiceActor from Application
        Pre Restart
        received instr by EmailServiceWorker
        Sending mail
        IO Exception occurred
        Pre Restart
        [ERROR] [08/12/2014 10:53:13.844] [play-akka.actor.default-dispatcher-5] [akka://play/user/EmailServiceActor/EmailServiceWorker] someFile (No such file or directory)
        java.io.FileNotFoundException: someFile (No such file or directory)
            at java.io.FileInputStream.open(Native Method)
            at java.io.FileInputStream.<init>(FileInputStream.java:146)
            at java.io.FileInputStream.<init>(FileInputStream.java:101)
            at java.io.FileReader.<init>(FileReader.java:58)
            at actors.EmailServiceWorker.onReceive(EmailServiceWorker.java:18)
            at akka.actor.UntypedActor$$anonfun$receive$1.applyOrElse(UntypedActor.scala:167)
            at akka.actor.Actor$class.aroundReceive(Actor.scala:465)
            at akka.actor.UntypedActor.aroundReceive(UntypedActor.scala:97)
            at akka.actor.ActorCell.receiveMessage(ActorCell.scala:516)
            at akka.actor.ActorCell.invoke(ActorCell.scala:487)
            at akka.dispatch.Mailbox.processMailbox(Mailbox.scala:238)
            at akka.dispatch.Mailbox.run(Mailbox.scala:220)
            at akka.dispatch.ForkJoinExecutorConfigurator$AkkaForkJoinTask.exec(AbstractDispatcher.scala:393)
            at scala.concurrent.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260)
            at scala.concurrent.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1339)
            at scala.concurrent.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979)
            at scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107)

        [error] play - Cannot invoke the action, eventually got an error: akka.pattern.AskTimeoutException: Ask timed out on [ActorSelection[Anchor(akka://play/), Path(/user/EmailServiceActor)]] after [190000 ms]
        [error] application - 

        ! @6j77j81fk - Internal server error, for (GET) [/sendmail] ->

        play.api.Application$$anon$1: Execution exception[[AskTimeoutException: Ask timed out on [ActorSelection[Anchor(akka://play/), Path(/user/EmailServiceActor)]] after [190000 ms]]]
            at play.api.Application$class.handleError(Application.scala:296) ~[play_2.11-2.3.2.jar:2.3.2]
            at play.api.DefaultApplication.handleError(Application.scala:402) [play_2.11-2.3.2.jar:2.3.2]
            at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$3$$anonfun$applyOrElse$4.apply(PlayDefaultUpstreamHandler.scala:320) [play_2.11-2.3.2.jar:2.3.2]
            at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$3$$anonfun$applyOrElse$4.apply(PlayDefaultUpstreamHandler.scala:320) [play_2.11-2.3.2.jar:2.3.2]
            at scala.Option.map(Option.scala:145) [scala-library-2.11.1.jar:na]
        Caused by: akka.pattern.AskTimeoutException: Ask timed out on [ActorSelection[Anchor(akka://play/), Path(/user/EmailServiceActor)]] after [190000 ms]
            at akka.pattern.PromiseActorRef$$anonfun$1.apply$mcV$sp(AskSupport.scala:333) ~[akka-actor_2.11-2.3.4.jar:na]
            at akka.actor.Scheduler$$anon$7.run(Scheduler.scala:117) ~[akka-actor_2.11-2.3.4.jar:na]
            at scala.concurrent.Future$InternalCallbackExecutor$.unbatchedExecute(Future.scala:599) ~[scala-library-2.11.1.jar:na]
            at scala.concurrent.BatchingExecutor$class.execute(BatchingExecutor.scala:109) ~[scala-library-2.11.1.jar:na]
            at scala.concurrent.Future$InternalCallbackExecutor$.execute(Future.scala:597) ~[scala-library-2.11.1.jar:na]

1 个答案:

答案 0 :(得分:0)

您的EmailServiceWorker演员从未回复发件人,因此控制器中的ask永远无法完成。