How to consume infinite stream in Kotlin?

Viewed 149

I want to write a program that will consume the infinite stream from the web. The stream will come in as JSON through web sockets. I'm looking for the data structured that I should use.

Requirements are:

  1. The stream will be infinite. I don't ever want to stop listening for the new data.
  2. The time between new events in the stream is unknown. They can come rapidly one after another, but also it's possible to have big pauses of several hours. I want to consume when there is something, and wait in the meantime.
  3. I want to consume events sequentally, one after another.

My component should only transform the consumed events, and forward them further. I tried with something like that:

fun consume(stream: Stream<WebEvent>): Sequence<TransformedEvent> {
    return try {
        stream.asSequence().let { seq ->
            var currentEvent = generator.firstEvent(seq.first())

            seq.map {
                currentEvent = generator.nextEvent(currentEvent, it)
                return@map currentEvent
            }
        }
    } catch (e: NoSuchElementException) {
        throw EmptyStreamException(e)
    }
}

The generator in this example "needs" the "previous" event to generate the new one, but that's part of the transformation logic. I'm interested in consuming the stream.

This worked. But I'm wondering is there a better way to do it in Kotlin. Maybe with a blocking queue, or something like that.

0 Answers
Related