본문 바로가기


AKKA

[Akka] 2. Actor의 생성

Actor 생성하기

1. ActorSystem를 사용하여 생성하기

ActorSystem

ActorSystem은 actor를 내부에서 생성하고 동작하게 담고 있는 컨테이너라고 생각하면 되는 하나의 객체이다. 하나의 ActorSystem은 같은 JVM 안에서 돌아간다. actorSystem에서 actorOf라는 함수를 사용해 actor를 생성한다. 첫번째 매개변수에는 Props, 두번째 매개변수에는 actor의 이름이 들어간다.

public class Main {
    public static void main(String[] args){
        ActorSystem actorSystem = ActorSystem.create("TestSystem");
        ActorRef helloWorld = actorSystem.actorOf(Props.create(HelloWorld.class), "helloWorldActor");
    }
}

ActorRef 

ActorRef는 Actor의 주소를 담고 있는 데이터형이다 Actor는 해당 객체로 직접 접근하여 public method를 쓰는 구조가 아니라 해당 actor의 주소로 서로의 메세지를 던져서 해당 함수를 호출하는 방식이다.

Props

Actor에 구현클래스와 해당 구현클래스의 생성자에 필요한 매개변수를 주입하기 위한 객체이다.

2. Context를 사용하여 생성하기

public class HelloWorld extends UnTypedActor{ public HelloWorld(){         ActorRef child = context().actorOf(Props.create(HelloChild.class), "helloChildActor");     } }

ActorContext

ActorContext는 해당 액터의 관점에서 보이는 ActorSystem의 모습을 나타내는 객체이다. 그러니까 해당 Context에서 Actor를 생성하면 ActorSystem안에 같이 있지만 자신을 기준으로 생성되어 자식이 되는 것이라고 생각하면 편하다.

이렇게 Context에서 Actor를 생성하면 helloChildActor는 helloWorldActor의 자식이 되며 이 둘은 생명주기를 같이 한다.


'AKKA' 카테고리의 다른 글

[Akka] 1. Akka 란  (0) 2018.04.30