在生命周期示例中未执行Akka postStop

问题描述 投票:0回答:1

我是akka的新手,我一直在关注文档示例。我已经运行了lifecycle example来停止actor,但是输出不包含清理消息。看来这个例子是错误的,或者我错过了某事。

为什么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$4 - Worker b stopped
22:32:12.011 [B7700-akka.actor.default-dispatcher-6] INFO Job$$anonfun$apply$4 - Worker a stopped
akka actor
1个回答
0
投票

这是2.6.4中修复的错误,请参见https://github.com/akka/akka/issues/28557

© www.soinside.com 2019 - 2024. All rights reserved.