Page History
...
Actor의 기본 생성과 Child Actor 생성
Code Block | ||||||||
---|---|---|---|---|---|---|---|---|
| ||||||||
public class PrintMyActorRefActor extends Actor MyActor : ReceiveActor { override def receive: Receive = { private ILoggingAdapter log = Context.GetLogger(); public MyActor() { case "printit" Receive<string>(message => { val secondRef =if context.actorOf(Props.empty, "second-actor") //자식의 생성에 대한 책임은 부모가 가진다. ( 생성하고 자식노드로 런타임 이동하는 형태의 방법 제한) (message == "createChild") { Context.ActorOf<MyActor>("myChild"); printlnSender.Tell(s"Second: $secondRefCreate Child Succed:myChild"); } } val firstRef = system.actorOf(Props[PrintMyActorRefActor], "first-actor") // actorOf 는 Actor생성시 사용됩니다. println(s"First : $firstRef") firstRef ! "printit" // !는 스칼라에서 Input을 위한 키워드이며, c++의 cout >> 과 유사한 키워드 , //C#또는 JAVA에서는 명시적으로 firstRef.Tell("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] else { Sender.Tell("RE:" + message); } }); Receive<SomeMessage>(message => { Sender.Tell("RE:" + message.message); }); } } IActorRef myActor = actorSystem.ActorOf<MyActor>("myactor"); myActor.tell("createChild"); |
아래와같이, rest의 endpoint 와 접근법이 유사합니다.
...
Code Block | ||||||||
---|---|---|---|---|---|---|---|---|
| ||||||||
var parentActor= system.ActorSelection("akka://testSystem/user/first-actormyactor"); var childActor= system.ActorSelection("akka://testSystem/user/first-actor/second-actormyactor/myChild"); |
RestAPI에서는 호출을 한번하면 내부적으로 ( 접속-요청-응답-접속해제) 의 과정을 거치지만
...
Code Block | ||||||||
---|---|---|---|---|---|---|---|---|
| ||||||||
public class StartStopActor1 extends Actor { override def preStart(): Unit = { println : ReceiveActor { private ILoggingAdapter log = Context.GetLogger(); public StartStopActor1() { Receive<string>(message => { if (message == "stop") { Context.Stop(Self); } }); } protected override void PreStart() { log.Info("first started"); context.actorOf(Props[StartStopActor2], Context.ActorOf<StartStopActor2>("second"); } protected override defvoid postStopPostStop(): Unit = println { log.Info("first stopped"); } override def receive: Receive = } public class StartStopActor2 : ReceiveActor { case "stop" private ILoggingAdapter log => context.stop(self) } } class StartStopActor2 extends Actor { override def preStart(): Unit = printlnContext.GetLogger(); public StartStopActor2() { Receive<string>(message => { }); } protected override void PreStart() { log.Info("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" protected override void PostStop() { log.Info("second stopped"); } } IActorRef myActor = actorSystem.ActorOf<StartStopActor1 >("myactor"); myActor.Tell("stop"); //OutPut //first started //second started //second stopped //first stopped |
...