简体   繁体   中英

Polling with Akka-Http stream

I have found an example where akka-http is used with Source.single to make a request. Now I'd like to use Source.tick to implement polling requests which are execute every X seconds like this:

    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.

And then

The “pool client flow” returned by Http().cachedHostConnectionPool(...) has the following type:

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. Eg

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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