Search code examples
scalaakka-stream

How to group elements according to time in streams?


I have a continuous stream of signals receiving. I want to create a time out for this stream. That is only after 30 seconds the output should be shown. How can I give timeout to streams from flows?

Thanks in advance

I tried grouping operations.None worked.


Solution

  • To address this, the Akka Streams API has the groupedWithin mechanism to group events, but also emit events within a bounded time-frame, even if the maximum number of events has not been satisfied. This leads to the efficient batching of events, without the introduction of unacceptable latency.

    Source.tick(0 milliseconds, 10 milliseconds, ())  
    .map(_ => Sample(System.currentTimeMillis(), random.nextFloat()))
    .groupedWithin(1000, 100 milliseconds)
    .map(database.bulkInsert)
    .runWith(Sink.ignore)
    

    Hope this helps.