管理 Akka Actor 系统生命周期
Managing Akka Actor System Lifecycles
我有一个 Actor 系统,我想启动它并一直运行到特定的 actor Destructor
收到 ShutdownNow
消息。当它收到这样的消息时,它基本上执行了一个相当于关闭钩子的操作,关闭了整个 actor 系统,然后父 JVM process/main 线程关闭: Something like this:
class MyApp {
static void main(String[] args) {
// Do some stuff before starting the actor system.
ActorSystem actorSystem = ActorSystem.create(“myapp-actorsys”)
ActorRef initializer = actorSystem.actorOf(Props.create(Initializer) “initializer”)
Inbox inbox = Inbox.create(actorSystem)
inbox.send(initializer, new Initialize())
// Now the actor system is initialized and is running
// Maybe its processing data off a message broker, etc.
// Here I’d like to wait until the actor system shuts down (see Destructor below)
while(actorSystem.isRunning()) { }
// Now do some final tasks.
} // <- end of main thread/parent JVM process
}
class Destructor extends UntypedActor {
@Override
void onReceive(Object msg) {
if(msg instanceof ShutdownNow) {
// Shut down the actor system such that actorSystem.isRunning()
// will return true.
}
}
}
这里的想法是,父 JVM 进程保持活动状态,除非被底层 OS(sigkill 等)或 actor 系统内部的某些东西打断ShutdownNow
消息给适当的参与者。
我该如何实现?
不知道您使用的确切设置是什么:
在您的 actor 系统停止之前,Java 进程不会停止。所以您什么都不用做,只需从您的 Destructor Actor 发送关闭命令即可。
下面的代码对我有用(无论是从进程内部停止进程还是从外部停止进程):
import java.util.concurrent.TimeUnit;
import scala.concurrent.duration.Duration;
import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.actor.Props;
import akka.actor.UntypedActor;
public class MyApp {
public static void main(String[] args) {
// Do some stuff before starting the actor system.
System.out.println("Start Actor System");
ActorSystem actorSystem = ActorSystem.create("myapp-actorsys");
ActorRef destructor = actorSystem.actorOf(Props.create(Destructor.class));
//Java process won't shut down before actor system stops
//Send message after timeout to stop system
actorSystem.scheduler().scheduleOnce(Duration.create(10, TimeUnit.SECONDS),
destructor, new ShutdownNow(), actorSystem.dispatcher(), null);
}
public static class Destructor extends UntypedActor {
public Destructor() {
}
@Override
public void onReceive(Object msg) {
if(msg instanceof ShutdownNow) {
//Shutdown system to stop java process
System.out.println("Stop Actor System");
getContext().system().shutdown();
}
}
}
public static class ShutdownNow {
}
}
我有一个 Actor 系统,我想启动它并一直运行到特定的 actor Destructor
收到 ShutdownNow
消息。当它收到这样的消息时,它基本上执行了一个相当于关闭钩子的操作,关闭了整个 actor 系统,然后父 JVM process/main 线程关闭: Something like this:
class MyApp {
static void main(String[] args) {
// Do some stuff before starting the actor system.
ActorSystem actorSystem = ActorSystem.create(“myapp-actorsys”)
ActorRef initializer = actorSystem.actorOf(Props.create(Initializer) “initializer”)
Inbox inbox = Inbox.create(actorSystem)
inbox.send(initializer, new Initialize())
// Now the actor system is initialized and is running
// Maybe its processing data off a message broker, etc.
// Here I’d like to wait until the actor system shuts down (see Destructor below)
while(actorSystem.isRunning()) { }
// Now do some final tasks.
} // <- end of main thread/parent JVM process
}
class Destructor extends UntypedActor {
@Override
void onReceive(Object msg) {
if(msg instanceof ShutdownNow) {
// Shut down the actor system such that actorSystem.isRunning()
// will return true.
}
}
}
这里的想法是,父 JVM 进程保持活动状态,除非被底层 OS(sigkill 等)或 actor 系统内部的某些东西打断ShutdownNow
消息给适当的参与者。
我该如何实现?
不知道您使用的确切设置是什么:
在您的 actor 系统停止之前,Java 进程不会停止。所以您什么都不用做,只需从您的 Destructor Actor 发送关闭命令即可。
下面的代码对我有用(无论是从进程内部停止进程还是从外部停止进程):
import java.util.concurrent.TimeUnit;
import scala.concurrent.duration.Duration;
import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.actor.Props;
import akka.actor.UntypedActor;
public class MyApp {
public static void main(String[] args) {
// Do some stuff before starting the actor system.
System.out.println("Start Actor System");
ActorSystem actorSystem = ActorSystem.create("myapp-actorsys");
ActorRef destructor = actorSystem.actorOf(Props.create(Destructor.class));
//Java process won't shut down before actor system stops
//Send message after timeout to stop system
actorSystem.scheduler().scheduleOnce(Duration.create(10, TimeUnit.SECONDS),
destructor, new ShutdownNow(), actorSystem.dispatcher(), null);
}
public static class Destructor extends UntypedActor {
public Destructor() {
}
@Override
public void onReceive(Object msg) {
if(msg instanceof ShutdownNow) {
//Shutdown system to stop java process
System.out.println("Stop Actor System");
getContext().system().shutdown();
}
}
}
public static class ShutdownNow {
}
}