2016-04-29 38 views
1

我有一個akka(akka-actor_2.11)應用程序,我們用它來測試我們的系統之一的壓力。名爲RunCoordinatorActor的頂級演員能夠根據工作完成時來自其下屬的響應知道。如何從java中關閉akka

工作完成後,RunCoordinatorActor撥打getContext().system().shutdown(),然後在主要方法中檢查system.isTerminated()調用返回true。所有的工作都很好,我很滿意它的工作方式。然而,system.sutdown()system.isTerminated()方法都被標記爲不推薦使用,我試圖找出正確的方式來實現正常關機而不使用它們。

這裏是我的主類:

public static void main(String[] args) throws Exception { 
    if (new ArgumentsValidator().validate(args)) { 
     // If the arguments are valid then we can load spring application 
     // context on here. 
     final ApplicationContext context = new AnnotationConfigApplicationContext(
       M6ApplicationContext.class); 

     // Use an akka system to be able to send messages in parallel 
     // without doing the low level thread manipulation ourselves. 
     final ActorSystem system = context.getBean(ActorSystem.class); 
     final ActorRef runCoordinator = system.actorOf(SPRING_EXT_PROVIDER.get(system) 
       .props("RunCoordinatorActor"), "runCoordinator"); 
     Thread.sleep(1000); 
     runCoordinator.tell(new StartTesting(), ActorRef.noSender()); 

     do { 
      LOGGER.info("Waiting for the process to finish"); 
      Thread.sleep(60000L); 
      // What would be the alternative for isTerminated() code below 
     } while (!system.isTerminated()); 
    } 
} 

,這裏是我的RunCoordinator類中調用關機:

@Named("RunCoordinatorActor") 
@Scope("prototype") 
public class RunCoordinator extends UntypedActor { 
    @Override 
    public void onReceive(Object message) throws Exception { 
     .... 
     if (message instanceof WorkDone) { 
      getContext().system().shutdown(); 
     } 
    } 
} 

我可以看到有一種叫終止(方法)返回未來,如果我用它取代關機呼叫,它也可以正常工作。

if (message instanceof WorkDone) { 
    Future<Terminated> work = getContext().system().terminate(); 
    // But where should I put the call work.isCompleted() 
    // and how would I make the main aware of it 
} 

我能找到這裏shutdown-patterns-in-akka-2一些Scala的例子,但它們仍然使用system.shutdown到底所以不知道如何達到該職位仍是日期。

非常感謝您的意見。

+0

那麼你有什麼反對毒藥的方法? – childofsoong

+0

不,我只是不知道現在在哪裏用它來解決我的問題:正常關機。 – Julian

+0

好吧,我想到的是,如果你可以重構使用工作拉模式,檢查是否所有的演員都完成了工作,這是相當容易的。 – childofsoong

回答

2

一旦我仔細觀察ActorSystem API,解決方案就不難找到。

我所要做的就是把它添加到我的RunCoordinator類:

if (message instanceof WorkDone) { 
    getContext().system().terminate(); 
} 

而不得不在之後的變化就成了我的主類中定義的Future<Terminated> workDone = system.whenTerminated();

public static void main(String[] args) throws Exception { 
    if (new ArgumentsValidator().validate(args)) { 
     // If the arguments are valid then we can load spring application 
     // context on here. 
     final ApplicationContext context = new AnnotationConfigApplicationContext(
       M6ApplicationContext.class); 

     // Use an akka system to be able to send messages in parallel 
     // without doing the low level thread manipulation ourselves. 
     final ActorSystem system = context.getBean(ActorSystem.class); 
     final Future<Terminated> workDone = system.whenTerminated(); 
     final ActorRef runCoordinator = system.actorOf(SPRING_EXT_PROVIDER.get(system) 
       .props("RunCoordinatorActor"), "runCoordinator"); 
     runCoordinator.tell(new StartTesting(), ActorRef.noSender()); 

     do { 
      LOGGER.info("Waiting for the process to finish"); 
      Thread.sleep(60000L); 
     } while (!workDone.isCompleted()); 
    } 
} 

所有工作非常在此之後。我仍然感到驚訝谷歌感冒並沒有帶我現有的任何示例如何做到這一點。