我有以下可以很好地工作的流:
source
.map(x => HttpRequest(uri = x.rawRequest))
.via(Http().outgoingConnection(host, port))
.to(Sink.actorRef(myActor, IsDone))
.run()
和一个简单的actor,用于在流完成时处理响应状态和最终消息:
/**
* A simple actor to count how many rows have been processed
* in the complete process given a http status
*
* It also finish the main thread upon a message of type [[IsDone]] is received
*/
class MyActor extends Actor with ActorLogging {
var totalProcessed = 0
def receive = LoggingReceive {
case response: HttpResponse =>
if(response.status.isSuccess()) {
totalProcessed = totalProcessed + 1
} else if(response.status.isFailure()) {
log.error(s"Http response error: ${response.status.intValue()} - ${response.status.reason()}")
} else {
log.error(s"Error: ${response.status.intValue()} - ${response.status.reason()}")
}
case IsDone =>
println(s"total processed: $totalProcessed")
sys.exit()
}
}
case object IsDone
我不知道这是否是计算事物和处理响应状态的最佳方法,但它到目前为止一直在工作。
问题是如何以一种我可以知道哪个请求导致特定错误的方式将原始请求传递给actor。
我的演员可以期待以下内容:
case (request: String, response: HttpResponse) =>
但是如何传递我在管道开头的信息呢?
我在考虑map
这样:
source
.map(x => (HttpRequest(uri = x.rawRequest), x.rawRequest))
但我不知道如何解雇Http流。
有什么建议吗?
答案 0 :(得分:1)
使用@cmbaxter帮助,我可以使用以下代码解决我的问题:
val poolClientFlow = Http().cachedHostConnectionPool[String](host, port)
source
.map(url => HttpRequest(uri = url) -> url)
.via(poolClientFlow)
.to(Sink.actorRef(myActor, IsDone))
.run()
现在我的演员能够收到这个:
case (respTry: Try[HttpResponse], request: String) =>