akka & pekko/streams
stream 처리 완료 후 ActorSystem 종료하기
wefree
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())
}
}