如何在关机时停止Akka线程 [英] How to stop an akka thread on shutdown

查看:72
本文介绍了如何在关机时停止Akka线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用播放框架(版本2.0.1). 需要我的应用程序才能收听udp广播消息.因此,我需要一个新线程来监视套接字并接收所需的字节. 在研究了游戏框架之后,似乎使用了Akka系统来处理线程/作业. 因此,我实现了一个新的后台akka任务,该任务可读取我的套接字.见下文.一切都按预期工作,并且能够在后台任务中接收所需的数据. 我遇到的问题是当我关闭服务器时.在开发中,按ControlD.它说消息"[信息]播放-关闭应用程序默认Akka系统".但随后挂起,再也没有关闭我的后台线程.

I am currently using the play framework (version 2.0.1). My application is required to listen to udp broadcast messages. So I need a new thread to monitor the socket and receive the required bytes. After looking into the play framework it seems the Akka system is used to handle threads/jobs. So I have implemented a new background akka task that reads my sockets. See below. Everything is working how I expected and I am able to receive the data I want in the background task. The problem I have is when I shutdown the server. In development pressing Control D. It says the message "[info] play - Shutdown application default Akka system." but then hangs and never shuts down my background thread.

public running = false;
public void onReceive(Object message) {
    if (message instanceof SystemConfiguration) {
        try {
            InetAddress group = InetAddress.getByName("222.1.1.1");
            MulticastSocket socket = new MulticastSocket(54321);
            socket.joinGroup(group);
            running = true;

            while (running) {
                Logger.info("MultiCastController - waiting to receive message");
                byte[] buf = new byte[1000];
                DatagramPacket recv = new DatagramPacket(buf, buf.length);
                socket.setSoTimeout(30000);
                try {
                    socket.receive(recv);
                    String msgString = new String(recv.getData()).trim();
                    Logger.info("retreived: " + msgString);
                } catch (SocketTimeoutException e) {
                    Logger.info("MultiCastController - timed out"
                            + recv.getData().toString());
                }
            }
        } catch (Exception ex) {
            Logger.info("Errror message caught: " + ex.toString());
        }
    } else {
        Logger.info("Error starting multicast receiver incorrect value passed: ");
    }
}

我尝试覆盖停靠点,但似乎从未被调用.我也尝试过akka.system().isTerminated(),但是没有用.

I have tried to override post stop but it never seems to be called. I have also tried akka.system().isTerminated() but it didnt work.

@Override
public void postStop()
{
    Logger.info("MultiCastController - postStop() - stopping thread");
    running = false;
}

我一直在寻找的另一个可能的选择是,有什么方法可以获取akka状态吗?它正在关闭,所以我可以做类似的事情

The other possible option I have been searching for is there any way to get the akka state? that it is shutting down so i can do something like

while(正在运行&& akka.system().alive()).我发现,如果确实收到消息,则由于ebean服务器已关闭而崩溃.我什至可以进行破解并检查它是否还活着吗?

while(running && akka.system().alive()). I have found that if i do receive a message it crashes because the ebean server has been shut down. Can I even do a hack and check if that is alive somehow?

while(正在运行&& ebean.server.isalive())

while(running && ebean.server.isalive())

推荐答案

将您的线程创建为守护线程,然后在应用程序退出时存在.

Create your thread as a daemon thread, then it will exist when the application exits.

或者您可以注册一个ActorSystem关闭时要执行的回调:

Or you can register a callback to be executed when the ActorSystem shuts down:

def registerOnTermination(代码:可运行):单位

def registerOnTermination (code: Runnable): Unit

注册一个代码块(回调),以在该actor系统中的所有actor被使用之后运行 停止了.

Register a block of code (callback) to run after all actors in this actor system have been stopped.

来自: http://doc.akka.io /api/akka/2.0.1/#akka.actor.ActorSystem

这篇关于如何在关机时停止Akka线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆