Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

접근이 가능하다란 말은 대상에게 메시지를 전송시킬수 있다란 의미이지, 객체 자체를 접근할수 있다란 의미는 아닙니다.( 객체 공유는 액터모델에 위배됨 )


Actor의 생성과 Child Actor 생성

Code Block
languagescala
title기본 액터생성
class PrintMyActorRefActor extends Actor {
  override def receive: Receive = {
    case "printit" =>
      val secondRef = context.actorOf(Props.empty, "second-actor") //자식의 생성은 부모만 할수 있다.
      println(s"Second: $secondRef")
  }
}

val firstRef = system.actorOf(Props[PrintMyActorRefActor], "first-actor")
println(s"First : $firstRef")
firstRef ! "printit"


//액터 생성 순서   first-actor <= second-actor  가 parent <= child 관계가 된다.
First : Actor[akka://testSystem/user/first-actor#1053618476]
Second: Actor[akka://testSystem/user/first-actor/second-actor#-1544706041]

...

Code Block
languagec#
titleActor접근법
linenumberstrue
var parentActor= system.ActorSelection("akka://testSystem/user/first-actor");
var childActor= system.ActorSelection("akka://testSystem/user/first-actor/second-actor");


액터의 소멸

 이런 구조적인 설계로, 부모의 액터를 정지시키면 자식의 액터를 모두 종류후 마지막에 부모 액터가 종료가됩니다.

...

Code Block
languagescala
titleActor 중지
class StartStopActor1 extends Actor {
  override def preStart(): Unit = {
    println("first started")
    context.actorOf(Props[StartStopActor2], "second")
  }
  override def postStop(): Unit = println("first stopped")

  override def receive: Receive = {
    case "stop" => context.stop(self)
  }
}

class StartStopActor2 extends Actor {
  override def preStart(): Unit = println("second started")
  override def postStop(): Unit = println("second stopped")

  // Actor.emptyBehavior is a useful placeholder when we don't
  // want to handle any messages in the actor.
  override def receive: Receive = Actor.emptyBehavior
}

val first = system.actorOf(Props[StartStopActor1], "first")
first ! "stop"


//OutPut
//first started
//second started
//second stopped
//first stopped


Supervision을 통한 장애처리

Child 액트가 장애발생시, 디양한 복구전략( 3초간 30초동안시도)을 통해 Child액트의 복구가 가능하다. 

Code Block
languagec#
linenumberstrue
var supervisor = BackoffSupervisor.Props(
                    Backoff.OnFailure(
                        childProps,
                        childName: "supervised-actor",
                        minBackoff: TimeSpan.FromSeconds(3),
                        maxBackoff: TimeSpan.FromSeconds(30),
                        randomFactor: 0.2)
                    );