我有一个Akka Streams源,它通过流程并发布HTTP请求:
source.map(toRequest)
.via(Http().outgoingConnection(host))
.map(toMessage)
假设toRequest
方法将字符串映射到HttpRequest
,toMessage
方法将HttpResponse
映射到下游处理所需的消息类。假设消息类需要包含一些原始信息。
是否可以从HttpRequest
获取原始HttpResponse
?如果没有,有没有办法保留一些原始信息?
答案 0 :(得分:3)
一种方法是使用Future
-based variant客户端API和自定义案例类,该案例类包含您要向下游传播的信息。例如:
case class Message(request: HttpRequest, response: HttpResponse)
source
.map(toRequest)
.mapAsync(parallelism = 3) { request => // adjust the level of parallelism as needed
Http().singleRequest(request).map(response => Message(request, response))
}
// continue processing; at this point you have a Source[Message, _]
答案 1 :(得分:1)
您可以使用图形API来绕过您的HttpRequest。一个例子是:
object Main extends App {
implicit val as = ActorSystem("Test")
implicit val m = ActorMaterializer()
implicit val ec = as.dispatcher
def src1 = Source.fromIterator(() => List(1, 2, 3).toIterator)
val srcGraph = Source.fromGraph(GraphDSL.create() { implicit builder: GraphDSL.Builder[NotUsed] =>
import GraphDSL.Implicits._
// Create one flow to prepend the 'Number' string to the input integer
def flow1 = Flow[Int].map { el => s"Number $el" }
// Create a broadcast stage
val broadcast = builder.add(Broadcast[Int](2))
val zip = builder.add(Zip[Int, String]())
src1 ~> broadcast.in
// The 0 port sends the int to the zip stage directly
broadcast.out(0) ~> zip.in0
broadcast.out(1) ~> flow1 ~> zip.in1
SourceShape(zip.out)
})
Source.fromGraph(srcGraph).runForeach(println(_))
}
图表api提供了许多选项来执行此类操作。