0
다음 코드 예제는 MyChildActor
을 만드는 MyParentActor
을 보여줍니다.Akka : Akka 다시 시작 후 메시지 순서
는 메시지를 다시 시작하도록하는 첫 번째 메시지에 대해 예외를 throw합니다.
그러나 달성하고자하는 것은 MyChildActor
을 다시 시작할 때 "메시지 1"을 "메시지 2"전에 처리하는 것입니다.
대신 메시지 1이 사서함 대기열의 끝에 추가되어 메시지 2가 먼저 처리됩니다.
나만의 사서함을 만들지 않고 배우를 다시 시작할 때 원본 메시지를 어떻게 주문합니까?
object TestApp extends App {
var count = 0
val actorSystem = ActorSystem()
val parentActor = actorSystem.actorOf(Props(classOf[MyParentActor]))
parentActor ! "Message 1"
parentActor ! "Message 2"
class MyParentActor extends Actor with ActorLogging{
var childActor: ActorRef = null
@throws[Exception](classOf[Exception])
override def preStart(): Unit = {
childActor = context.actorOf(Props(classOf[MyChildActor]))
}
override def receive = {
case message: Any => {
childActor ! message
}
}
override def supervisorStrategy: SupervisorStrategy = {
OneForOneStrategy() {
case _: CustomException => Restart
case _: Exception => Restart
}
}
}
class MyChildActor extends Actor with ActorLogging{
override def preRestart(reason: Throwable, message: Option[Any]): Unit = {
message match {
case Some(e) => self ! e
}
}
override def receive = {
case message: String => {
if (count == 0) {
count += 1
throw new CustomException("Exception occurred")
}
log.info("Received message {}", message)
}
}
}
class CustomException(message: String) extends RuntimeException(message)
}