2012-07-09 10 views
22

ユーザーがページにアクセスしたときに生成される、長時間実行されるバックグラウンドプロセスを実装しようとしています。 http://web.archive.org/web/20130122091205/http://www.lunatech-research.com/archives/2011/10/31/progressbar-jqueryui-websockets-playframework進行中の進行中のプロセスの例PlayFramework 2

私はPlayFramework 2.0のチュートリアル(inbuilt AKKAを使用しています)を知っている人はいますか?いいえ、この例のようにタスクの進行状況を表示したいのですか?これは1.2用です

答えて

21

すべてのAkkaドキュメンテーションJava for http://doc.akka.io/docs/akka/2.0.1/intro/getting-started-first-java.htmlを読んだところ、これはうまくいくようです。

システムは、最初に "レポート"を処理するためのユニークなアクタを作成することによって機能します(生成ページがロードされているとき)。このアクタは、子のアクタを生成し、子のアクタはその進捗状況を親に返します。親アクターはJavaScriptを介して子スレッドのステータスをポーリングされます。

子が終了すると、子が終了し、親が子の終了を検出すると、子は終了します。

以下はすべてのコードですが、これについて間違った方法をとってしまった場合は、私を裂くことが自由です。 (?!?それは俳優の状態を保存するためにOKです)

コントローラコード:

​​

マスター俳優:

public class MyGeneratorMaster extends UntypedActor { 

    private int completed = 0; 

    @Override 
    public void postStop() { 
     super.postStop(); 
     System.out.println("Master Killed"); 
    } 

    @Override 
    public void onReceive(Object message) throws Exception { 
     if (message instanceof actors.messages.ConfigMessage) { 
      ConfigMessage config = (ConfigMessage) message; 

      System.out.println("Received Config:" + config.getConfig()); 

      //Need to spawn child actor here.. 
      ActorRef child = this.getContext().actorOf(new Props(MyGeneratorChildWorker.class)); 

      //make the child thread do stuff 
      child.tell(new ConfigMessage("doSomething!")); 

      child.tell(akka.actor.PoisonPill.getInstance());//kill the child after the work is complete... 

     } else if (message instanceof StatusUpdate) { 
      System.out.println("Got Status Update"); 
      completed = ((StatusUpdate) message).getProgress(); 
     } else if (message instanceof StatusMessage) { 
      System.out.println("Got Status Message"); 
      getSender().tell(new ResultMessage("Status: " + completed + "%"), getSelf()); 

      if(completed == 100) 
      { 
       //kill this actor, we're done! 
       //could also call stop... 
       this.getSelf().tell(akka.actor.PoisonPill.getInstance()); 
      } 
     } else { 
      System.out.println("unhandled message"+message.toString()); 
      unhandled(message); 
     } 

    } 
} 

子役:

public class MyGeneratorChildWorker extends UntypedActor { 

    @Override 
    public void postStop() { 
     super.postStop();  
     System.out.println("Child Killed"); 
    } 

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

     if (message instanceof ConfigMessage) { 

      System.out.println("Created Child Worker"); 

      System.out.println("Doing Work:"); 
      try { 

       for (int i = 0; i <= 100; i++) { 


        //update parent 
        this.context().parent().tell(new StatusUpdate(i)); 
        long j = 1; 
        //waste loads of cpu cycles 
        while (j < 1E8) { 
         j = j + 1; 
        } 
       } 
      } catch (Exception ex) { 

      } 
      System.out.println("Done Work:"); 


     } else 
      unhandled(message); 
    } 
} 

ロングポーリングを使用したビューページJavaScript:

@(message: String)(title: String)(id: String)@main(title) { 

<h2>@message</h2> 

     <script type="text/javascript"> 

      function getPercentage() 
      { 

       $.ajax({ 
        type: "GET", 
        url: "/status/@id", 
        dataType: "html", 
        success: function(html) 
         { 
         $('#status').html(html); 


         } 
       }); 

      } 

      $(document).ready(function() { 


      setInterval("getPercentage()",100); 
      }); 



     </script> 

     <div id="status"> 

     </div> 

} 
+2

いいですね、試してみましょう。再生は素晴らしいですが、このようなことはほとんどコードサンプルがありません。 – Steve

+3

私は知っている、その非常にイライラ! –

+1

「アクターに状態を保存することはできますか?アクターは、状態を保存することができる数少ない場所の1つです。 – EECOLOR

関連する問題