Akka postStop 未在生命周期示例中执行
Akka postStop not executed in lifecycle example
我是 akka 的新手,我一直在关注文档示例。
我 运行 lifecycle example 关于停止演员,但输出不包含清理消息。看来这个例子是错误的或者我错过了……
为什么 PostStop 信号没有到达 MasterControlProgram?
import akka.actor.typed.Behavior
import akka.actor.typed.scaladsl.Behaviors
import akka.actor.typed.{ ActorSystem, PostStop }
object MasterControlProgram {
sealed trait Command
final case class SpawnJob(name: String) extends Command
final case object GracefulShutdown extends Command
// Predefined cleanup operation
def cleanup(log: Logger): Unit = log.info("Cleaning up!")
def apply(): Behavior[Command] = {
Behaviors
.receive[Command] { (context, message) =>
message match {
case SpawnJob(jobName) =>
context.log.info("Spawning job {}!", jobName)
context.spawn(Job(jobName), name = jobName)
Behaviors.same
case GracefulShutdown =>
context.log.info("Initiating graceful shutdown...")
// perform graceful stop, executing cleanup before final system termination
// behavior executing cleanup is passed as a parameter to Actor.stopped
Behaviors.stopped { () =>
cleanup(context.system.log)
}
}
}
.receiveSignal {
case (context, PostStop) =>
context.log.info("Master Control Program stopped")
Behaviors.same
}
}
}
object Job {
sealed trait Command
def apply(name: String): Behavior[Command] = {
Behaviors.receiveSignal[Command] {
case (context, PostStop) =>
context.log.info("Worker {} stopped", name)
Behaviors.same
}
}
}
import MasterControlProgram._
val system: ActorSystem[Command] = ActorSystem(MasterControlProgram(), "B7700")
system ! SpawnJob("a")
system ! SpawnJob("b")
Thread.sleep(100)
// gracefully stop the system
system ! GracefulShutdown
Thread.sleep(100)
Await.result(system.whenTerminated, 3.seconds)
这是输出:
22:32:11.797 [B7700-akka.actor.default-dispatcher-6] INFO akka.event.slf4j.Slf4jLogger - Slf4jLogger started
22:32:11.868 [B7700-akka.actor.default-dispatcher-3] INFO MasterControlProgram$ - Spawning job a!
22:32:11.877 [B7700-akka.actor.default-dispatcher-3] INFO MasterControlProgram$ - Spawning job b!
22:32:11.958 [B7700-akka.actor.default-dispatcher-6] INFO MasterControlProgram$ - Initiating graceful shutdown...
22:32:12.011 [B7700-akka.actor.default-dispatcher-3] INFO Job$$anonfun$apply - Worker b stopped
22:32:12.011 [B7700-akka.actor.default-dispatcher-6] INFO Job$$anonfun$apply - Worker a stopped
这是一个已在 2.6.4 中修复的错误,请参阅 https://github.com/akka/akka/issues/28557
我是 akka 的新手,我一直在关注文档示例。 我 运行 lifecycle example 关于停止演员,但输出不包含清理消息。看来这个例子是错误的或者我错过了……
为什么 PostStop 信号没有到达 MasterControlProgram?
import akka.actor.typed.Behavior
import akka.actor.typed.scaladsl.Behaviors
import akka.actor.typed.{ ActorSystem, PostStop }
object MasterControlProgram {
sealed trait Command
final case class SpawnJob(name: String) extends Command
final case object GracefulShutdown extends Command
// Predefined cleanup operation
def cleanup(log: Logger): Unit = log.info("Cleaning up!")
def apply(): Behavior[Command] = {
Behaviors
.receive[Command] { (context, message) =>
message match {
case SpawnJob(jobName) =>
context.log.info("Spawning job {}!", jobName)
context.spawn(Job(jobName), name = jobName)
Behaviors.same
case GracefulShutdown =>
context.log.info("Initiating graceful shutdown...")
// perform graceful stop, executing cleanup before final system termination
// behavior executing cleanup is passed as a parameter to Actor.stopped
Behaviors.stopped { () =>
cleanup(context.system.log)
}
}
}
.receiveSignal {
case (context, PostStop) =>
context.log.info("Master Control Program stopped")
Behaviors.same
}
}
}
object Job {
sealed trait Command
def apply(name: String): Behavior[Command] = {
Behaviors.receiveSignal[Command] {
case (context, PostStop) =>
context.log.info("Worker {} stopped", name)
Behaviors.same
}
}
}
import MasterControlProgram._
val system: ActorSystem[Command] = ActorSystem(MasterControlProgram(), "B7700")
system ! SpawnJob("a")
system ! SpawnJob("b")
Thread.sleep(100)
// gracefully stop the system
system ! GracefulShutdown
Thread.sleep(100)
Await.result(system.whenTerminated, 3.seconds)
这是输出:
22:32:11.797 [B7700-akka.actor.default-dispatcher-6] INFO akka.event.slf4j.Slf4jLogger - Slf4jLogger started
22:32:11.868 [B7700-akka.actor.default-dispatcher-3] INFO MasterControlProgram$ - Spawning job a!
22:32:11.877 [B7700-akka.actor.default-dispatcher-3] INFO MasterControlProgram$ - Spawning job b!
22:32:11.958 [B7700-akka.actor.default-dispatcher-6] INFO MasterControlProgram$ - Initiating graceful shutdown...
22:32:12.011 [B7700-akka.actor.default-dispatcher-3] INFO Job$$anonfun$apply - Worker b stopped
22:32:12.011 [B7700-akka.actor.default-dispatcher-6] INFO Job$$anonfun$apply - Worker a stopped
这是一个已在 2.6.4 中修复的错误,请参阅 https://github.com/akka/akka/issues/28557