我有一個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到底所以不知道如何達到該職位仍是日期。
非常感謝您的意見。
那麼你有什麼反對毒藥的方法? – childofsoong
不,我只是不知道現在在哪裏用它來解決我的問題:正常關機。 – Julian
好吧,我想到的是,如果你可以重構使用工作拉模式,檢查是否所有的演員都完成了工作,這是相當容易的。 – childofsoong