2012-04-16 26 views
5

Tôi có một diễn viên Akka xác nhận dữ liệu ngẫu nhiên và thực hiện một số thay đổi đối với nó dựa trên thời gian hiển thị của dữ liệu đó và cập nhật nó. Hiện nay những gì tôi đang làm là sử dụng mã này bên trong một bộ điều khiển:Chơi Framework 2.0 lên lịch một diễn viên Akka khi khởi chạy máy chủ

static ActorRef instance = Akka.system().actorOf(new Props(ValidateAndChangeIt.class)); 
static { 
    Akka.system().scheduler().schedule(
     Duration.Zero(), 
     Duration.create(5, TimeUnit.MINUTES), 
     instance, "VALIDATE" 
    ); 
} 

Vấn đề với việc sử dụng này bên trong một bộ điều khiển là ai đó đã truy cập vào một trang xử lý bởi bộ điều khiển cho các diễn viên để bắt đầu, và nếu điều này không xảy ra, mọi thứ đều dừng lại.

Có cách nào để thực hiện việc này khi bắt đầu máy chủ không? Tôi thực sự không biết nó hoạt động như thế nào nếu tác nhân tạo ra một ngoại lệ. Nó có dừng lịch trình trong tương lai hay không? Trong trường hợp không, liệu có cách nào để làm cho lịch trình diễn viên trở lại trong trường hợp có sự cố hoặc lỗi không?

Trả lời

13

Đối với chạy mã của bạn lúc khởi động máy chủ, hãy nhìn vào các Global object: di chuyển mã từ điều khiển của bạn đến onStart() phương pháp:

public class Global extends GlobalSettings { 

    @Override 
    public void onStart(Application app) { 
    ActorRef instance = Akka.system().actorOf(new Props(ValidateAndChangeIt.class)); 
    Akka.system().scheduler().schedule(
     Duration.Zero(), 
     Duration.create(5, TimeUnit.MINUTES), 
     instance, "VALIDATE" 
    ); 
    } 

} 
+0

việc này giúp ích rất nhiều –

1

Chơi Framework cung cấp một cách mà lịch trình của một công việc có thể được thực hiện trong Global.java mà không cần bạn gọi nó một cách rõ ràng.

public class Global extends GlobalSettings { 

    private Cancellable scheduler; 

    @Override 
    public void onStart(Application app) { 
     super.onStart(app); 
     schedule(); 
    } 

    @Override 
    public void onStop(Application app) { 
    //Stop the scheduler 
     if (scheduler != null) { 
      scheduler.cancel(); 
      this.scheduler = null; 
     } 
    } 
    private void schedule() { 
     try { 
      ActorRef helloActor = Akka.system().actorOf(new Props(HelloActor.class)); 
      scheduler = Akka.system().scheduler().schedule(
        Duration.create(0, TimeUnit.MILLISECONDS), //Initial delay 0 milliseconds 
        Duration.create(30, TimeUnit.MINUTES),  //Frequency 30 minutes 
        helloActor, 
        "tick", 
        Akka.system().dispatcher(), null); 
     }catch (IllegalStateException e){ 
      Logger.error("Error caused by reloading application", e); 
     }catch (Exception e) { 
      Logger.error("", e); 
     } 
    } 
} 

Và tạo ra các diễn viên, HelloActor.java Trong trên onReceive phương pháp, bạn có thể làm quá trình xử lý dữ liệu, gửi email, vv

public class HelloActor extends UntypedActor { 

    @Override 
    public void onReceive(Object message) throws Exception { 
     // Do the processing here. Or better call another class that does the processing. 
     // This method will be called when ever the job runs. 
     if (message.equals("tick")) { 
      //Do something 
      // controllers.Application.sendEmails(); 
     } else { 
      unhandled(message); 
     } 
    } 
} 

Hope this helps.

Các vấn đề liên quan