简体   繁体   English

使用Akka-Http流进行轮询

[英]Polling with Akka-Http stream

I have found an example where akka-http is used with Source.single to make a request. 我找到了一个示例 ,其中akka-http与Source.single一起使用来发出请求。 Now I'd like to use Source.tick to implement polling requests which are execute every X seconds like this: 现在,我想使用Source.tick来实现每隔X秒执行一次的轮询请求,如下所示:

    import scala.concurrent.duration._
    val request: _root_.akka.http.scaladsl.model.HttpRequest = RequestBuilding.Get(Uri("http://api.someSite.com"))
    val source: Source[HttpRequest, Cancellable] = Source.tick(1.seconds, 1.seconds, request)
    val sourceWithDest = source.via(Http().superPool())

However, I get a compile error in the last line which I cant resolve(Type mismatch). 但是,我在无法解决的最后一行中遇到了编译错误(类型不匹配)。 Any ideas on what I am doing wrong or suggestions for alternatives? 关于我做错事情的任何想法或其他建议?

As per the docs : 根据文档

The Flow returned by Http().superPool(...) is very similar to the one from the Host-Level Client-Side API, so the Using a Host Connection Pool section also applies here. Http()。superPool(...)返回的流与主机级客户端API中的流非常相似,因此“使用主机连接池”部分也适用于此。

And then 然后

The “pool client flow” returned by Http().cachedHostConnectionPool(...) has the following type: Http()。cachedHostConnectionPool(...)返回的“池客户端流”具有以下类型:

Flow[(HttpRequest, T), (Try[HttpResponse], T), HostConnectionPool]

This is to give client-side code the possibility to implement some logic to match the original requests to the corresponding response. 这是为了使客户端代码可以实现一些逻辑,以将原始请求与相应的响应进行匹配。 Assuming you don't need this kind of behaviour in your case, you can always proceed by appending NotUsed to your request before feeding it to the pool flow. 假设您在这种情况下不需要这种行为,则始终可以通过在请求添加到池流之前在请求中附加NotUsed来继续进行操作。 Eg 例如

val sourceWithDest: Source[Try[HttpResponse], Cancellable] = 
    source.map(req ⇒ (req, NotUsed)).via(Http().superPool[NotUsed]()).map(_._1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM