ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Actor 생성
    akka & pekko/actors 2023. 7. 17. 11:15

     

    import akka.actor.{Actor, ActorRef, ActorSystem, Props}
    
    object AkkaMain {
      def main(args: Array[String]): Unit = {
        // name 으로는 alphabet 만 사용 가능 ('-, _, space' 도 사용 불가)
        val actorSystem: ActorSystem = ActorSystem(name = "firstActorSystem")
        println(actorSystem.name)
    
        class Person(name: String) extends Actor {
          // type Receive = PartialFunction[Any, Unit]
          override def receive: Receive = {
            case "hi" => println(s"Hi, my name is $name")
            case _    =>
          }
        }
    
        object Person {
          def props(name: String): Props = Props(new Person(name))
        }
    
    //    val person = actorSystem.actorOf(Props(new Person("Bob")))
    // actor 를 만들 때 아래처럼 companion object 를 이용하는 것이 권장되는 방법임
        val person: ActorRef = actorSystem.actorOf(Person.props("Bob"))
        person ! "hi"
      }
    }

    'akka & pekko > actors' 카테고리의 다른 글

    Akka Logging  (0) 2023.07.17
    Child Actor  (0) 2023.07.17
    Behavior control (become / unbecome)  (0) 2023.07.17
    Messages and Behavior  (0) 2023.07.17
    akka actors 소개  (0) 2023.07.17

    댓글

Designed by Tistory.