2016-07-15 2 views
1

이전에 나는 루트 (after/user /) 액터 (system.actorOf)를 만드는 것이 비용이 많이 든다는 것을 읽었습니다.공장 출연자 패턴

ClientFactoryActor를 만드는 일반적인 패턴은 주된 책임은 요청시 새로운 액터를 반환하기 만하면됩니다 (예를 들어 클라이언트 당 새로운 websocket 액터가 필요함).

+0

그리고 메모리 누수를 피하기 위해 배우를 죽이는 것을 잊지 마십시오. – ipoteka

답변

1

실제로는 오류 처리 목적을 위해 액터의 계층 구조를 유지해야합니다 (다양한 감독 전략) 액터를 만드는 편리한 방법 중 하나는 원하는 액터에 대한 참조를 반환하는 동반자 객체를 인스턴스화하는 것입니다 지정된 파라미터 (싱글 공장)

object DemoActor { 
     /** 
     * Create Props for an actor of this type. 
     * 
     * @param magicNumber The magic number to be passed to this actor’s constructor. 
     * @return a Props for creating this actor, which can then be further configured 
     *   (e.g. calling `.withDispatcher()` on it) 
     */ 
     def props(magicNumber: Int): Props = Props(new DemoActor(magicNumber)) 
    } 

    class DemoActor(magicNumber: Int) extends Actor { 
     def receive = { 
     case x: Int => sender() ! (x + magicNumber) 
     } 
    } 

    class SomeOtherActor extends Actor { 
     // Props(new DemoActor(42)) would not be safe 
     context.actorOf(DemoActor.props(42), "demo") 
     // ... 
    } 

하나의 좋은 출발점은 Akka documentation 페이지입니다.