akka-stream

Backpressure strategies for Akka Stream Source.queue not working

為{幸葍}努か 提交于 2019-12-06 11:12:38
问题 I'm trying to understand why the below code snippet is doing what it's doing. I would have thought that because the Sink cannot produce demand faster than the Source is producing content, then I would be getting dropped messages in response to some of the offers (overflow strategy is set to Drop Buffer) and also an error and queue closed message after the self destruct piece. The snippet: package playground import java.time.LocalDateTime import java.util.concurrent.atomic.AtomicInteger import

RestartFlow in Akka Streams not working as expected

孤者浪人 提交于 2019-12-06 09:40:35
问题 I am using Delayed restarts with backoff stage feature of Akka Streams which doesn't seem to work for me. The code for my test is: object Test { import akka.stream.scaladsl.{ Flow, RestartFlow, Sink, Source } import scala.concurrent.duration._ import akka.actor.ActorSystem import akka.stream.ActorMaterializer implicit val actorSystem = ActorSystem() implicit val mat = ActorMaterializer() def main(args: Array[String]): Unit = { val source = Source(1 to 10) val flow = Flow[Int].map { x =>

How to send a file as a response using akka http?

天涯浪子 提交于 2019-12-06 08:12:48
I am a little new to akka world, so my domain of knowledge is a bit small. I am creating an https server and handling it using akka streams and http, for a specific url, i need to send a file back to client. How can i achieve that using akka streams and avoiding the akka routes. def handleCall(request:HttpRequest):HttpResponse = { logger.info("Request is {}",request) val uri:String = request.getUri().path() if(uri == "/download"){ val f = new File("/1000.txt") logger.info("file download") return HttpEntity( //What should i put here if i want to return a text file. ) } If the file is likely to

Reading a large file using Akka Streams

二次信任 提交于 2019-12-06 07:53:43
问题 I'm trying out Akka Streams and here is a short snippet that I have: override def main(args: Array[String]) { val filePath = "/Users/joe/Softwares/data/FoodFacts.csv"//args(0) val file = new File(filePath) println(file.getAbsolutePath) // read 1MB of file as a stream val fileSource = SynchronousFileSource(file, 1 * 1024 * 1024) val shaFlow = fileSource.map(chunk => { println(s"the string obtained is ${chunk.toString}") }) shaFlow.to(Sink.foreach(println(_))).run // fails with a null pointer

Akka Streams / HTTP: Get original request from response

本秂侑毒 提交于 2019-12-06 06:04:15
I have an Akka Streams source that goes through a flow and posts an HTTP request: source.map(toRequest) .via(Http().outgoingConnection(host)) .map(toMessage) Suppose that the toRequest method maps a string to an HttpRequest , and the toMessage method maps the HttpResponse to a message class that is needed to process downstream. Suppose that the message class needs to contain some of the original information. Is it possible to get the original HttpRequest from the HttpResponse ? If not, is there any way to keep some of the original information? One approach is to use a Future -based variant of

How do you throttle Flow in the latest Akka (2.4.6)?

隐身守侯 提交于 2019-12-06 05:55:54
问题 How do you throttle Flow in the latest Akka (2.4.6) ? I'd like to throttle Http client flow to limit number of requests to 3 requests per second. I found following example online but it's for old Akka and akka-streams API changed so much that I can't figure out how to rewrite it. def throttled[T](rate: FiniteDuration): Flow[T, T] = { val tickSource: Source[Unit] = TickSource(rate, rate, () => ()) val zip = Zip[T, Unit] val in = UndefinedSource[T] val out = UndefinedSink[T] PartialFlowGraph {

How to attach multiple actors as sources to an Akka stream?

旧巷老猫 提交于 2019-12-06 03:02:44
问题 I am trying to build and run an akka stream flow (in Java DSL) with 2 actors as sources, then a merge junction and then 1 sink: Source<Integer, ActorRef> src1 = Source.actorRef(100, OverflowStrategy.backpressure()); Source<Integer, ActorRef> src2 = Source.actorRef(100, OverflowStrategy.backpressure()); Sink<Integer, BoxedUnit> sink = Flow.of(Integer.class).to(Sink.foreach(System.out::println)); RunnableFlow<BoxedUnit> closed = FlowGraph.factory().closed(sink, (b, out) -> { UniformFanInShape

How to run Akka Streams graph on a separate dispatcher with timeout?

只谈情不闲聊 提交于 2019-12-06 02:32:14
This question is based on a pet project that I did and this SO thread. Inside a Akka HTTP route definition, I start a long-running process, and naturally I want to do that without blocking the user. I'm able to achieve this with the code snippet below: blocking-io-dispatcher { type = Dispatcher executor = "thread-pool-executor" thread-pool-executor { fixed-pool-size = 16 } throughput = 1 } complete { Try(new URL(url)) match { case scala.util.Success(u) => { val src = Source.fromIterator(() => parseMovies(u).iterator) src .via(findMovieByTitleAndYear) .via(persistMovies) .toMat(Sink.fold(Future

akka-stream - How to treat the last element of a stream differently in a Flow/Graph

僤鯓⒐⒋嵵緔 提交于 2019-12-06 01:28:41
问题 I'm trying to implement an Akka Streams Flow that will convert a stream of JSON objects to a stream of a single array of JSON objects. I can use Concat to add an "[" before and "]" after, as well as Zip to insert commas in between elements, but I can't figure out how to not insert the final comma. The code I have so far is: trait JsonStreamSupport { protected def toJsonArrayString[T : Writes] = Flow[T].map(Json.toJson(_)).map(_.toString()).via(jsonArrayWrapper) private[this] val

Closing an Akka stream from inside a GraphStage (Akka 2.4.2)

荒凉一梦 提交于 2019-12-06 01:15:20
In Akka Stream 2.4.2, PushStage has been deprecated. For Streams 2.0.3 I was using the solution from this answer: How does one close an Akka stream? which was: import akka.stream.stage._ val closeStage = new PushStage[Tpe, Tpe] { override def onPush(elem: Tpe, ctx: Context[Tpe]) = elem match { case elem if shouldCloseStream ⇒ // println("stream closed") ctx.finish() case elem ⇒ ctx.push(elem) } } How would I close a stream in 2.4.2 immediately, from inside a GraphStage / onPush() ? Use something like this: val closeStage = new GraphStage[FlowShape[Tpe, Tpe]] { val in = Inlet[Tpe]("closeStage