如何从java关闭akka

如何从java关闭akka,java,akka,Java,Akka,我有一个akka(akka-actor_2.11)应用程序,用于对我们的一个系统进行压力测试。名为RunCoordinatorActor的顶级参与者能够根据其下属在工作完成时的反应来了解情况 当工作完成时,RunCoordinator调用getContext().system().shutdown(),然后在main方法中有一个循环检查system.isTerminated()调用以返回true。一切都很好,我对它的工作方式很满意。但是system.sutdown()和system.isTerm

我有一个akka(akka-actor_2.11)应用程序,用于对我们的一个系统进行压力测试。名为RunCoordinatorActor的顶级参与者能够根据其下属在工作完成时的反应来了解情况

当工作完成时,RunCoordinator调用
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类中对shutdown的调用:

@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) {
    getContext().system().terminate();
}
我可以看到还有另一个名为terminate()的方法,它返回一个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
}
if(WorkDone的消息实例){
未来工作=getContext().system().terminate();
//但是我应该把电话工作放在哪里呢
//我怎样才能让主体意识到这一点
}
我可以在这里找到一些scala示例,但它们最终仍然使用system.shutdown,所以不确定这篇文章是否仍然是最新的


提前感谢您的投入。

一旦我仔细研究ActorSystem API,就不难找到解决方案

我所要做的就是将这个添加到我的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) {
    getContext().system().terminate();
}
并且有一个
Future workOne=system.wheinterminated()在我的主类中定义,更改后成为:

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());
    }
}
publicstaticvoidmain(字符串[]args)引发异常{
if(新参数validator().validate(args)){
//如果参数有效,那么我们可以加载spring应用程序
//上下文在这里。
最终ApplicationContext上下文=新注释ConfigApplicationContext(
M6ApplicationContext.class);
//使用akka系统能够并行发送消息
//而不是自己进行低级线程操作。
最终ActorSystem=context.getBean(ActorSystem.class);
最终未来工作单=system.whenTerminated();
最终ActorRef runCoordinator=system.actorOf(SPRING\u EXT\u PROVIDER.get(system)
.道具(“runCoordinator”)、“runCoordinator”);
runCoordinator.tell(newstarttesting(),ActorRef.noSender());
做{
LOGGER.info(“等待流程完成”);
线程睡眠(60000L);
}而(!workDone.isCompleted());
}
}

这之后一切都很顺利。我仍然很惊讶google cold没有把我带到任何现有的例子来展示如何做到这一点。

那么你对毒药丸方法有什么反对意见吗?没有。我只是不知道现在在哪里使用它来解决我的问题:一个优雅的关机。好吧,我有一个想法,如果你可以重构以使用工作拉动模式,很容易检查所有演员是否都完成了这项工作。