0
我寫了一個簡單的演員,下載一個網頁並將此頁面的主體發送給其發件人。我使用Akka HTTP來構建HTTP請求並處理HTTP響應。這裏是我的代碼:Akka HTTP只響應4個請求
class Downloader(uri: String) extends Actor {
import akka.pattern.pipe
import context.dispatcher
final implicit val materializer: ActorMaterializer = ActorMaterializer(ActorMaterializerSettings(context.system))
val http = Http(context.system)
http.singleRequest(HttpRequest(uri = uri)) pipeTo self
println(s"SENDING request to $uri")
def receive = {
case HttpResponse(StatusCodes.OK, headers, entity, _) =>
println(s"HttpResponse: SUCCESS")
val body = entity.dataBytes.runFold(ByteString(""))(_ ++ _) map (bytes => bytes.decodeString(ByteString.UTF_8)) foreach println
sender() ! body
context.stop(self)
case HttpResponse(code, _, _, _) =>
println(s"HttpResponse: FAILURE")
context.stop(self)
}
}
在主程序中,我創建了10個角色,開始在構造函數中下載他們的網頁。
val system = ActorSystem("akkaHttpClient")
for (i <- 1 to 10)
system.actorOf(Props(classOf[Downloader], "http://akka.io"), s"downloader-$i")
Thread.sleep(20000)
val termFuture = system.terminate()
Await.ready(termFuture, Duration.Inf)
不幸的是,只創造了10名演員4得到迴應:
SENDING request to http://akka.io
SENDING request to http://akka.io
SENDING request to http://akka.io
SENDING request to http://akka.io
SENDING request to http://akka.io
SENDING request to http://akka.io
SENDING request to http://akka.io
SENDING request to http://akka.io
SENDING request to http://akka.io
SENDING request to http://akka.io
HttpResponse: SUCCESS
HttpResponse: SUCCESS
HttpResponse: SUCCESS
HttpResponse: SUCCESS
的哪些錯誤?我忘了發佈一些資源嗎?
這是正確的方法與Akka HTTP同時下載多個網頁嗎?