object Executor extends App {
implicit val system = ActorSystem()
implicit val materializer = ActorMaterializer()
implicit val ec = system.dispatcher
import akka.stream.io._
val file = new File("res/AdviceAnimals.tsv")
import akka.stream.io.Implicits._
val foreach: Future[Long] = SynchronousFileSource(file)
.to( Sink.outputStream(()=>System.out))
.run()
foreach onComplete { v =>
println(s"the foreach is ${v.get}") // the will not be print
}
}
但如果我将Sink.outputStream(()=>System.out)
更改为Sink.ignore
,则println(s"the foreach is ${v.get}")
会打印出来。
有人可以解释原因吗?
答案 0 :(得分:2)
您不是在等待流完成,而是主要方法(Executor的主体)将完成,并且由于主方法已完成退出,JVM将关闭。
您要做的是阻止该线程,并在未来完成之前不退出应用程序。
object Executor extends App {
// ...your stuff with streams...
val yourFuture: Future[Long] = ???
val result = Await.result(yourFuture, 5 seconds)
println(s"the foreach is ${result}")
// stop the actor system (or it will keep the app alive)
system.terminate()
}
答案 1 :(得分:0)
巧妙地,我创建了几乎相同的应用程序,用于测试/播放Akka Streams。 导入的隐含会导致问题吗? 这个应用程序适合我:
object PrintAllInFile extends App {
val file = new java.io.File("data.txt")
implicit val system = ActorSystem("test")
implicit val mat = ActorMaterializer()
implicit val ec = system.dispatcher
SynchronousFileSource(file)
.to(Sink.outputStream(() => System.out))
.run()
.onComplete(_ => system.shutdown())
}
请注意在'onComplete'中停止ActorSystem。否则应用程序将不会退出。