[英]How to shutdown akka from java
我有一個akka(akka-actor_2.11)應用程序,用於對其中一個系統進行壓力測試。 當工作完成時,稱為RunCoordinatorActor的頂級角色可以根據其下屬的響應來知道。
工作完成后,RunCoordinatorActor會調用getContext().system().shutdown()
,然后在main方法中循環檢查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();
}
}
}
我可以看到還有另一個名為terreit()的方法,該方法返回Future,並且如果我將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,因此不確定該帖子的最新狀態。
預先感謝您的輸入。
當我仔細研究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());
}
}
此后,所有人都工作得很好。 我仍然感到驚訝,谷歌冷不帶我去任何現有的例子,展示如何做到這一點。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.