ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • stream 처리 완료 후 ActorSystem 종료하기
    akka & pekko/streams 2023. 7. 22. 18:03

    toMat + run, runWith 로 실행하면 최종 리턴값이 Future 가 되는데, Future 의 onComplete method 를 이용해 종료한다.

     

    import org.apache.pekko.Done
    import org.apache.pekko.actor.ActorSystem
    import org.apache.pekko.stream.scaladsl.{Flow, Keep, Sink, Source}
    
    import scala.concurrent.Future
    
    object TestMain {
      def main(args: Array[String]): Unit = {
        implicit val system: ActorSystem = ActorSystem("pekko")
    
        val source = Source[Int](1 to 10)
        val flow = Flow[Int].map(_ + 1)
        val sink = Sink.foreach[Int] { x =>
          Thread.sleep(100)
          println(x)
        }
    
        val graph = source.via(flow).toMat(sink)(Keep.right)
        val done: Future[Done] = graph.run()
    //    val done: Future[Done] = source.via(flow).runWith(sink)
    
        import system.dispatcher
        done.onComplete(_ => system.terminate())
      }
    }

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

    fibonacci 수열 구현하기  (0) 2023.07.26

    댓글

Designed by Tistory.