繁体   English   中英

如何从Java关闭Akka

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM